cmd_toolkit

package module
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: MIT Imports: 18 Imported by: 0

README

cmd-toolkit

GoDoc Reference go.mod LICENSE

Release Code Coverage Report Go Report Card Build Status

Basic code for windows command line tools; take a look at the GoDoc reference.

Documentation

Index

Constants

View Source
const (
	StdFilePermissions = 0o644 // -rw-r--r--
	StdDirPermissions  = 0o755 // -rwxr-xr-x
)
View Source
const ApplicationDataEnvVarName = "APPDATA"

ApplicationDataEnvVarName is the name of the environment variable used to read/write application-specific files that are intended to have some permanence.

Variables

View Source
var ProductionLogger *simpleLogger = &simpleLogger{
	exitFunction:    os.Exit,
	currentLogLevel: defaultLoggingLevel,
	lock:            &sync.RWMutex{},
}

Functions

func AddCommandData

func AddCommandData(name string, d *CommandDescription)

func AppName

func AppName() (string, error)

AppName retrieves the name of the application

func ApplicationPath

func ApplicationPath() string

ApplicationPath returns the path to application-specific data (%APPDATA%\appname)

func AssignFileSystem added in v0.10.0

func AssignFileSystem(fs afero.Fs) afero.Fs

AssignFileSystem sets the current afero.Fs instance and returns the original pre-assignment value

func BuildDependencies

func BuildDependencies() []string

BuildDependencies returns information about the dependencies used to compile the program

func BuildInformationHeader added in v0.8.0

func BuildInformationHeader() string

https://github.com/majohn-r/cmd-toolkit/issues/17

func CopyFile

func CopyFile(src, dest string) (err error)

CopyFile copies a file. Adapted from https://github.com/cleversoap/go-cp/blob/master/cp.go

func Copyright(o output.Bus, first int, timestamp, owner string) string

https://github.com/majohn-r/cmd-toolkit/issues/17

func CreateAppSpecificPath

func CreateAppSpecificPath(topDir string) (string, error)

CreateAppSpecificPath creates a path string for an app-related directory

func CreateFile

func CreateFile(fileName string, content []byte) error

CreateFile creates a file; it returns an error if the file already exists

func CreateFileInDirectory

func CreateFileInDirectory(dir, name string, content []byte) error

CreateFileInDirectory creates a file in a specified directory. It returns an error if the file already exists

func DecorateBoolFlagUsage

func DecorateBoolFlagUsage(usage string, defaultValue bool) string

DecorateBoolFlagUsage appends a default value to the provided usage if the default value is false. This is a work-around for the flag package's defaultUsage function, which displays each flag's usage, along with its default value - but it only includes the default value if the default value is not the zero value for the flag's type.

func DecorateIntFlagUsage

func DecorateIntFlagUsage(usage string, defaultValue int) string

DecorateIntFlagUsage appends a default value to the provided usage if the default value is 0. This is a work-around for the flag package's defaultUsage function, which displays each flag's usage, along with its default value - but it only includes the default value if the default value is not the zero value for the flag's type.

func DecorateStringFlagUsage

func DecorateStringFlagUsage(usage, defaultValue string) string

DecorateStringFlagUsage appends a default value to the provided usage if the default value is the empty string. This is a work-around for the flag package's defaultUsage function, which displays each flag's usage, along with its default value - but it only includes the default value if the default value is not the zero value for the flag's type.

func DecoratedAppName added in v0.8.0

func DecoratedAppName(applicationName, applicationVersion, timestamp string) string

https://github.com/majohn-r/cmd-toolkit/issues/17

func DefaultConfigFileName

func DefaultConfigFileName() string

DefaultConfigFileName retrieves the name of the configuration file that contains defaults for the commands

func DereferenceEnvVar

func DereferenceEnvVar(s string) (string, error)

DereferenceEnvVar scans a string for environment variable references, looks up the values of those environment variables, and replaces the references with their values. If one or more of the referenced environment variables are undefined, they are all reported in the error return

func DirExists

func DirExists(path string) bool

DirExists returns whether the specified file exists as a directory

func Execute

func Execute(o output.Bus, firstYear int, appName, appVersion, buildTimestamp string, cmdLine []string) (exitCode int)

func FileSystem added in v0.10.0

func FileSystem() afero.Fs

FileSystem returns the current afero.Fs instance

func FlagIndicator added in v0.4.0

func FlagIndicator() string

