Skip to content

Instantly share code, notes, and snippets.

@wingkwong
Created February 6, 2020 14:54
Show Gist options
  • Save wingkwong/3de8a05e152979c715738a6b6b537ab2 to your computer and use it in GitHub Desktop.
Save wingkwong/3de8a05e152979c715738a6b6b537ab2 to your computer and use it in GitHub Desktop.
Cobra MakeInfo
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
func MakeInfo() *cobra.Command {
info := &cobra.Command{
Use: "info",
Short: "Vivamus elementum semper nisi",
Long: "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor",
Example: "cli info",
SilenceUsage: true,
}
info.RunE = func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
// No args. Print available args to users here.
fmt.Println("Lorem ipsum dolor sit amet, consectetuer adipiscing elit.")
return nil
}
if len(args) != 1 {
// Only one info can be fetched
return fmt.Errorf("Lorem ipsum dolor sit amet, consectetuer adipiscing elit.")
}
commandName := args[0]
switch commandName {
case "command1":
fmt.Printf("Info for app: %s\n", commandName)
fmt.Println(cmd.Command1InfoMsg)
case "command2":
fmt.Printf("Info for app: %s\n", commandName)
fmt.Println(cmd.Command2InfoMsg)
default:
return fmt.Errorf("no info available for command: %s", commandName)
}
return nil
}
return info
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment