target

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2018 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package target implements gnu make like target processing from yaml files and OS environment variables.

Index

Constants

View Source
const (
	// ConfigDirName is the name of the folder where ron will look for yaml config
	// files.
	ConfigDirName = ".ron"
	// ConfigFileName is the main ron config file that overrides other files.
	ConfigFileName = "ron.yaml"
)
View Source
const (
	// ExecSentinel is the first character looked for in envs to
	// signify that the value should be executed in the shell
	// and the output assigned to the key.
	ExecSentinel = "+"
)

Variables

View Source
var (
	// DefaultYamlBytes is used for the default built in binary targets
	// exported into default.go. If using as a library you can override this
	// by setting target.DefaultYamlBytes globally during startup.
	//   defaultYaml, err := config.Asset("myconfig/default.yaml")
	//   if err != nil {
	//       return err
	//   }
	//   target.DefaultYamlBytes = defaultYaml
	DefaultYamlBytes = []byte{}
)
View Source
var LoadConfigFile = func(path string) (*RawConfig, error) {
	path, err := filepath.Abs(path)
	if err != nil {
		return nil, err
	}
	f, err := fi.NewFile(path)
	if err != nil {
		return nil, err
	}
	content, err := template.RenderGo(path, f.String())
	if err != nil {
		return nil, err
	}

	var c *ConfigFile
	err = yaml.Unmarshal([]byte(content), &c)
	if err != nil {
		return nil, extractConfigError(path, content, err)
	}
	if c == nil {
		return nil, fmt.Errorf("empty file requires envs and target keys")
	}
	envs, err := yaml.Marshal(c.Envs)
	if err != nil {
		return nil, err
	}
	remotes, err := yaml.Marshal(c.Remotes)
	if err != nil {
		return nil, err
	}
	targets, err := yaml.Marshal(c.Targets)
	if err != nil {
		return nil, err
	}
	return &RawConfig{
		Envs:     string(envs),
		Filepath: path,
		Remotes:  string(remotes),
		Targets:  string(targets),
	}, nil
}

LoadConfigFile will open a given file path and return it's raw envs and targets.

Functions

func Asset

func Asset(name string) ([]byte, error)

Asset loads and returns the asset for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetDir

func AssetDir(name string) ([]string, error)

AssetDir returns the file names below a certain directory embedded in the file by go-bindata. For example if you run go-bindata on data/... and data contains the following hierarchy:

data/
  foo.txt
  img/
    a.png
    b.png

then AssetDir("data") would return []string{"foo.txt", "img"} AssetDir("data/img") would return []string{"a.png", "b.png"} AssetDir("foo.txt") and AssetDir("notexist") would return an error AssetDir("") will return []string{"data"}.

func AssetInfo

func AssetInfo(name string) (os.FileInfo, error)

AssetInfo loads and returns the asset info for the given name. It returns an error if the asset could not be found or could not be loaded.

func AssetNames

func AssetNames() []string

AssetNames returns the names of the assets.

func BuiltinDefault

func BuiltinDefault() (string, string, error)

BuiltinDefault loads the binary yaml file and returns envs, targets, and any errors.

func MustAsset

func MustAsset(name string) []byte

MustAsset is like Asset but panics when Asset would return an error. It simplifies safe initialization of global variables.

func RestoreAsset

func RestoreAsset(dir, name string) error

RestoreAsset restores an asset under the given directory

func RestoreAssets

func RestoreAssets(dir, name string) error

RestoreAssets restores an asset under the given directory recursively

Types

type ConfigFile

type ConfigFile struct {
	Envs    []map[string]string `json:"envs" yaml:"envs"`
	Remotes *Remotes            `json:"remotes" yaml:"remotes"`
	Targets map[string]struct {
		Before      []string `json:"before" yaml:"before"`
		After       []string `json:"after" yaml:"after"`
		Cmd         string   `json:"cmd" yaml:"cmd"`
		Description string   `json:"description" yaml:"description"`
	} `json:"targets" yaml:"targets"`
}

ConfigFile is used to unmarshal configuration files.

func (*ConfigFile) EnvsString

func (c *ConfigFile) EnvsString() string

EnvsString is used for debugging the loaded envs.

func (*ConfigFile) RemotesString

func (c *ConfigFile) RemotesString() string

RemotesString is used for debugging the loaded remotes.

func (*ConfigFile) TargetsString

func (c *ConfigFile) TargetsString() string

TargetsString is used for debugging the loaded targets.

type Configs

type Configs struct {
	RemoteEnv   string               // The remote hosts to run the command on. This is (file):env
	RemoteHosts []*execute.SSHConfig // a list of remote hosts to execute on.
	Files       []*File
	StdOut      io.Writer
	StdErr      io.Writer
}

Configs is a mapping of filename to target file.

func NewConfigs

func NewConfigs(configs []*RawConfig, remoteEnv string, stdOut io.Writer, stdErr io.Writer) (*Configs, error)

NewConfigs takes a default set of yaml in config format and then overrides them with a new set of config target replacements.

func (*Configs) GetEnv

func (tc *Configs) GetEnv(name string) MSS

