util

package
Version: v2.5.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2017 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ExitCodeUsageDisplayed is returned when some usage info / help page was displayed. Unsure whether it should == E_SUCCESS or not
	ExitCodeUsageDisplayed ExitCode = 0
	// ExitCodeSuccess is used to say everything went well
	ExitCodeSuccess = 0
	// ExitCodeTrappedInterrupt is the exit code returned when an unexpected interrupt like SIGUSR1 was trapped
	ExitCodeTrappedInterrupt = -1
	// ExitCodeClientBug is the exit code returned when bytemark-client knows it's faulty
	ExitCodeClientBug = 1
	// ExitCodeCantReadConfig is the exit code returned when we couldn't read a config variable from the disk for some reason
	ExitCodeCantReadConfig = 3
	// ExitCodeCantWriteConfig is the exit code returned when we couldn't write a config variable to the disk for some reason
	ExitCodeCantWriteConfig = 4
	// ExitCodeUserExit is the exit code returned when the user's action caused the program to terminate (usually by saying no to a prompt)
	ExitCodeUserExit = 5
	// ExitCodeWontDeletePopulated is the exit code returned when the user's requested that a group be deleted when it still had servers in
	ExitCodeWontDeletePopulated = 6
	// ExitCodeBadInput is the exit code returned when the user entered a malformed command, name, or flag.
	ExitCodeBadInput = 7
	// ExitCodeSubprocessFailed is the exit code returned when the client attempted to run a subprocess (e.g. ssh, a browser or a vpn client) but couldn't
	ExitCodeSubprocessFailed = 8

	// ExitCodeNoDefaultAccount is the exit code returned when the client couldn't determine a default account. In this situation, the user should manually specify the account to use with the --account flag or using `bytemark config set account`
	ExitCodeNoDefaultAccount = 9

	// ExitCodeUnknownError is the exit code returned when we got an error we couldn't deal with.
	ExitCodeUnknownError = 49

	// ExitCodeCantConnectAuth is the exit code returned when we were unable to establish an HTTP connection to the auth endpoint.
	ExitCodeCantConnectAuth = 50
	// ExitCodeCantConnectAPI is the exit code returned when we were unable to establish an HTTP connection to the API endpoint.
	ExitCodeCantConnectAPI = 150

	// ExitCodeAuthInternalError is the exit code returned when the auth server reported an internal error.
	ExitCodeAuthInternalError = 51
	// ExitCodeAPIInternalError is the exit code returned when the API server reported an internal error.
	ExitCodeAPIInternalError = 152

	// ExitCodeCantParseAuthResponse is the exit code returned when the auth server returned something we were unable to parse.
	ExitCodeCantParseAuthResponse = 52
	// ExitCodeCantParseAPIResponse is the exit code returned when the API server returned something we were unable to parse.
	ExitCodeCantParseAPIResponse = 152

	// ExitCodeInvalidCredentials is the exit code returned when the auth server says your credentials contain invalid characters.
	ExitCodeInvalidCredentials = 53
	// ExitCodeBadCredentials is the exit code returned when the auth server says your credentials don't match a user in its database.
	ExitCodeBadCredentials = 54

	// ExitCodeActionNotPermitted is the exit code returned when the API server says you haven't got permission to do that.
	ExitCodeActionNotPermitted = 155

	// ExitCodeNotFound is the exit code returned when the API server says you do not have permission to see the object you are trying to view, or that it does not exist.
	ExitCodeNotFound = 156

	// ExitCodeBadRequest is the exit code returned when we send a bad request to API. (E.g. names being too short or having wrong characters in)
	ExitCodeBadRequest = 157

	// ExitCodeUnknownAuthError is the exit code returned when we get an unexpected error from the auth server.
	ExitCodeUnknownAuthError = 149
	// ExitCodeUnknownAPIError is the exit code returned when we get an unexpected error from the Bytemark API.
	ExitCodeUnknownAPIError = 249
)
View Source
const (
	// FormFieldOwnerName is the index of the account owner's username field in the signup form
	FormFieldOwnerName = iota
	// FormFieldOwnerPassword is the index of the account owner's password field in the signup form
	FormFieldOwnerPassword
	// FormFieldOwnerPasswordConfirmation is the index of the account owner's password confirmation field in the signup form
	FormFieldOwnerPasswordConfirmation
	// FormFieldOwnerEmail is the index of the account owner's email address field in the signup form
	FormFieldOwnerEmail
	// FormFieldOwnerFirstName is the index of the account owner's first name field in the signup form
	FormFieldOwnerFirstName
	// FormFieldOwnerLastName is the index of the account owner's last name field in the signup form
	FormFieldOwnerLastName
	// FormFieldOwnerAddress is the index of the account owner's address field in the signup form
	FormFieldOwnerAddress
	// FormFieldOwnerCity is the index of the account owner's city field in the signup form
	FormFieldOwnerCity
	// FormFieldOwnerPostcode is the index of the account owner's postcode field in the signup form
	FormFieldOwnerPostcode
	// FormFieldOwnerCountryCode is the index of the account owner's country code field in the signup form
	FormFieldOwnerCountryCode
	// FormFieldOwnerPhoneNumber is the index of the account owner's phone number field in the signup form
	FormFieldOwnerPhoneNumber
	// FormFieldOwnerMobileNumber is the index of the account owner's mobile number field in the signup form
	FormFieldOwnerMobileNumber
	// FormFieldOwnerOrgName is the index of the account owner's organisation name field in the signup form
	FormFieldOwnerOrgName
	// FormFieldOwnerOrgDivision is the index of the account owner's organisation division field in the signup form
	FormFieldOwnerOrgDivision
	// FormFieldOwnerOrgVATNumber is the index of the account owner's organisation VAT code field in the signup form
	FormFieldOwnerOrgVATNumber
	// FormFieldCreditCardNumber is the index of the credit card number field in the signup form
	FormFieldCreditCardNumber
	// FormFieldCreditCardName is the index of the credit card full name field in the signup form
	FormFieldCreditCardName
	// FormFieldCreditCardExpiry is the index of the credit card expiry field in the signup form
	FormFieldCreditCardExpiry
	// FormFieldCreditCardCVV is the index of the credit card cvv field in the signup form
	FormFieldCreditCardCVV
)
View Source
const DefaultSessionValidity = 1800

