ctl

package
v0.8.4 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2021 License: Apache-2.0 Imports: 9 Imported by: 13

Documentation

Overview

Package ctl provides common code for building a command line control app that works via https calls to a service

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AskForConfirmation added in v0.4.0

func AskForConfirmation(out io.Writer, r io.Reader, s string) (bool, error)

AskForConfirmation asks the user for confirmation. A user must type in "yes" or "no" and then press enter. It has fuzzy matching, so "y", "Y", "yes", "YES", and "Yes" all count as confirmations. If the input is not recognized, it will ask again. The function does not return until it gets a valid response from the user or if an error occurs.

func WriteJSON

func WriteJSON(out io.Writer, value interface{}) error

WriteJSON prints response to out

Types

type Action

type Action func() error

Action is a CTL action

type Application

type Application interface {
	Version(version string) Application
	Parse(args []string) (command string, err error)
	Command(name, help string) *CmdClause
	Flag(name, help string) *FlagClause
	Terminate(terminate func(int)) Application
	Writer(w io.Writer) Application
	ErrorWriter(w io.Writer) Application
	UsageWriter(w io.Writer) Application
}

Application is a proxy interface to kingpin's *Application

func NewApplication

func NewApplication(name, help string) Application

NewApplication creates a new application instance

type CmdClause

type CmdClause kp.CmdClause

CmdClause is a proxy interface to kingpin's *CmdClause

func (*CmdClause) Action

func (c *CmdClause) Action(action Action) *CmdClause

Action adds an action for this command.

func (*CmdClause) Alias

func (c *CmdClause) Alias(name string) *CmdClause

Alias adds an alias for this command.

func (*CmdClause) Command

func (c *CmdClause) Command(name, help string) *CmdClause

Command adds a new sub-command.

func (*CmdClause) Flag added in v0.4.0

func (c *CmdClause) Flag(name, help string) *FlagClause

Flag defines a new flag with the given long name and help.

func (*CmdClause) PreAction

func (c *CmdClause) PreAction(action Action) *CmdClause

PreAction adds a pre-action for this command.

type Control

type Control interface {
	App() Application
	// Reader is the source to read from, typically set to os.Stdin
	Reader() io.Reader
	// Writer is the destination for all output from the command, typically set to os.Stdout
	Writer() io.Writer
	// ErrWriter is the destinaton for errors, typically set to os.Stderr
	ErrWriter() io.Writer

	Fail(msg string, err error) error
	Parse(args []string) string
	ReturnCode() ReturnCode
}

Control is an interface for CLI

type ControlAction

type ControlAction func(c Control, flags interface{}) error

ControlAction is a wrapper over kp.Action

type ControlDefinition

type ControlDefinition struct {
	App Application
	// Stdin is the source to read from, typically set to os.Stdin
	Stdin io.Reader
	// Output is the destination for all output from the command, typically set to os.Stdout
	Output io.Writer
	// ErrOutput is the destinaton for errors.
	// If not set, errors will be written to os.StdError
	ErrOutput io.Writer
}

ControlDefinition contains the default settings for control application

type Ctl

type Ctl struct {
	Control
	// contains filtered or unexported fields
}

Ctl contains the definition and result from the parsed and initialized data It contains all the information needed by pre-action and action to perform the task

func NewControl

func NewControl(d *ControlDefinition) *Ctl

NewControl creates new Control

func (*Ctl) App

func (ctl *Ctl) App() Application

App returns current control App

func (*Ctl) ErrWriter added in v0.4.0

func (ctl *Ctl) ErrWriter() io.Writer

ErrWriter returns a writer for control output

func (*Ctl) Fail

func (ctl *Ctl) Fail(msg string, err error) error

Fail the execution and return error

func (*Ctl) Parse

func (ctl *Ctl) Parse(args []string) string

Parse will parse all the supplied args and will perform any pre-actions and actions defined on the command

func (*Ctl) Reader added in v0.4.0

func (ctl *Ctl) Reader() io.Reader

Reader is the source to read from, typically set to os.Stdin

func (*Ctl) RegisterAction

func (ctl *Ctl) RegisterAction(f ControlAction, params interface{}) Action

RegisterAction create new Control action

func (*Ctl) Reset

func (ctl *Ctl) Reset(out io.Writer, errout io.Writer)

Reset is used mostly in tests to reset the control to initial state

func (*Ctl) ReturnCode

func (ctl *Ctl) ReturnCode() ReturnCode

ReturnCode returns execution code

func (*Ctl) WithErrWriter added in v0.4.0

func (ctl *Ctl) WithErrWriter(out io.Writer) *Ctl

WithErrWriter allows to specify a custom error writer

func (*Ctl) WithReader added in v0.4.0

func (ctl *Ctl) WithReader(reader io.Reader) *Ctl

WithReader allows to specify a custom reader

func (*Ctl) WithWriter added in v0.4.0

func (ctl *Ctl) WithWriter(out io.Writer) *Ctl

WithWriter allows to specify a custom writer

func (*Ctl) Writer

func (ctl *Ctl) Writer() io.Writer

Writer returns a writer for control output

type FilesList

type FilesList []string

FilesList allows to specify files in command

func GetFilesList

func GetFilesList(s Settings) *FilesList

GetFilesList retruns list from command arguments

func (*FilesList) IsCumulative

func (d *FilesList) IsCumulative() bool

IsCumulative always returns true

func (*FilesList) Set

func (d *FilesList) Set(value string) error

Set value

func (*FilesList) String

func (d *FilesList) String() string

String returns coma separated list of files

type FlagClause

type FlagClause kp.FlagClause

FlagClause is a proxy interface to kingpin's *FlagClause

func (*FlagClause) Bool

func (f *FlagClause) Bool() (target *bool)

Bool makes this flag a boolean flag.

func (*FlagClause) Default

func (f *FlagClause) Default(values ...string) *FlagClause

Default values for this flag. They *must* be parseable by the value of the flag.

func (*FlagClause) Hidden

func (f *FlagClause) Hidden() *FlagClause

Hidden hides a flag from usage but still allows it to be used.

func (*FlagClause) Required

func (f *FlagClause) Required() *FlagClause

Required makes the flag required. You can not provide a Default() value to a Required() flag.

func (*FlagClause) Short

func (f *FlagClause) Short(name rune) *FlagClause

Short sets the short flag name.

type ReturnCode

type ReturnCode int

ReturnCode is the type that your command returns, these map to standard process return codes

const (
	// RCOkay denotes success
	RCOkay ReturnCode = 0
	// RCFailed denotes a failure in the requested command
	RCFailed ReturnCode = 1
	// RCUsage denotes that the parmaters supplied to the tool were somehow incorrect
	RCUsage ReturnCode = 64
)

type Settings

type Settings kp.Settings

Settings interface

Jump to

Keyboard shortcuts

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