GetEnv will return the targets associated environment variables to use when running the target.

func (*Configs) List

func (tc *Configs) List(verbose bool, fuzzy string)

List prints out each target and its before and after targets.

func (*Configs) ListClean

func (tc *Configs) ListClean()

ListClean will print out a full list of targets suitable for bash completion.

func (*Configs) ListEnvs

func (tc *Configs) ListEnvs() error

ListEnvs will print out the list of file envs.

func (*Configs) ListRemotes

func (tc *Configs) ListRemotes() error

ListRemotes will print out the list of remote envs set in each file.

func (*Configs) Target

func (tc *Configs) Target(name string) (*Target, bool)

Target retrieves the named target from config. If it doesn't exist a bool false will be returned along with nil. If the name contains a file prefix such as "default:mytarget", it will only search within that configuration file.

type Env

type Env struct {
	OSEnvs MSS       // the initial environment variables
	W      io.Writer // underlying writer
	// contains filtered or unexported fields
}

Env takes a raw yaml environment definition and expands and overrides any variables.

func NewEnv

func NewEnv(parentFile *File, config *RawConfig, osEnvs MSS, writer io.Writer) (*Env, error)

NewEnv create a new environment variable parser similar to make variables. In order to populate the Config envs, the func Process must be run after initialization.

func (*Env) Config

func (e *Env) Config() (MSS, error)

Config returns the envs config as a map[string]string. It will process each env if that has not been done already.

func (*Env) Getenv

func (e *Env) Getenv(key string) string

Getenv retrieves the value of the environment variable named by the key. It returns the value, which will be empty if the variable is not present.

func (*Env) List

func (e *Env) List() error

List prints to the underlying writer a list of the configured env based on overriden environment variables and default yaml ones.

func (*Env) MergeTo added in v1.1.3

func (e *Env) MergeTo(node *Env) error

MergeTo the current env into any missing keys for the input node.

func (*Env) PrintRaw

func (e *Env) PrintRaw() error

PrintRaw outputs the unprocessed yaml given to Env in both the defaults and overriden.

type File

type File struct {

	// Filepath is the path to the input file.
	Filepath string
	// Targets are the files targets.
	Targets map[string]*Target
	// Env are the files environment variables.
	Env *Env
	// Remotes is a mapping of environment to remote hosts.
	Remotes Remotes
	// contains filtered or unexported fields
}

File is a mapping of the config file to its parsed envs and targets.

func (*File) Basename

func (f *File) Basename() string

Basename will return the Filepath name of file without the extension.

type MSS

type MSS map[string]string

MSS is an alias for map[string]string

func ParseOSEnvs

func ParseOSEnvs(osEnvs []string) MSS

ParseOSEnvs takes a list of "key=val" and splits them into a map[string]string.

type Make

type Make struct {
	Configs *Configs
}

Make runs targets...like make kinda

func NewMake

func NewMake(configs *Configs) (*Make, error)

NewMake creates a Make type with config embedded.

func (*Make) Run

func (m *Make) Run(names ...string) error

Run executes the given target name.

type RawConfig

type RawConfig struct {
	Filepath string
	Envs     string
	Remotes  string
	Targets  string
}

RawConfig contains the raw strings from a loaded config file.

func LoadConfigFiles

func LoadConfigFiles(defaultYamlPath, overrideYamlPath string, withHomeDirectory bool) ([]*RawConfig, string, error)

LoadConfigFiles loads the default, override, and any directory config files and returns them as a slice. If defaultYamlPath is an empty string, the defaults compiled into ron will be used instead. If overrideYamlPath is blank, it will find the nearest parent folder containing a ron.yaml file and use that file instead. In that case, the path to that file will be returned so that the caller can change the working directory to that folder before running further commands.

type Remotes

type Remotes map[string][]*execute.SSHConfig

Remotes are the mapping of env to list of remote hosts.

func (*Remotes) List

func (r *Remotes) List(w io.Writer) error

List will return a list of all possible remotes defined

type Target

type Target struct {
	File        *File     `json:"-" yaml:"-"`
	Name        string    `json:"name" yaml:"name"`
	Before      []string  `json:"before" yaml:"before"`
	After       []string  `json:"after" yaml:"after"`
	Cmd         string    `json:"cmd" yaml:"cmd"`
	Description string    `json:"description" yaml:"description"`
	W           io.Writer `json:"-" yaml:"-"` // underlying stdout writer
	WErr        io.Writer `json:"-" yaml:"-"` // underlying stderr writer
	// contains filtered or unexported fields
}

Target contains the set of commands to run along with any before and after targets to run.

func (*Target) List

func (t *Target) List(verbose bool, nameWidth int)

List displays the defined before, after, description and cmd of the target.

func (*Target) Run

func (t *Target) Run() (int, string, error)

Run executes the targets before commands then runs its own followed by after targets. Try not to make circular references please.

func (*Target) RunRemote

func (t *Target) RunRemote(conf *execute.SSHConfig) (int, string, error)

RunRemote executes the target on a remote host. It ignores any before and after targets.

Jump to

Keyboard shortcuts

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