// Normalize perform all required normalizations on a given command. func Normalize(cmd *cobra.Command) *cobra.Command { if len(cmd.Long) > 0 { cmd.Long = LongDesc(cmd.Long) } if len(cmd.Example) > 0 { cmd.Example = Examples(cmd.Example) } return cmd }
func loadLongDescription(cmd *cobra.Command, path string) error { for _, cmd := range cmd.Commands() { if cmd.Name() == "" { continue } fullpath := filepath.Join(path, cmd.Name()+".md") if cmd.HasSubCommands() { loadLongDescription(cmd, filepath.Join(path, cmd.Name())) } if _, err := os.Stat(fullpath); err != nil { log.Printf("WARN: %s does not exist, skipping\n", fullpath) continue } content, err := ioutil.ReadFile(fullpath) if err != nil { return err } cmd.Long = string(content) } return nil }
func Deprecated(baseName, to string, parent, cmd *cobra.Command) string { cmd.Long = fmt.Sprintf("Deprecated: This command is deprecated, all its functionalities are covered by \"%s %s\"", baseName, to) cmd.Short = fmt.Sprintf("Deprecated: %s", to) parent.AddCommand(cmd) return cmd.Name() }
func moved(fullName, to string, parent, cmd *cobra.Command) string { cmd.Long = fmt.Sprintf("DEPRECATED: This command has been moved to \"%s %s\"", fullName, to) cmd.Short = fmt.Sprintf("DEPRECATED: %s", to) parent.AddCommand(cmd) return cmd.Name() }
func (c *permissionHandler) DefineFlags(cmd *cobra.Command) { cmd.Flags().BoolVar(&c.writeAccess, "write", false, "Enable write access for the selected repositories") cmd.Long += "\n\nFor Git repositories, pass the --write flag to grant write access." }
func cli() (*cobra.Command, error) { // parse the configDir argument cfgCmd := new(cobra.Command) cfgCmd.Flags().StringVarP(&configDir, "configDir", "d", "", "Configuration directory to be added on top of the search list") cfgCmd.ParseFlags(os.Args) // configuration parameters cfgParams, err := getConfigParams() if err != nil { return nil, err } // overwrites the configuration parameters with the ones specified in the command line (if any) appParams = &cfgParams rootCmd := new(cobra.Command) rootCmd.Flags().StringVarP(&configDir, "configDir", "d", "", "Configuration directory to be added on top of the search list") rootCmd.Flags().StringVarP(&appParams.log.Level, "logLevel", "o", cfgParams.log.Level, "Log level: EMERGENCY, ALERT, CRITICAL, ERROR, WARNING, NOTICE, INFO, DEBUG") rootCmd.Flags().BoolVarP(&appParams.serverMode, "serverMode", "s", cfgParams.serverMode, "Start an HTTP RESTful API server") rootCmd.Flags().StringVarP(&appParams.serverAddress, "serverAddress", "u", cfgParams.serverAddress, "HTTP API address (ip:port) or just (:port)") rootCmd.Flags().StringVarP(&appParams.charset, "charset", "c", cfgParams.charset, "Characters to use to generate a password") rootCmd.Flags().IntVarP(&appParams.length, "length", "l", cfgParams.length, "Length of each password (number of characters or bytes)") rootCmd.Flags().IntVarP(&appParams.quantity, "quantity", "q", cfgParams.quantity, "Number of passwords to generate") rootCmd.Flags().StringVarP(&appParams.stats.Prefix, "statsPrefix", "p", cfgParams.stats.Prefix, "StatsD bucket prefix name") rootCmd.Flags().StringVarP(&appParams.stats.Network, "statsNetwork", "k", cfgParams.stats.Network, "StatsD client network type (udp or tcp)") rootCmd.Flags().StringVarP(&appParams.stats.Address, "statsAddress", "m", cfgParams.stats.Address, "StatsD daemon address (ip:port) or just (:port)") rootCmd.Flags().IntVarP(&appParams.stats.FlushPeriod, "statsFlushPeriod", "r", cfgParams.stats.FlushPeriod, "StatsD client flush period in milliseconds") rootCmd.Use = "rndpwd" rootCmd.Short = "Command-line and Web-service Random Password Generator" rootCmd.Long = `rndpwd is a Command-line and Web-service Random Password Generator` rootCmd.RunE = func(cmd *cobra.Command, args []string) error { // check values err := checkParams(appParams) if err != nil { return err } // initialize StatsD client (ignore errors) initStats(appParams.stats) defer stats.Close() if appParams.serverMode { // start the HTTP server return startServer(appParams.serverAddress) } // generate and print the passwords for _, psw := range getAllPassword(appParams) { fmt.Println(psw) } return nil } // sub-command to print the version var versionCmd = &cobra.Command{ Use: "version", Short: "print this program version", Long: `print this program version`, Run: func(cmd *cobra.Command, args []string) { fmt.Println(ProgramVersion) }, } rootCmd.AddCommand(versionCmd) return rootCmd, nil }