DefaultSessionValidity is the default for the --session-validity flag

Variables

This section is empty.

Functions

func CallBrowser

func CallBrowser(url string) error

CallBrowser opens the user's desktop browser to the given URL. It tries really hard - first trying open on mac or xdg-open on other systems. If xdg-open couldn't be used, it attempts to use /usr/bin/x-www-browser

func GeneratePassword

func GeneratePassword() (pass string)

GeneratePassword generates a random 16-character password made entirely of letters.

func IsConfigVar added in v0.7.0

func IsConfigVar(name string) bool

IsConfigVar checks to see if the named variable is actually one of the settable configVars.

func Luhn added in v0.7.0

func Luhn(number string) (int, error)

Luhn calculates the Luhn checksum for the given number

func MakeSignupForm added in v0.7.0

func MakeSignupForm(creditCardForm bool) (fields map[int]form.Field, f *form.Form, signup *bool)

MakeSignupForm constructs the singup form, returning a may of all the fields, the form itself and a pointer to a bool that will be true when the user has requested to sign up, and false otherwise. This is so that once the signup form exits you know whether to continue or not.

func ParseDiscSpec

func ParseDiscSpec(spec string) (*brain.Disc, error)

ParseDiscSpec reads the given string and attempts to interpret it as a disc spec.

func Prompt

func Prompt(prompt string) string

Prompt provides a string prompt, returns the entered string with no whitespace

