Add shell completion support
This commit is contained in:
@@ -36,6 +36,8 @@ func run(args []string) error {
|
||||
return runAIInit(args[1:])
|
||||
case "version":
|
||||
return runVersion(args[1:])
|
||||
case "completion":
|
||||
return runCompletion(args[1:])
|
||||
case "validate":
|
||||
return runValidate(args[1:])
|
||||
case "index":
|
||||
@@ -65,6 +67,8 @@ Usage:
|
||||
gsp init [path] [--name project-name] [--entry project.entry] [--force]
|
||||
gsp ai-init [--root .] [--agents] [--skill generic|codex] [--all] [--force]
|
||||
gsp version [--json]
|
||||
gsp completion powershell|bash|zsh|fish
|
||||
gsp completion install powershell
|
||||
gsp validate [--root .] [--out report.json]
|
||||
gsp index [--root .] [--out index.json]
|
||||
gsp trace <id> [--root .] [--depth 3] [--out trace.json]
|
||||
@@ -75,6 +79,79 @@ Usage:
|
||||
`)
|
||||
}
|
||||
|
||||
func runCompletion(args []string) error {
|
||||
if len(args) == 0 {
|
||||
return fmt.Errorf("completion requires powershell, bash, zsh, fish, or install powershell")
|
||||
}
|
||||
if args[0] == "install" {
|
||||
if len(args) != 2 {
|
||||
return fmt.Errorf("completion install requires one shell")
|
||||
}
|
||||
if args[1] != "powershell" {
|
||||
return fmt.Errorf("completion install currently supports powershell")
|
||||
}
|
||||
return installPowerShellCompletion()
|
||||
}
|
||||
if len(args) != 1 {
|
||||
return fmt.Errorf("completion accepts one shell")
|
||||
}
|
||||
switch args[0] {
|
||||
case "powershell":
|
||||
return writeText("", powerShellCompletionScript())
|
||||
case "bash":
|
||||
return writeText("", bashCompletionScript())
|
||||
case "zsh":
|
||||
return writeText("", zshCompletionScript())
|
||||
case "fish":
|
||||
return writeText("", fishCompletionScript())
|
||||
default:
|
||||
return fmt.Errorf("unsupported completion shell %q", args[0])
|
||||
}
|
||||
}
|
||||
|
||||
func installPowerShellCompletion() error {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
completionDir := filepath.Join(home, ".gsp", "completion")
|
||||
completionFile := filepath.Join(completionDir, "gsp.ps1")
|
||||
if err := os.MkdirAll(completionDir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.WriteFile(completionFile, []byte(powerShellCompletionScript()), 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
profilePath := filepath.Join(home, "Documents", "PowerShell", "Microsoft.PowerShell_profile.ps1")
|
||||
if err := os.MkdirAll(filepath.Dir(profilePath), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
sourceLine := fmt.Sprintf(". '%s'", strings.ReplaceAll(completionFile, "'", "''"))
|
||||
existing, err := os.ReadFile(profilePath)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
if !strings.Contains(string(existing), sourceLine) {
|
||||
var builder strings.Builder
|
||||
if len(existing) > 0 {
|
||||
builder.Write(existing)
|
||||
if !strings.HasSuffix(string(existing), "\n") {
|
||||
builder.WriteString("\n")
|
||||
}
|
||||
}
|
||||
builder.WriteString(sourceLine)
|
||||
builder.WriteString("\n")
|
||||
if err := os.WriteFile(profilePath, []byte(builder.String()), 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
fmt.Printf("Installed PowerShell completion to %s\n", completionFile)
|
||||
fmt.Printf("Updated PowerShell profile %s\n", profilePath)
|
||||
fmt.Println("Open a new PowerShell terminal or run the profile file to enable completion.")
|
||||
return nil
|
||||
}
|
||||
|
||||
func runAIInit(args []string) error {
|
||||
fs := flag.NewFlagSet("ai-init", flag.ContinueOnError)
|
||||
root := commonRoot(fs)
|
||||
|
||||
Reference in New Issue
Block a user