feat: add completion and manpage commands
This commit is contained in:
parent
937aa09a2c
commit
7bb540bd11
2 changed files with 86 additions and 0 deletions
37
cmd/completion.go
Normal file
37
cmd/completion.go
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var completionCmd = &cobra.Command{
|
||||||
|
Use: "completion [bash|zsh|fish|powershell]",
|
||||||
|
Short: "Generate shell completion scripts",
|
||||||
|
Long: "Generate shell completion scripts for fgj.",
|
||||||
|
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
|
||||||
|
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
|
||||||
|
DisableFlagsInUseLine: true,
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
var out io.Writer = os.Stdout
|
||||||
|
switch args[0] {
|
||||||
|
case "bash":
|
||||||
|
return rootCmd.GenBashCompletion(out)
|
||||||
|
case "zsh":
|
||||||
|
return rootCmd.GenZshCompletion(out)
|
||||||
|
case "fish":
|
||||||
|
return rootCmd.GenFishCompletion(out, true)
|
||||||
|
case "powershell":
|
||||||
|
return rootCmd.GenPowerShellCompletionWithDesc(out)
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unsupported shell: %s", args[0])
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(completionCmd)
|
||||||
|
}
|
||||||
49
cmd/manpages.go
Normal file
49
cmd/manpages.go
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/cobra/doc"
|
||||||
|
)
|
||||||
|
|
||||||
|
var manpagesCmd = &cobra.Command{
|
||||||
|
Use: "manpages",
|
||||||
|
Short: "Generate manpages",
|
||||||
|
Long: "Generate manpages for fgj commands.",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
dir, _ := cmd.Flags().GetString("dir")
|
||||||
|
if dir == "" {
|
||||||
|
return fmt.Errorf("directory is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||||
|
return fmt.Errorf("failed to create %s: %w", dir, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
absDir, err := filepath.Abs(dir)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to resolve %s: %w", dir, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
header := &doc.GenManHeader{
|
||||||
|
Title: "FGJ",
|
||||||
|
Section: "1",
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := doc.GenManTree(rootCmd, header, absDir); err != nil {
|
||||||
|
return fmt.Errorf("failed to generate manpages: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Manpages generated in %s\n", absDir)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(manpagesCmd)
|
||||||
|
manpagesCmd.Flags().String("dir", "", "Output directory for manpages")
|
||||||
|
_ = manpagesCmd.MarkFlagRequired("dir")
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue