app

package
v1.7.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2022 License: Apache-2.0 Imports: 72 Imported by: 0

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

View Source
const VerboseUsageTemplate = `` /* 1136-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

View Source
var CompactUsageTemplate = `` /* 1929-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.

View Source
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)
	},
	"SeeAlso": func(cm *kingpin.CmdModel) string {
		cmd := cm.FullCommand()
		url := "https://developer.fastly.com/reference/cli/"
		var trail string
		if len(cmd) > 0 {
			trail = "/"
		}
		return fmt.Sprintf("  %s%s%s", url, strings.ReplaceAll(cmd, " ", "/"), trail)
	},
}

UsageTemplateFuncs is a map of template functions which get passed to the usage template renderer.

Functions

func FastlyAPIClient

func FastlyAPIClient(token, endpoint string) (api.Interface, error)

FastlyAPIClient is a ClientFactory that returns a real Fastly API client using the provided token and endpoint.

func Run

func Run(opts RunOpts) 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

func Usage(args []string, app *kingpin.Application, out, err io.Writer, vars map[string]interface{}) string

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.

func UsageJSON added in v0.13.0

func UsageJSON(app *kingpin.Application) (string, error)

UsageJSON returns a structured representation of the application usage documentation in JSON format. This is useful for machine consumtion.

Types

type APIClientFactory

type APIClientFactory func(token, endpoint string) (api.Interface, error)

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.

type Example added in v1.2.0

type Example struct {
	Cmd         string `json:"cmd"`
	Description string `json:"description,omitempty"`
	Title       string `json:"title"`
}

Example represents a metadata.json command example.

type RunOpts added in v0.33.0

type RunOpts struct {
	APIClient  APIClientFactory
	Args       []string
	ConfigFile config.File
	ConfigPath string
	Env        config.Environment
	ErrLog     errors.LogInterface
	HTTPClient api.HTTPClient
	Stdin      io.Reader
	Stdout     io.Writer
	Versioners Versioners
}

RunOpts represent arguments to Run()

type Versioners added in v0.34.0

type Versioners struct {
	CLI     update.Versioner
	Viceroy update.Versioner
}

Versioners represents all supported versioner types.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL