cli

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2021 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrLoginURLMissing = errors.New("MissingLoginURL")

ErrLoginURLMissing presents error that login URL option is unspecified in neither command line nor configuration file.

View Source
var ErrPasswordMissing = errors.New("MissingPassword")

ErrPasswordMissing presents error that password option is unspecified in neither command line nor configuration file.

View Source
var ErrUsernameMissing = errors.New("MissingUsername")

ErrUsernameMissing presents error that username option is unspecified in neither command line nor configuration file.

Functions

This section is empty.

Types

type CleanupFunc

type CleanupFunc = func()

CleanupFunc is the type of cleanup function returned by Barton. CleanupFunc instances releases resource gracefully.

type CobraRunEFunc

type CobraRunEFunc = func(*cobra.Command, []string) error

CobraRunEFunc is the type of Cobra's command processor function, used by cobra.Command.RunE.

type HTTPBasicLogin

type HTTPBasicLogin struct {
	// contains filtered or unexported fields
}

HTTPBasicLogin defines a configuration that creates a subcommand to request JWT token via HTTP basic config. It does the work by taking a combination username and password, plus a remote login URL. Then it returns a JWT token remotely, and save to local configuration.

func NewHTTPBasicLogin

func NewHTTPBasicLogin(name, fallbackLoginURL string) *HTTPBasicLogin

NewHTTPBasicLogin creates a new configuration object to form a login command.

func (*HTTPBasicLogin) AferoFS

func (c *HTTPBasicLogin) AferoFS(fs afero.Fs)

AferoFS method sets an instance of afero.Fs to decide the file system we want to write to. Same with RootCLI. This method is useful during unit test. Similar with Viper() method, it's called by RootCLI.AddSubcommand() to ensure a consistent file system target.

func (*HTTPBasicLogin) Name

func (c *HTTPBasicLogin) Name() string

Name method returns name of subcommand.

func (*HTTPBasicLogin) NewCobraE

func (c *HTTPBasicLogin) NewCobraE() *cobra.Command

NewCobraE returns a Cobra's corba.Command object, which reads command line and perform login action. Unlike RootCLI, it does not pass customized RunE function from parameter, since sub-command should be done quickly.

func (*HTTPBasicLogin) Viper

func (c *HTTPBasicLogin) Viper(v *viper.Viper, section string)

Viper method sets a new Viper instance to read configuration. In most case this function can be ignored. It's useful when working with unit test or setting namespace from a sub-section of viper instance. For HTTPBasicLogin command, it's called by RootCLI.AddSubcommand() method to ensure a correct, layed viper configuration structure. This method also takes a string that represents root section of subcommand.

type LoginHTTPError

type LoginHTTPError struct {
	StatusCode int
}

LoginHTTPError is an error object that returns on HTTP code error.

func (LoginHTTPError) Error

func (e LoginHTTPError) Error() string

Error method prints error message of LoginHTTPError

type RootCLI

type RootCLI struct {
	// contains filtered or unexported fields
}

RootCLI configures a root client command line interface.

func NewRootCLI

func NewRootCLI(appName string) *RootCLI

NewRootCLI creates a new command line configuration object. It takes configurations and create an cobra.Command object. RootCLI returns a root cobra.Command object with a pre-configured cobra.Command.RunE function, which handles proper cleanup operations.

func (*RootCLI) AddSubcommand

func (c *RootCLI) AddSubcommand(child SubcommandBuilder) *RootCLI

AddSubcommand methods binds a SubcommandBuilder object to RootCLI. It allows subcommand share configuration reading (via Viper) and file system abstraction via Afero. Internally, RootCLI uses a map to keep a reference of each subcommand.

func (*RootCLI) AferoFS

func (c *RootCLI) AferoFS(fs afero.Fs) *RootCLI

AferoFS method sets an instance of afero.Fs to decide the file system we want to write to. In most cases we can just leave it unset as it points to an afero.OsFs object for all regular filesystem based calls. This API is useful for unit test, which allows we set memory file system.

func (*RootCLI) NewCobraE

func (c *RootCLI) NewCobraE(run CobraRunEFunc) (*cobra.Command, CleanupFunc)

NewCobraE methods creates a Cobra's cobra.Command object that represents root command line interface. It takes function object to fill cobra's RunE field. If there's no speical step to process, pass nil.

Besides returning cobra.Command, this function also returns a cleanup function, which should be called before program exits. For RootCLI case, it closes log files and configuration file properly.

The reason we introduce this cleanup function is because cobra does not offer a good way to handle it properly. By the time the code is written (2021-10), Cobra 1.2.1 offers only PersisentPreRunE hook to allow subcommands run a hook from root. However, this does not work for our scenario because the corresponding API, PersistentPostRunE, is not invoked on completion of subcommand. Thus, it's necessary to wrap the calling sequence to ensure config files or logs are properly closed without losing data.

func (*RootCLI) SetLocalViperPolicy

func (c *RootCLI) SetLocalViperPolicy() *RootCLI

SetLocalViperPolicy method sets default viper configuration search file name and path. This API uses os.UserConfigDir() to get XDG compatible path. If it's working on a non-supported OS, it will fallback to a non-standard ~/.appName/config.yml

func (*RootCLI) Viper

func (c *RootCLI) Viper(v *viper.Viper) *RootCLI

Viper method sets a new Viper instance to read configuration. In most case this function can be ignored. It's useful when working with unit test or configure a remote readable configuration setting.

type SubcommandBuilder

type SubcommandBuilder interface {
	Name() string
	Viper(*viper.Viper, string)
	AferoFS(afero.Fs)
	NewCobraE() *cobra.Command
}

SubcommandBuilder provides a interface that all subcommand builders needs to provide. They are used by RootCLI to configure subcommand creation.

Jump to

Keyboard shortcuts

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