cli

package
v0.0.0-...-814a86c Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2024 License: Apache-2.0 Imports: 5 Imported by: 136

Documentation

Overview

Package cli is a helper package for "github.com/maruel/subcommands".

It adds a non-intrusive integration with context.Context.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetContext

GetContext sniffs ContextModificator in the app and in the cmd and uses them to derive a context for the command.

Subcommands can use it to get an initial context in their `Run` methods.

Uses `env` to initialize luci/common/system/environ environment in the context. In particular populates unset (but registered in the CLI app) env vars with their default values (if they are not empty).

Order of the context modifications:

  1. Start with the background context.
  2. Apply `app` modifications if `app` implements ContextModificator.
  3. Initialize luci/common/system/environ in the context.
  4. Apply `cmd` modifications if `cmd` implements ContextModificator.

In particular, command's modificator is able to examine env vars in the context.

Types

type Application

type Application struct {
	Name     string
	Title    string
	Context  func(context.Context) context.Context
	Commands []*subcommands.Command
	EnvVars  map[string]subcommands.EnvVarDefinition
	// contains filtered or unexported fields
}

Application is like subcommands.DefaultApplication, except it also implements ContextModificator.

func (*Application) GetCommands

func (a *Application) GetCommands() []*subcommands.Command

GetCommands implements interface subcommands.Application.

func (*Application) GetEnvVars

func (a *Application) GetEnvVars() map[string]subcommands.EnvVarDefinition

GetEnvVars implements interface subcommands.Application.

func (*Application) GetErr

func (a *Application) GetErr() io.Writer

GetErr implements interface subcommands.Application.

func (*Application) GetName

func (a *Application) GetName() string

GetName implements interface subcommands.Application.

func (*Application) GetOut

func (a *Application) GetOut() io.Writer

GetOut implements interface subcommands.Application.

func (*Application) GetTitle

func (a *Application) GetTitle() string

GetTitle implements interface subcommands.Application.

func (*Application) ModifyContext

func (a *Application) ModifyContext(ctx context.Context) context.Context

ModifyContext implements interface ContextModificator.

'ctx' here is always context.Background().

type ContextModificator

type ContextModificator interface {
	ModifyContext(context.Context) context.Context
}

ContextModificator takes a context, adds something, and returns a new one.

It is implemented by Application and can optionally by implemented by subcommands.CommandRun instances. It will be called by GetContext to modify initial context.

Jump to

Keyboard shortcuts

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