github.com/mutagen-io/mutagen@v0.18.0-rc1/cmd/mutagen/generate.go (about) 1 package main 2 3 import ( 4 "fmt" 5 6 "github.com/spf13/cobra" 7 8 "github.com/mutagen-io/mutagen/cmd" 9 ) 10 11 // generateMain is the entry point for the generate command. 12 func generateMain(_ *cobra.Command, _ []string) error { 13 // Generate a Bash completion script, if requested. 14 if generateConfiguration.bashCompletionScript != "" { 15 if err := rootCommand.GenBashCompletionFile(generateConfiguration.bashCompletionScript); err != nil { 16 return fmt.Errorf("unable to generate Bash completion script: %w", err) 17 } 18 } 19 20 // Generate a fish completion script, if requested. 21 if generateConfiguration.fishCompletionScript != "" { 22 if err := rootCommand.GenFishCompletionFile(generateConfiguration.fishCompletionScript, true); err != nil { 23 return fmt.Errorf("unable to generate fish completion script: %w", err) 24 } 25 } 26 27 // Generate a PowerShell completion script, if requested. 28 if generateConfiguration.powerShellCompletionScript != "" { 29 if err := rootCommand.GenPowerShellCompletionFile(generateConfiguration.powerShellCompletionScript); err != nil { 30 return fmt.Errorf("unable to generate PowerShell completion script: %w", err) 31 } 32 } 33 34 // Generate a Zsh completion script, if requested. 35 if generateConfiguration.zshCompletionScript != "" { 36 if err := rootCommand.GenZshCompletionFile(generateConfiguration.zshCompletionScript); err != nil { 37 return fmt.Errorf("unable to generate Zsh completion script: %w", err) 38 } 39 } 40 41 // Success. 42 return nil 43 } 44 45 // generateCommand is the generate command. 46 var generateCommand = &cobra.Command{ 47 Use: "generate", 48 Short: "Generate various files", 49 Args: cmd.DisallowArguments, 50 Hidden: true, 51 RunE: generateMain, 52 SilenceUsage: true, 53 } 54 55 // generateConfiguration stores configuration for the generate command. 56 var generateConfiguration struct { 57 // help indicates whether or not to show help information and exit. 58 help bool 59 // bashCompletionScript indicates the path, if any, at which to generate the 60 // Bash completion script. 61 bashCompletionScript string 62 // fishCompletionScript indicates the path, if any, at which to generate the 63 // fish completion script. 64 fishCompletionScript string 65 // powerShellCompletionScript indicates the path, if any, at which to 66 // generate the PowerShell completion script. 67 powerShellCompletionScript string 68 // zshCompletionScript indicates the path, if any, at which to generate the 69 // Zsh completion script. 70 zshCompletionScript string 71 } 72 73 func init() { 74 // Grab a handle for the command line flags. 75 flags := generateCommand.Flags() 76 77 // Disable alphabetical sorting of flags in help output. 78 flags.SortFlags = false 79 80 // Manually add a help flag to override the default message. Cobra will 81 // still implement its logic automatically. 82 flags.BoolVarP(&generateConfiguration.help, "help", "h", false, "Show help information") 83 84 // Wire up file generation flags. 85 flags.StringVar(&generateConfiguration.bashCompletionScript, "bash-completion-script", "", "Specify the Bash completion script output path") 86 flags.StringVar(&generateConfiguration.fishCompletionScript, "fish-completion-script", "", "Specify the fish completion script output path") 87 flags.StringVar(&generateConfiguration.powerShellCompletionScript, "powershell-completion-script", "", "Specify the PowerShell completion script output path") 88 flags.StringVar(&generateConfiguration.zshCompletionScript, "zsh-completion-script", "", "Specify the Zsh completion script output path") 89 }