Created
October 7, 2018 21:29
-
-
Save miguelmota/401210be3a116a3225cf73301a996989 to your computer and use it in GitHub Desktop.
Golang cobra example
This file contains 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 ( | |
"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) | |
} | |
} |
This file contains 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
go run main.go start --node-url "https://kovan.infura.io" --address "0xcfbded0bbf3726a056b1d9458308dd338e9eea63" |
This file contains 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 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