75 lines
1.8 KiB
Go
75 lines
1.8 KiB
Go
|
|
package cli
|
||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
"io"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"gitea.maximumdirect.net/eric/promptkit"
|
||
|
|
)
|
||
|
|
|
||
|
|
type promptKitSourceFlagEntry struct {
|
||
|
|
selector string
|
||
|
|
path string
|
||
|
|
}
|
||
|
|
|
||
|
|
// promptKitSourceFlagValue treats each occurrence as one selected source path.
|
||
|
|
type promptKitSourceFlagValue struct {
|
||
|
|
entries []promptKitSourceFlagEntry
|
||
|
|
}
|
||
|
|
|
||
|
|
func (value *promptKitSourceFlagValue) Set(path string) error {
|
||
|
|
trimmed := strings.TrimSpace(path)
|
||
|
|
if trimmed == "" {
|
||
|
|
return errors.New("--promptkit-source requires a nonblank path")
|
||
|
|
}
|
||
|
|
|
||
|
|
info, err := os.Stat(filepath.Clean(trimmed))
|
||
|
|
if err != nil {
|
||
|
|
reason := "unavailable"
|
||
|
|
if errors.Is(err, os.ErrPermission) {
|
||
|
|
reason = "permission denied"
|
||
|
|
}
|
||
|
|
return fmt.Errorf("--promptkit-source %q: %s", path, reason)
|
||
|
|
}
|
||
|
|
|
||
|
|
selector := ""
|
||
|
|
switch {
|
||
|
|
case info.IsDir():
|
||
|
|
selector = "directory"
|
||
|
|
case info.Mode().IsRegular():
|
||
|
|
selector = "file"
|
||
|
|
default:
|
||
|
|
return fmt.Errorf("--promptkit-source %q must be a regular file or directory", path)
|
||
|
|
}
|
||
|
|
value.entries = append(value.entries, promptKitSourceFlagEntry{selector: selector, path: path})
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (value *promptKitSourceFlagValue) Get() any {
|
||
|
|
selections := make([]any, 0, len(value.entries))
|
||
|
|
for _, entry := range value.entries {
|
||
|
|
selections = append(selections, map[string]any{entry.selector: entry.path})
|
||
|
|
}
|
||
|
|
return selections
|
||
|
|
}
|
||
|
|
|
||
|
|
func (value *promptKitSourceFlagValue) String() string {
|
||
|
|
paths := make([]string, 0, len(value.entries))
|
||
|
|
for _, entry := range value.entries {
|
||
|
|
paths = append(paths, entry.path)
|
||
|
|
}
|
||
|
|
return fmt.Sprint(paths)
|
||
|
|
}
|
||
|
|
|
||
|
|
func writeFrameworkDiagnostics(output io.Writer, engine *promptkit.Engine) error {
|
||
|
|
view, err := engine.InspectConfiguration()
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("inspect captured PromptKit diagnostics: %w", err)
|
||
|
|
}
|
||
|
|
return writeApplicationWarnings(output, view.Diagnostics())
|
||
|
|
}
|