FlagIndicator returns the current flag indicator, typically "-" or "--"

func FlowerBox added in v0.8.0

func FlowerBox(lines []string) []string

https://github.com/majohn-r/cmd-toolkit/issues/17

func FormatBuildDependencies added in v0.8.0

func FormatBuildDependencies(dependencies []string) []string

https://github.com/majohn-r/cmd-toolkit/issues/17

func FormatGoVersion added in v0.8.0

func FormatGoVersion(version string) string

https://github.com/majohn-r/cmd-toolkit/issues/17

func GenerateAboutContent added in v0.5.0

func GenerateAboutContent(o output.Bus)

GenerateAboutContent writes 'about' content in a pretty format

func GoVersion

func GoVersion() string

GoVersion returns the version of Go used to compile the program

func InitApplicationPath

func InitApplicationPath(o output.Bus) bool

InitApplicationPath ensures that the application path exists

func InitBuildData

func InitBuildData(version, creation string)

InitBuildData captures information about how the program was compiled, the version of the program, and the timestamp for when the program was built.

func InitLogging

func InitLogging(o output.Bus) (ok bool)

InitLogging sets up logging at the default log level

func InitLoggingWithLevel added in v0.2.0

func InitLoggingWithLevel(o output.Bus, l output.Level) (ok bool)

InitLoggingWithLevel initializes logging with a specific log level

func InterpretBuildData added in v0.8.0

func InterpretBuildData() (v string, dependencies []string)

https://github.com/majohn-r/cmd-toolkit/issues/17

func LogCommandStart

func LogCommandStart(o output.Bus, name string, m map[string]any)

LogCommandStart logs the beginning of command execution; if the provided map contains a "command" value, then the name parameter is ignored

func LogFileDeletionFailure

func LogFileDeletionFailure(o output.Bus, s string, e error)

LogFileDeletionFailure logs errors when a file cannot be deleted; does not write anything to the error output because that typically needs additional context

func LogUnreadableDirectory

func LogUnreadableDirectory(o output.Bus, s string, e error)

LogUnreadableDirectory logs errors when a directory cannot be read; does not write anything to the error output because that typically needs additional context

func Mkdir

func Mkdir(dir string) error

Mkdir makes the specified directory; succeeds if the directory already exists. Fails if a plain file exists with the specified path.

func PlainFileExists

func PlainFileExists(path string) bool

PlainFileExists returns whether the specified file exists as a plain file (i.e., not a directory)

func ProcessArgs

func ProcessArgs(o output.Bus, f *flag.FlagSet, rawArgs []string) (ok bool)

ProcessArgs processes a slice of command line arguments and handles common errors therein

func ReadDirectory

func ReadDirectory(o output.Bus, dir string) (files []fs.FileInfo, ok bool)

ReadDirectory returns the contents of a specified directory

func ReportDirectoryCreationFailure

func ReportDirectoryCreationFailure(o output.Bus, cmd, dir string, e error)

ReportDirectoryCreationFailure reports an error creating a directory to error output and to the log

func ReportFileCreationFailure

func ReportFileCreationFailure(o output.Bus, cmd, file string, e error)

ReportFileCreationFailure reports an error creating a file to error output and to the log

func ReportFileDeletionFailure

func ReportFileDeletionFailure(o output.Bus, file string, e error)

ReportFileDeletionFailure reports an error deleting a file to error output and to the log

func ReportInvalidConfigurationData

func ReportInvalidConfigurationData(o output.Bus, s string, e error)

ReportInvalidConfigurationData handles errors found when attempting to parse a YAML configuration file, both logging the error and notifying the user of the error

func ReportNothingToDo

func ReportNothingToDo(o output.Bus, cmd string, fields map[string]any)

ReportNothingToDo reports a user error in which a command's parameter values prevent the command from doing any work; the report is made to error output and to the log

func SecureAbsolutePath

func SecureAbsolutePath(path string) string

SecureAbsolutePath returns a path's absolute value

func SetAppName

func SetAppName(s string) error

SetAppName sets the name of the application, returning an error if the name has already been set to a different value or if it is being set to an empty string

func SetApplicationPath

func SetApplicationPath(s string) (previous string)

SetApplicationPath is used to set applicationPath to a known value; intent is for use in tesing scenarios

func SetAuthor

func SetAuthor(s string)

SetAuthor is used to override the default value of author, in case someone besides me decides to use this library

