Custom Application Commands

Introduction

In addition to custom flags, an application may register completely new commands. For example, to support alternative topologies or integrated automated acceptance tests as part of a CI/CD pipeline.

To register a custom command, define a new cobra.Command and add it to the sparta.CommandLineOptions.Root command value. Ensure you use the xxxxE Cobra functions so that errors can be properly propagated.

httpServerCommand := &cobra.Command{
  Use:   "httpServer",
  Short: "Sample HelloWorld HTTP server",
  Long:  `Sample HelloWorld HTTP server that binds to port: ` + HTTPServerPort,
  RunE: func(cmd *cobra.Command, args []string) error {
    http.HandleFunc("/", helloWorldResource)
    return http.ListenAndServe(fmt.Sprintf(":%d", HTTPServerPort), nil)
  },
}
sparta.CommandLineOptions.Root.AddCommand(httpServerCommand)

Registering a user-defined command makes that command’s usage information seamlessly integrate with the standard commands:

$ ./SpartaOmega --help
Provision AWS Lambda and EC2 instance with same code

Usage:
  SpartaOmega [command]

Available Commands:
  httpServer  Sample HelloWorld HTTP server
  version     Sparta framework version
  provision   Provision service
  delete      Delete service
  execute     Execute
  describe    Describe service
  explore     Interactively explore service

Flags:
  -l, --level string   Log level [panic, fatal, error, warn, info, debug] (default "info")
  -n, --noop           Dry-run behavior only (do not perform mutations)

Use "SpartaOmega [command] --help" for more information about a command.

And you can query for user-command specific usage as in:

$ ./SpartaOmega httpServer --help
Custom command

Usage:
  SpartaOmega httpServer [flags]

Global Flags:
  -l, --level string   Log level [panic, fatal, error, warn, info, debug] (default "info")
  -n, --noop           Dry-run behavior only (do not perform mutations)