Initial commit
This commit is contained in:
71
skills/cobra-patterns/templates/command.go.template
Normal file
71
skills/cobra-patterns/templates/command.go.template
Normal file
@@ -0,0 +1,71 @@
|
||||
package {{.PackageName}}
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
// Command-specific flags
|
||||
{{.CommandName}}Name string
|
||||
{{.CommandName}}Force bool
|
||||
{{.CommandName}}DryRun bool
|
||||
)
|
||||
|
||||
// {{.CommandName}}Cmd represents the {{.CommandName}} command
|
||||
var {{.CommandName}}Cmd = &cobra.Command{
|
||||
Use: "{{.CommandName}} [flags]",
|
||||
Short: "{{.ShortDescription}}",
|
||||
Long: `{{.LongDescription}}
|
||||
|
||||
This command provides {{.CommandName}} functionality with proper
|
||||
error handling and validation.
|
||||
|
||||
Examples:
|
||||
{{.CLIName}} {{.CommandName}} --name example
|
||||
{{.CLIName}} {{.CommandName}} --force
|
||||
{{.CLIName}} {{.CommandName}} --dry-run`,
|
||||
Args: cobra.NoArgs,
|
||||
GroupID: "{{.GroupID}}",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
// Validate required flags
|
||||
if {{.CommandName}}Name == "" {
|
||||
return fmt.Errorf("--name is required")
|
||||
}
|
||||
|
||||
// Check dry-run mode
|
||||
if {{.CommandName}}DryRun {
|
||||
fmt.Printf("DRY RUN: Would execute {{.CommandName}} with name: %s\n", {{.CommandName}}Name)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Execute command logic
|
||||
if cmd.Root().PersistentFlags().Lookup("verbose").Changed {
|
||||
fmt.Printf("Executing {{.CommandName}} in verbose mode...\n")
|
||||
}
|
||||
|
||||
if err := execute{{.CommandName}}({{.CommandName}}Name, {{.CommandName}}Force); err != nil {
|
||||
return fmt.Errorf("{{.CommandName}} failed: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf("Successfully executed {{.CommandName}}: %s\n", {{.CommandName}}Name)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Define flags
|
||||
{{.CommandName}}Cmd.Flags().StringVarP(&{{.CommandName}}Name, "name", "n", "", "resource name (required)")
|
||||
{{.CommandName}}Cmd.Flags().BoolVarP(&{{.CommandName}}Force, "force", "f", false, "force operation")
|
||||
{{.CommandName}}Cmd.Flags().BoolVar(&{{.CommandName}}DryRun, "dry-run", false, "simulate operation without making changes")
|
||||
|
||||
// Mark required flags
|
||||
{{.CommandName}}Cmd.MarkFlagRequired("name")
|
||||
}
|
||||
|
||||
// execute{{.CommandName}} performs the actual operation
|
||||
func execute{{.CommandName}}(name string, force bool) error {
|
||||
// Implementation goes here
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// completionCmd represents the completion command
|
||||
var completionCmd = &cobra.Command{
|
||||
Use: "completion [bash|zsh|fish|powershell]",
|
||||
Short: "Generate completion script",
|
||||
Long: `Generate shell completion script for {{.CLIName}}.
|
||||
|
||||
The completion script must be evaluated to provide interactive
|
||||
completion. This can be done by sourcing it from your shell profile.
|
||||
|
||||
Bash:
|
||||
source <({{.CLIName}} completion bash)
|
||||
|
||||
# To load completions for each session, execute once:
|
||||
# Linux:
|
||||
{{.CLIName}} completion bash > /etc/bash_completion.d/{{.CLIName}}
|
||||
# macOS:
|
||||
{{.CLIName}} completion bash > /usr/local/etc/bash_completion.d/{{.CLIName}}
|
||||
|
||||
Zsh:
|
||||
# If shell completion is not already enabled, enable it:
|
||||
echo "autoload -U compinit; compinit" >> ~/.zshrc
|
||||
|
||||
# To load completions for each session, execute once:
|
||||
{{.CLIName}} completion zsh > "${fpath[1]}/_{{.CLIName}}"
|
||||
|
||||
# You will need to start a new shell for this setup to take effect.
|
||||
|
||||
Fish:
|
||||
{{.CLIName}} completion fish | source
|
||||
|
||||
# To load completions for each session, execute once:
|
||||
{{.CLIName}} completion fish > ~/.config/fish/completions/{{.CLIName}}.fish
|
||||
|
||||
PowerShell:
|
||||
{{.CLIName}} completion powershell | Out-String | Invoke-Expression
|
||||
|
||||
# To load completions for every new session:
|
||||
{{.CLIName}} completion powershell > {{.CLIName}}.ps1
|
||||
# and source this file from your PowerShell profile.
|
||||
`,
|
||||
DisableFlagsInUseLine: true,
|
||||
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
switch args[0] {
|
||||
case "bash":
|
||||
return cmd.Root().GenBashCompletion(os.Stdout)
|
||||
case "zsh":
|
||||
return cmd.Root().GenZshCompletion(os.Stdout)
|
||||
case "fish":
|
||||
return cmd.Root().GenFishCompletion(os.Stdout, true)
|
||||
case "powershell":
|
||||
return cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
|
||||
default:
|
||||
return fmt.Errorf("unsupported shell type: %s", args[0])
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(completionCmd)
|
||||
}
|
||||
13
skills/cobra-patterns/templates/main.go.template
Normal file
13
skills/cobra-patterns/templates/main.go.template
Normal file
@@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"{{.ModulePath}}/cmd"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := cmd.Execute(); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
20
skills/cobra-patterns/templates/nested-command.go.template
Normal file
20
skills/cobra-patterns/templates/nested-command.go.template
Normal file
@@ -0,0 +1,20 @@
|
||||
package {{.PackageName}}
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// {{.CommandName}}Cmd represents the {{.CommandName}} parent command
|
||||
var {{.CommandName}}Cmd = &cobra.Command{
|
||||
Use: "{{.CommandName}}",
|
||||
Short: "{{.ShortDescription}}",
|
||||
Long: `{{.LongDescription}}`,
|
||||
GroupID: "{{.GroupID}}",
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Add subcommands here
|
||||
// {{.CommandName}}Cmd.AddCommand({{.CommandName}}ListCmd)
|
||||
// {{.CommandName}}Cmd.AddCommand({{.CommandName}}CreateCmd)
|
||||
// {{.CommandName}}Cmd.AddCommand({{.CommandName}}DeleteCmd)
|
||||
}
|
||||
95
skills/cobra-patterns/templates/root.go.template
Normal file
95
skills/cobra-patterns/templates/root.go.template
Normal file
@@ -0,0 +1,95 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var (
|
||||
cfgFile string
|
||||
verbose bool
|
||||
output string
|
||||
)
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "{{.CLIName}}",
|
||||
Short: "{{.ShortDescription}}",
|
||||
Long: `{{.LongDescription}}
|
||||
|
||||
This is a production-grade CLI application built with Cobra.
|
||||
It provides a complete command-line interface with proper error
|
||||
handling, configuration management, and extensibility.`,
|
||||
Version: "{{.Version}}",
|
||||
}
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func Execute() error {
|
||||
return rootCmd.Execute()
|
||||
}
|
||||
|
||||
func init() {
|
||||
cobra.OnInitialize(initConfig)
|
||||
|
||||
// Global flags
|
||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{.CLIName}}.yaml)")
|
||||
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")
|
||||
rootCmd.PersistentFlags().StringVarP(&output, "output", "o", "text", "output format (text|json|yaml)")
|
||||
|
||||
// Bind flags to viper
|
||||
viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose"))
|
||||
viper.BindPFlag("output", rootCmd.PersistentFlags().Lookup("output"))
|
||||
|
||||
// Command groups for organized help
|
||||
rootCmd.AddGroup(&cobra.Group{
|
||||
ID: "basic",
|
||||
Title: "Basic Commands:",
|
||||
})
|
||||
rootCmd.AddGroup(&cobra.Group{
|
||||
ID: "management",
|
||||
Title: "Management Commands:",
|
||||
})
|
||||
rootCmd.AddGroup(&cobra.Group{
|
||||
ID: "other",
|
||||
Title: "Other Commands:",
|
||||
})
|
||||
}
|
||||
|
||||
// initConfig reads in config file and ENV variables if set.
|
||||
func initConfig() {
|
||||
if cfgFile != "" {
|
||||
// Use config file from the flag.
|
||||
viper.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
// Find home directory.
|
||||
home, err := os.UserHomeDir()
|
||||
cobra.CheckErr(err)
|
||||
|
||||
// Search config in home directory with name ".{{.CLIName}}" (without extension).
|
||||
viper.AddConfigPath(home)
|
||||
viper.AddConfigPath(".")
|
||||
viper.SetConfigType("yaml")
|
||||
viper.SetConfigName(".{{.CLIName}}")
|
||||
}
|
||||
|
||||
viper.AutomaticEnv() // read in environment variables that match
|
||||
|
||||
// If a config file is found, read it in.
|
||||
if err := viper.ReadInConfig(); err == nil && verbose {
|
||||
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
||||
}
|
||||
}
|
||||
|
||||
// GetVerbose returns whether verbose mode is enabled
|
||||
func GetVerbose() bool {
|
||||
return viper.GetBool("verbose")
|
||||
}
|
||||
|
||||
// GetOutput returns the output format
|
||||
func GetOutput() string {
|
||||
return viper.GetString("output")
|
||||
}
|
||||
Reference in New Issue
Block a user