Created
February 6, 2020 14:54
-
-
Save wingkwong/3de8a05e152979c715738a6b6b537ab2 to your computer and use it in GitHub Desktop.
Cobra MakeInfo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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