runtime

package
v0.0.0-...-cb74382 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2017 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTerminalNotInitialized is returned when any of the Terminal API is called without calling Initialize() first
	ErrTerminalNotInitialized = errors.New("Terminal is not initialized. Maybe you haven't called the Initialize() method first")
	// ErrUnknownCommand is returned when user enters a subcommand that is not implemented
	ErrUnknownCommand = errors.New("Unknown command. Type 'help' to see the list of available commands")
	// ErrNilConfig is returned when config passed to Run() is nil
	ErrNilConfig = errors.New("Config cannot be nil")
)

Functions

func GetCommandMap

func GetCommandMap() map[string]Command

func RegisterCommand

func RegisterCommand(cmd Command)

RegisterCommand adds the command to the list of invokable subcommands. Your subcommand is available to the user only if you have Registered the command using this API.

Best place to register would be to define an init function in your command struct and call this API from it.

func init() {
	cli.RegisterCommand(&MySubCommand{})
}

Types

type Command

type Command interface {
	Name() string
	Run(runtime Runtime, args []string) (exitProgram bool, err error)
	Synopsis() string
	Help() string
}

Command represents a runnable subcommand e.g. if you are implementing a git command line tool and you intend to invoke it as "git init", "git clone <args>" etc, then init and clone represents two Commands for your program

To create a new sub command, implement this interface and have an init() method that calls

cli.RegisterCommand(&MyCommand{})

type Config

type Config interface {
	AppName() string
	SetPrompt(prompt string)
	Prompt() string
	VersionNumber() string
	VersionTag() string
	Get(key string) (val interface{}, ok bool)
	Set(key string, val interface{})
	SetTheme(theme Theme)
	Theme() Theme
}

Config represents the basic configuration parameters that change the runtime behavior of the application.

func GetDefaultConfig

func GetDefaultConfig(appName, versionNumber, versionTag string) Config

GetDefaultConfig returns an implementation of Config interface where the prompt is set to the binary name.

type Runtime

type Runtime interface {
	Term() Terminal
	OSArgument() []string
	CommandArgs() []string
	GetAllCommandNames() []string
	GetCommand(name string) Command
	RunCommand(name string, args []string) (exitProgram bool, err error)
	Config() Config
	Store() map[string]interface{}

	Trace(format string, a ...interface{})
	Info(format string, a ...interface{})
	Warn(format string, a ...interface{})
	Error(format string, a ...interface{})
}

Runtime provides a set of helper APIs that makes the life easy for a developer.

Trace(), Info(), Warn() and Error() provide out of the box color coded debug statements to the console.

Term() object provides even low level APIs like ReadLine(), ReadPassword() and ReadAnswer()

OSArgument() provides you the entire os.Args fields that was received

CommandArgs is the part without the sub command name and sub command arguments. For example, if the program was invoked as

$mycli -a arg1 -b arg2 myCommand -x commangArg1 -y commandArg2 -z commandArg3

CommandArgs() will return []{"-a", "arg1", "-b", "arg2"}

GetCommand returns a Command object and is useful if you want to invoke another subcommand from within a subcommand's Run()

func GetDefaultRuntime

func GetDefaultRuntime(term Terminal, osArguments []string, commandArguments []string, config Config, store map[string]interface{}) Runtime

GetDefaultRuntime provides a default implementation of Runtime interface

type Terminal

type Terminal interface {
	Initialize(prompt string) error
	SetPrompt(prompt string)

	PrintBuf([]byte) (int, error)
	Printf(format string, a ...interface{}) (int, error)
	Println(a ...interface{}) (int, error)

	ReadLine() (string, error)
	ReadPassword(prompt string) (password string, err error)
	ReadAnswer(question string) (answer string, err error)

	TearDown() error
}

Terminal is a representation of an actual command line terminal. Users should Initialize() before making any API calls. TearDown() should be called before the program exits

func NewTerminal

func NewTerminal() Terminal

NewTerminal returns a default implementation of Terminal

type Theme

type Theme struct {
	PromptColor color.Color
	PromptStyle color.Style

	TraceColor color.Color
	TraceStyle color.Style

	InfoColor color.Color
	InfoStyle color.Style

	WarnColor color.Color
	WarnStyle color.Style

	ErrorColor color.Color
	ErrorStyle color.Style
}

func DefaultTheme

func DefaultTheme() Theme

Jump to

Keyboard shortcuts

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