func PromptValidate added in v0.7.0

func PromptValidate(prompt string, valid func(string) bool) (input string)

PromptValidate prompts for input, validates it. Repeats until the input is actually valid. Returns the valid input.

func PromptYesNo

func PromptYesNo(prompt string) bool

PromptYesNo provides a y/n prompt. Returns true if the user enters y, false otherwise.

func Promptf added in v0.7.0

func Promptf(promptFormat string, values ...interface{}) string

Promptf formats its arguments with fmt.Sprintf, prompts for input and then returns it.

func PromptfValidate added in v0.7.0

func PromptfValidate(valid func(string) (bool, string), prompt string, values ...interface{}) (input string)

PromptfValidate uses prompt as a format string, values as the values for the prompt, then prompts for input. The input is then validated using the validation function, and if the input is invalid, it repeats the prompting. Returns the valid input once valid input is put in.

func ValidCreditCard added in v0.7.0

func ValidCreditCard(input string) (bool, string)

ValidCreditCard is a credit-card-looking bunch of numbers. Doesn't check the check digit.

func ValidEmail added in v0.7.0

func ValidEmail(input string) (bool, string)

ValidEmail checks that the input looks vaguely like an email address. It's very loose, relies on better validation elsewhere.

func ValidExpiry added in v0.7.0

func ValidExpiry(input string) (bool, string)

ValidExpiry checks that the input is a valid credit card expiry, written in MMYY format.

func ValidName added in v0.7.0

func ValidName(input string) (bool, string)

ValidName checks to see that the input looks like a name. Names can't have spaces in, that's all I know.

Types

type CannotLoadDefinitionsError

type CannotLoadDefinitionsError struct {
	Err error
}

CannotLoadDefinitionsError is unused. Planned to be used if bytemark-client starts caching definitions, but it doesn't at the moment.

func (*CannotLoadDefinitionsError) Error

type Config

type Config struct {
	Dir         string
	Memo        map[string]ConfigVar
	Definitions map[string]string
	// contains filtered or unexported fields
}

A Config determines the configuration of the Bytemark client. It's responsible for handling things like the credentials to use and what endpoints to talk to.

Each configuration item is read from the following places, falling back to successive places:

Per-command command-line flags, global command-line flags, environment variables, configuration directory, hard-coded defaults

The location of the configuration directory is read from global command-line flags, or is otherwise ~/.bytemark

func NewConfig

func NewConfig(configDir string) (config *Config, err error)

NewConfig sets up a new config struct. Pass in an empty string to default to ~/.bytemark

func (*Config) ConfigDir

func (config *Config) ConfigDir() string

ConfigDir returns the path of the directory used to read config.

func (*Config) EndpointName

func (config *Config) EndpointName() string

EndpointName trims the URL scheme off the beginning of the endpoint. TODO(telyn): Why?

func (*Config) Get

func (config *Config) Get(name string) (string, error)

Get returns the value of a ConfigVar. Used to simplify code when the source is unnecessary.

func (*Config) GetAll

func (config *Config) GetAll() (vars []ConfigVar, err error)

GetAll returns all of the available ConfigVars in the Config.

func (*Config) GetBool

func (config *Config) GetBool(name string) (bool, error)

GetBool returns the given configvar as a bool - true if it is set, not blank, and not equal to "false". false otherwise.

func (*Config) GetDebugLevel

func (config *Config) GetDebugLevel() int

GetDebugLevel returns the current debug-level as an integer. This is used throughout the github.com/BytemarkHosting/bytemark-client library to determine verbosity of output.

func (*Config) GetDefault

func (config *Config) GetDefault(name string) ConfigVar

GetDefault returns the default ConfigVar for the given key.

func (*Config) GetGroup

func (config *Config) GetGroup() (group *lib.GroupName)

GetGroup returns a GroupName with the config's default group and account

func (*Config) GetIgnoreErr

