Documentation
¶
Overview ¶
Package app provides helpers for creating and running the CLI application. Care has been taken to make the CLI testable through the use of dependency injection and interfaces.
Index ¶
- Constants
- Variables
- func FastlyAPIClient(token, endpoint string) (api.Interface, error)
- func Run(args []string, env config.Environment, file config.File, configFilePath string, ...) error
- func Usage(args []string, app *kingpin.Application, out, err io.Writer) string
- func UsageJSON(app *kingpin.Application) (string, error)
- type APIClientFactory
Constants ¶
const VerboseUsageTemplate = `` /* 1077-byte string literal not displayed */
VerboseUsageTemplate is the full-fat usage template, rendered when users type the long-form e.g. `fastly help service`.
Variables ¶
var CompactUsageTemplate = `` /* 1816-byte string literal not displayed */
CompactUsageTemplate is the default usage template, rendered when users type e.g. just `fastly`, or use the `-h, --help` flag.
var UsageTemplateFuncs = template.FuncMap{ "CommandsToTwoColumns": func(c []*kingpin.CmdModel) [][2]string { rows := [][2]string{} for _, cmd := range c { if !cmd.Hidden { rows = append(rows, [2]string{cmd.Name, cmd.Help}) } } return rows }, "GlobalFlags": func(f []*kingpin.ClauseModel) []*kingpin.ClauseModel { flags := []*kingpin.ClauseModel{} for _, flag := range f { if globalFlags[flag.Name] { flags = append(flags, flag) } } return flags }, "OptionalFlags": func(f []*kingpin.ClauseModel) []*kingpin.ClauseModel { optionalFlags := []*kingpin.ClauseModel{} for _, flag := range f { if !flag.Required && !flag.Hidden && !globalFlags[flag.Name] { optionalFlags = append(optionalFlags, flag) } } return optionalFlags }, "Bold": func(s string) string { return text.Bold(s) }, }
UsageTemplateFuncs is a map of template functions which get passed to the usage template renderer.
Functions ¶
func FastlyAPIClient ¶
FastlyAPIClient is a ClientFactory that returns a real Fastly API client using the provided token and endpoint.
func Run ¶
func Run(args []string, env config.Environment, file config.File, configFilePath string, cf APIClientFactory, httpClient api.HTTPClient, versioner update.Versioner, in io.Reader, out io.Writer) error
Run constructs the application including all of the subcommands, parses the args, invokes the client factory with the token to create a Fastly API client, and executes the chosen command, using the provided io.Reader and io.Writer for input and output, respectively. In the real CLI, func main is just a simple shim to this function; it exists to make end-to-end testing of commands easier/possible.
The Run helper should NOT output any error-related information to the out io.Writer. All error-related information should be encoded into an error type and returned to the caller. This includes usage text.
func Usage ¶
Usage returns a contextual usage string for the application. In order to deal with Kingpin's annoying love of side effects, we have to swap the app.Writers to capture output; the out and err parameters, therefore, are the io.Writers re-assigned to the app via app.Writers after calling Usage.
Types ¶
type APIClientFactory ¶
APIClientFactory creates a Fastly API client (modeled as an api.Interface) from a user-provided API token. It exists as a type in order to parameterize the Run helper with it: in the real CLI, we can use NewClient from the Fastly API client library via RealClient; in tests, we can provide a mock API interface via MockClient.