func SetDefaultConfigFileName

func SetDefaultConfigFileName(s string)

SetDefaultConfigFileName sets the name of the configuration file that contains defaults for the commands

func SetFirstYear

func SetFirstYear(i int)

SetFirstYear sets the first year of application development

func SetFlagIndicator added in v0.4.0

func SetFlagIndicator(val string)

SetFlagIndicator sets the flag indicator to the specified value

func WriteDirectoryCreationError

func WriteDirectoryCreationError(o output.Bus, d string, e error)

WriteDirectoryCreationError writes a suitable error message to the user when a directory cannot be created

Types

type CommandDescription

type CommandDescription struct {
	IsDefault   bool
	Initializer func(output.Bus, *Configuration, *flag.FlagSet) (CommandProcessor, bool)
}

type CommandProcessor

type CommandProcessor interface {
	Exec(output.Bus, []string) bool
}

CommandProcessor defines the functions needed to run a command

func ProcessCommand

func ProcessCommand(o output.Bus, args []string) (cmd CommandProcessor, cmdArgs []string, ok bool)

ProcessCommand selects which command to be run and returns the relevant CommandProcessor, command line arguments and ok status

type Configuration

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

Configuration defines the data structure for configuration information.

func EmptyConfiguration

func EmptyConfiguration() *Configuration

EmptyConfiguration creates an empty Configuration instance

func NewConfiguration

func NewConfiguration(o output.Bus, data map[string]any) *Configuration

NewConfiguration returns a Configuration instance populated as specified by the data parameter

func ReadConfigurationFile

func ReadConfigurationFile(o output.Bus) (c *Configuration, ok bool)

ReadConfigurationFile reads defaults.yaml from the specified path and returns a pointer to a cooked Configuration instance; if there is no such file, then an empty Configuration is returned and ok is true

func (*Configuration) BoolDefault

func (c *Configuration) BoolDefault(key string, defaultValue bool) (b bool, err error)

BoolDefault returns a boolean value for a specified key

func (*Configuration) BooleanValue

func (c *Configuration) BooleanValue(key string) (value, ok bool)

BooleanValue returns a boolean value and whether it exists

func (*Configuration) HasSubConfiguration

func (c *Configuration) HasSubConfiguration(key string) bool

HasSubConfiguration returns whether the specified subConfiguration exists

func (*Configuration) IntDefault

func (c *Configuration) IntDefault(key string, b *IntBounds) (i int, err error)

IntDefault returns a default value for a specified key, which may or may not be defined in the Configuration instance

func (*Configuration) IntValue

func (c *Configuration) IntValue(key string) (value int, ok bool)

IntValue returns an int value and whether it exists

func (*Configuration) String

func (c *Configuration) String() string

func (*Configuration) StringDefault

func (c *Configuration) StringDefault(key, defaultValue string) (s string, err error)

StringDefault returns a string value for a specified key

func (*Configuration) StringValue

func (c *Configuration) StringValue(key string) (value string, ok bool)

StringValue returns the definition of the specified key and ok if the value is defined

func (*Configuration) SubConfiguration

func (c *Configuration) SubConfiguration(key string) *Configuration

SubConfiguration returns a specified sub-configuration

type EnvVarMemento

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

EnvVarMemento preserves a memento of an environment variable's state

func NewEnvVarMemento

func NewEnvVarMemento(name string) *EnvVarMemento

NewEnvVarMemento reads a specified environment variable and returns a memento of its state

func (*EnvVarMemento) Restore

func (mem *EnvVarMemento) Restore()

Restore resets a saved environment variable to its original state

type IntBounds

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

IntBounds holds the bounds for an int value which has a minimum value, a maximum value, and a default that lies within those bounds

func NewIntBounds

func NewIntBounds(v1, v2, v3 int) *IntBounds

NewIntBounds creates a instance of IntBounds, sorting the provided value into reasonable fields

func (*IntBounds) Default added in v0.6.0

func (b *IntBounds) Default() int

Default returns the default value for a bounded int

func (*IntBounds) Maximum added in v0.6.0

func (b *IntBounds) Maximum() int

Maximum returns the maximum value for a bounded int

func (*IntBounds) Minimum added in v0.6.0

func (b *IntBounds) Minimum() int

Minimum returns the minimum value for a bounded int

Directories

Path Synopsis
build module

Jump to

Keyboard shortcuts

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