func (config *Config) GetIgnoreErr(name string) string

GetIgnoreErr returns the value of a ConfigVar or an empty string , if it was unable to read it for whatever reason.

func (*Config) GetPath

func (config *Config) GetPath(name string) string

GetPath joins the given string onto the end of the Config.Dir path

func (*Config) GetSessionValidity

func (config *Config) GetSessionValidity() (validity int, err error)

GetSessionValidity returns the configured session validity or the default, if the configured one is not a valid int between 0 and infinity

func (*Config) GetV

func (config *Config) GetV(name string) (ConfigVar, error)

GetV returns the ConfigVar for the given key.

func (*Config) GetVirtualMachine

func (config *Config) GetVirtualMachine() (vm *lib.VirtualMachineName)

GetVirtualMachine returns a VirtualMachineName with the config's default group and account set, and a blank VirtualMachine field

func (*Config) ImportFlags

func (config *Config) ImportFlags(flags *flag.FlagSet) []string

ImportFlags reads all the flags from the passed FlagSet that have the same name as a valid configVar, and sets the configVar to that.

func (*Config) PanelURL

func (config *Config) PanelURL() string

PanelURL returns config's best guess at the correct URL for the bytemark panel for the cluster with the endpoint we're using. Basically it flips between panel.bytemark and panel-int.

func (*Config) Set

func (config *Config) Set(name, value, source string)

Set stores the given key-value pair in config's Memo. This storage does not persist once the program terminates.

func (*Config) SetPersistent

func (config *Config) SetPersistent(name, value, source string) error

SetPersistent writes a file to the config directory for the given key-value pair.

func (*Config) Unset

func (config *Config) Unset(name string) (err error)

Unset removes the named key from both config's Memo and the user's config directory.

type ConfigDirInvalidError

type ConfigDirInvalidError struct {
	Path string
}

ConfigDirInvalidError is returned when the path specified as the config dir was not a directory.

func (*ConfigDirInvalidError) Error

func (e *ConfigDirInvalidError) Error() string

type ConfigManager

type ConfigManager interface {
	Get(string) (string, error)
	GetIgnoreErr(string) string
	GetBool(string) (bool, error)
	GetV(string) (ConfigVar, error)
	GetSessionValidity() (int, error)
	GetVirtualMachine() *lib.VirtualMachineName
	GetGroup() *lib.GroupName
	GetAll() ([]ConfigVar, error)
	Set(string, string, string)
	SetPersistent(string, string, string) error
	Unset(string) error
	GetDebugLevel() int
	EndpointName() string
	PanelURL() string
	ConfigDir() string

	ImportFlags(*flag.FlagSet) []string
}

ConfigManager is an interface defining a key->value store that also knows where the values were set from.

type ConfigReadError

type ConfigReadError struct {
	Name string
	Path string
	Err  error
}

ConfigReadError is returned when a file containing a value for a configVar couldn't be read.

func (*ConfigReadError) Error

func (e *ConfigReadError) Error() string

type ConfigVar

type ConfigVar struct {
	Name   string
	Value  string
	Source string
}

ConfigVar is a struct which contains a name-value-source triplet Source is up to two words separated by a space. The first word is the source type: FLAG, ENV, DIR, CODE. The second is the name of the flag/file/environment var used.

func (*ConfigVar) SourceBaseName

func (v *ConfigVar) SourceBaseName() string

SourceBaseName returns the basename of the configVar's source. it's a bit stupid and so its output is only valid for configVars with SourceType() of DIR

func (*ConfigVar) SourceType

func (v *ConfigVar) SourceType() string

SourceType returns one of the following: FLAG for a configVar whose value was set by passing a flag on the command line ENV for a configVar whose value was set from an environment variable DIR for a configVar whose value was set from a file in the config dir

type ConfigWriteError

type ConfigWriteError struct {
	Name string
	Path string
	Err  error
}

