An application should be easy to use. You’ve heard it before, about the legacy app that was migrated, and now the guy who managed it is no longer around. No one knows how it works. This is how I use Cobra for easy Golang cli flags for all my apps that need it.
The Concepts Behind Cobra
Start with what Cobra is and then touch on the finer details.
- Cobra is both a library for creating powerful modern CLI applications as well as a program to generate applications and command files.
- And the guidance given by the author on how it should be used: APPNAME VERB NOUN –ADJECTIVE
- Flags should be used to modify the behavior of commands.
Table of Contents
Here’s What You Need
- Golang installed.
- Cobra package.
To get the Cobra binary you need to use this one specific command in a terminal go get -u -v github.com/spf13/cobra/cobra
to get the generator.
How I Use Cobra For Easy Golang CLI Flags
Use this command to create a new Cobra project:mkdir ~/go/src/GoShell cd ~/go/src/GoShell ~/go/bin/cobra init --pkg-name
Notice the success message will be Cobra has two different kinds of flags, local flags, and persistent flags. The difference is that local flags values will be only in scope for that one command they are bound to. The persistent flags can be globally-accessed values.
Cobra Root Command
The root command will run every time the program is executed without any other defined commands.
package cmd import ( "os" "github.com/spf13/cobra" ) var ( rootCmd = &cobra.Command{ Use: "myfirstcobrapp", Short: "This is for my First Cobra App!", Long: `MyFirstCobraApp is an app that will add provided numbers together and has a float switch too!.`, } ) func Execute() { if err := rootCmd.Execute(); err != nil { panic(err) os.Exit(1) } }
Let’s break this down. After importing the necessary packages, I define some global variables
Add New Commands
The cobra binary can be used to add new commands using this easy command
So either use the binary or manually add the new command file. I named mine secondCmd.go and it is in the same directory as cmd. That makes the path for this file > go > src > myFirstCobraApp > cmd > secondCmd.go for your information.
Final Cobra App
The app is now complete once I add this code to the secondCmd.go file.
package cmd import ( "fmt" "strconv" "github.com/spf13/cobra" ) func init() { secondCmd.Flags().BoolP("float", "f", false, "Add Floats") } var secondCmd = &cobra.Command{ Use: "add command", Short: "This is my second command, add", Long: `This command will add two numbers with -number 1 2, and floats with -f added to the end.`, Run: func(cmd *cobra.Command, args []string) { floatSwitch, _ := cmd.Flags().GetBool("float") if floatSwitch { addFloats(args) } else { addInts(args) } }, } func addInts(args []string) { var total int numOne, _ := strconv.Atoi(args[0]) for _, value := range args { intVal, err := strconv.Atoi(value) if err != nil { fmt.Println(err) } total = numOne + intVal } fmt.Printf("Addition of numbers %s is %d", args, total) } func addFloats(args []string) { var sum float64 func addFloats(args []string) { var total float64 for _, value := range args { floatVal, err := strconv.ParseFloat(value, 64) if err != nil { fmt.Println(err) } total = total + floatVal } fmt.Printf("Addition of floating numbers %s is %f", args, total) }
In this file secondCmd.go I define two methods
Recommended Reading
I highly recommend buying Georgia Wiedman’s Penetration Testing. Check the price on Amazon.
Want to learn more ethical hacking? I highly recommend buying my book made for beginners to Pentesting Become An Ethical Hacker. Check the price on Amazon.