Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created October 7, 2018 21:29
Show Gist options
  • Save miguelmota/401210be3a116a3225cf73301a996989 to your computer and use it in GitHub Desktop.
Save miguelmota/401210be3a116a3225cf73301a996989 to your computer and use it in GitHub Desktop.
Golang cobra example
package cmd
import (
"log"
"os"
server "github.com/org/app/server"
"github.com/spf13/cobra"
)
// Start ...
func Start() {
var (
nodeURL string
contractAddress string
)
rootCmd := &cobra.Command{
Use: "app",
}
startCmd := &cobra.Command{
Use: "start",
Short: "Start the server",
Long: "Start the server",
Run: func(cmd *cobra.Command, args []string) {
svr := server.NewServer(&server.Config{
NodeURL: nodeURL,
ContractAddress: contractAddress,
})
svr.Start()
},
}
startCmd.Flags().StringVarP(&contractAddress, "address", "a", "", "contract address")
startCmd.Flags().StringVarP(&nodeURL, "node-url", "u", "", "node url")
rootCmd.AddCommand(startCmd)
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
os.Exit(1)
}
}
go run main.go start --node-url "https://kovan.infura.io" --address "0xcfbded0bbf3726a056b1d9458308dd338e9eea63"
package main
import (
"github.com/org/app/cmd"
)
func main() {
cmd.Start()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment