cobra

A CLI tool.

File Structure

▾ appName/
    ▾ cmd/
        add.go
        your.go
        commands.go
        here.go
      main.go

How to use

// put this in main.go
package main

import (
  "{pathToYourApp}/cmd"
)

func main() {
  cmd.Execute()
}
// put this in root.go
var rootCmd = &cobra.Command{
  Use:   "hugo",
  Short: "Hugo is a very fast static site generator",
  Long: `A Fast and Flexible Static Site Generator built with
                love by spf13 and friends in Go.
                Complete documentation is available at http://hugo.spf13.com`,
  Run: func(cmd *cobra.Command, args []string) {
    // Do Stuff Here
  },
}

func Execute() {
  if err := rootCmd.Execute(); err != nil {
    fmt.Fprintln(os.Stderr, err)
    os.Exit(1)
  }
}

Initialize and Configure

func init() {
	cobra.OnInitialize(initConfig)
	// set flags...
	// add commands...
}

func initConfig

How to run

// in pacakge.go
// in root.go
var rootCmd = &cobra.Command{
  Use: "app"
  ...
}

// in package cmd
// in whatever a file you want, ex: run.go
var runCmd = &cobra.Command{
	Use: "run"
	...
}

func init() {
	...
	rootCmd.AddCommand(runCmd)
}
$ app run ...

Source