ConfigWriteError is returned when a file containing a value for a configVar couldn't be written to.

func (*ConfigWriteError) Error

func (e *ConfigWriteError) Error() string

type DiscSpecError

type DiscSpecError struct {
	Position  int
	Character rune
}

DiscSpecError represents an error during parse.

func (*DiscSpecError) Error

func (e *DiscSpecError) Error() string

type DiscSpecFlag

type DiscSpecFlag []brain.Disc

DiscSpecFlag is a flag which reads its argument as a disc spec. It can be specified multiple times to add multiple discs.

func (*DiscSpecFlag) Set

func (discsFlag *DiscSpecFlag) Set(value string) error

Set adds all the defined discs to this flag's value

func (*DiscSpecFlag) String

func (discsFlag *DiscSpecFlag) String() string

type ExitCode

type ExitCode int

ExitCode is a named type for the E_* constants which are used as exit codes.

func HelpForExitCodes

func HelpForExitCodes() ExitCode

HelpForExitCodes prints readable information on what the various exit codes do.

func ProcessError

func ProcessError(err error, message ...string) ExitCode

ProcessError processes the given error, outputs a message, and returns the relevant ExitCode for the given error.

type FileFlag

type FileFlag struct {
	FileName string
	Value    string
}

FileFlag implements the flag.Value (aka urfave/cli.Generic) interface to provide a flag value type that reads its effective value from the file named as its input.

func (*FileFlag) Set

func (f *FileFlag) Set(name string) error

Set sets the value of FileFlag given the filename as an argument. This reads in the file synchronously.

func (*FileFlag) String

func (f *FileFlag) String() string

type IPFlag

type IPFlag []net.IP

IPFlag is a flag.Value used to provide an array of net.IPs

func (*IPFlag) Set

func (ips *IPFlag) Set(value string) error

Set sets the IPFlag given the space-seperated string of IPs

func (*IPFlag) String

func (ips *IPFlag) String() string

type InvalidConfigVarError

type InvalidConfigVarError struct {
	ConfigVar string
}

InvalidConfigVarError is used to inform the user that they variable they attempted to set / get doesn't exist

func (InvalidConfigVarError) Error

func (e InvalidConfigVarError) Error() string

type SizeSpecFlag

type SizeSpecFlag int

SizeSpecFlag represents a capacity as an integer number of megabytes.

func (*SizeSpecFlag) Set

func (ssf *SizeSpecFlag) Set(spec string) error

Set sets the value to the size specified. Users can add "M" or "G" as a suffix to specify that they are talking about megabytes/gigabytes. Gigabytes are assumed by default.

func (*SizeSpecFlag) String

func (ssf *SizeSpecFlag) String() string

type SubprocessFailedError

type SubprocessFailedError struct {
	Args     []string
	ExitCode int
	Err      error
}

SubprocessFailedError is returned when a process run by bytemark-client (e.g. open/xdg-open to open a browser) failed

func (SubprocessFailedError) Error

func (e SubprocessFailedError) Error() string

type UsageDisplayedError

type UsageDisplayedError struct {
	TheProblem string
	Command    string
}

UsageDisplayedError is returned by commands when the user entered wrong info and the help was output

func (UsageDisplayedError) Error

func (e UsageDisplayedError) Error() string

type UserRequestedExit

type UserRequestedExit struct{}

UserRequestedExit is returned when the user said 'No' to a 'yes/no' prompt.

func (UserRequestedExit) Error

func (e UserRequestedExit) Error() string

type WontDeleteNonEmptyGroupError

type WontDeleteNonEmptyGroupError struct {
	Group *lib.GroupName
}

WontDeleteNonEmptyGroupError is returned when 'delete group' was called on a group with stuff in, without --recursive being specified

func (WontDeleteNonEmptyGroupError) Error

Directories

Path Synopsis
Package sizespec implements a parser for size specifications.
Package sizespec implements a parser for size specifications.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL