github.com/mweagle/Sparta@v1.15.0/docs_source/content/reference/application/custom_commands.md (about) 1 --- 2 date: 2016-03-09T19:56:50+01:00 3 title: Custom Commands 4 weight: 10 5 --- 6 7 In addition to [custom flags](/reference/application/custom_flags), an application may register completely new commands. For example, to support [alternative topologies](/reference/application/custom_flags) or integrated automated acceptance tests as part of a CI/CD pipeline. 8 9 To register a custom command, define a new [cobra.Command](https://github.com/spf13/cobra) and add it to the `sparta.CommandLineOptions.Root` command value. Ensure you use the `xxxxE` Cobra functions so that errors can be properly propagated. 10 11 ```go 12 httpServerCommand := &cobra.Command{ 13 Use: "httpServer", 14 Short: "Sample HelloWorld HTTP server", 15 Long: `Sample HelloWorld HTTP server that binds to port: ` + HTTPServerPort, 16 RunE: func(cmd *cobra.Command, args []string) error { 17 http.HandleFunc("/", helloWorldResource) 18 return http.ListenAndServe(fmt.Sprintf(":%d", HTTPServerPort), nil) 19 }, 20 } 21 sparta.CommandLineOptions.Root.AddCommand(httpServerCommand) 22 ``` 23 24 Registering a user-defined command makes that command's usage information seamlessly integrate with the standard commands: 25 26 ```bash 27 $ go run main.go --help 28 Provision AWS Lambda and EC2 instance with same code 29 30 Usage: 31 main [command] 32 33 Available Commands: 34 delete Delete service 35 describe Describe service 36 execute Start the application and begin handling events 37 explore Interactively explore a provisioned service 38 help Help about any command 39 httpServer Sample HelloWorld HTTP server 40 profile Interactively examine service pprof output 41 provision Provision service 42 status Produce a report for a provisioned service 43 version Display version information 44 45 Flags: 46 -f, --format string Log format [text, json] (default "text") 47 -h, --help help for main 48 --ldflags string Go linker string definition flags (https://golang.org/cmd/link/) 49 -l, --level string Log level [panic, fatal, error, warn, info, debug] (default "info") 50 --nocolor Boolean flag to suppress colorized TTY output 51 -n, --noop Dry-run behavior only (do not perform mutations) 52 -t, --tags string Optional build tags for conditional compilation 53 -z, --timestamps Include UTC timestamp log line prefix 54 ``` 55 56 And you can query for user-command specific usage as in: 57 58 ```bash 59 60 $ ./SpartaOmega httpServer --help 61 Custom command 62 63 Usage: 64 SpartaOmega httpServer [flags] 65 66 Global Flags: 67 -l, --level string Log level [panic, fatal, error, warn, info, debug] (default "info") 68 -n, --noop Dry-run behavior only (do not perform mutations) 69 70 ```