sellsword

package module
v0.0.0-...-216c1da Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2015 License: Apache-2.0 Imports: 14 Imported by: 0

README

sellsword

Sellsword is a generic command-line tool for switching between application environments

Technology consultants, such as this project's original author, have to manage different environments for a given application for each customer. Doing this separately for each application just sucks. Sellsword is a generic command-line tool for switching between arbitrary application environments. Sellsword currently supports two mechanisms for switching between applications:

  • loading environment variables
  • changing symlinks to either directories or individual files

There are two components to sellsword, ssw and sellsword. The sellsword binary does all the work but cannot source any environment variables into the parent shell. source /path/to/ssw [ARG ...] executes sellsword with t the supplied arguments and loads any changed environment variables into the parent shell. This is a huge hack but it is the only way I know how to load environment variables into the parent shell.

Sellsword is only supported for the BASH shell and on the OS X and linux operating systems. Sellsword is implemented primarily in Go because writing complex logic in BASH dramatically shortens one's life expectancy.

Sellsword has two core concepts, applications which are defined by YAML files in ~/.ssw/config/ and environments per application stored in ~/.ssw/appname/. The environment can be a set of environment variables, a directory containing arbitrary files, or a single file to be linked to a particular location.

There are two types of environments, directory and environment. I realize the naming of this second type is very confusing so suggestions are most welcome.

Installation

  • Download the tarball

  • tar xvzf sellsword*.gz -C /usr/local/bin

  • Add the following to your .bashrc file:

      # this loads environment variables for current configurations
      alias ssw='source $(which ssw)'
      ssw load
    

Configuration

Sellsword knows about a few applications by default but these can be overridden:

.ssw/
     config/
             aws.ssw  # this is just a yaml file, .ssw extension is used to avoid conflicts
                      # with the application's own configuration files
Example environment for AWS
.ssw/
     aws/
        acme-dev
        acme-qa
        acme-prod
        megacorp-dev
        megacorp-qa
        megacorp-prod
        current  # symlink to current environment

The environment file which current points to will be sourced to the parent shell and should only include key/value pairs. Note that this actually a yaml file. Also note that we are using lower-case values here and not the typical uppercase used by environment variables. This is because most self-respecting developers swap caps lock with control, making long uppercase names inconvenient to type.

# file ~/.ssw/aws/acme-dev
access_key: ASDFAFASDFSDAF...
secret_key: asdfasdfadsf...

There should be a corresponding configuration file that maps the keys to environment variables. Notice that you can map a single key to multiple environment variables.

# file ~/.ssw/config/aws.ssw yaml
type: environment

variables:
  - access_key=AWS_ACCESS_KEY_ID
  - access_key=AWS_ACCESS_ID
  - region=AWS_DEFAULT_REGION
  - region=AWS_REGION

It is important to note that the variables section is a list of key/value pairs where the same keys can be present multiple times. This is so that the same key can be mapped to multiple values. The reason for this is that different applications often use different names for the same environment variables.

Example Setup for Chef Server

.ssw/
     chef/
        acme-dev/
                knife.rb
                chefadmin.pem
                default-validator.pem
        acme-qa/
        acme-prod/
     config/
        chef.ssw
# file chef.ssw
type: directory
target: ~/.chef

Sellsword supports running arbitrary shell command when an environment is loaded and unloaded. In practice this is only relevant to directory environments, at least in my experience.

For example, you could use sellsword to manage which ssh keys are loaded into your local SSH Agent

# file ssh.ssw
type: directory
target: ~/ssh
load: ssh-add $SSW_CURRENT/*.pem $SSW_CURRENT/*.priv
unload: ssh-add -D

Here is what the contents of ~/.ssw/ssh/acme directory look like

acme-prod.pem
acme-dev.priv
acme-dev.pub

Usage

ssw list chef          # list possible chef environments
ssw show chef          # show current environment in use
ssw load               # load default environments
ssw new aws acme-prod  # wizard to create new aws environment
ssw use aws acme-qa
ssw unlink aws         # unlink default environment but do not delete the
                       # actual environment
ssw rm aws acme-qa     # remove acme-qa environment  TODO

For applications with the environment type, sellsword new app_name env_name will interactively prompt you for the values needed.

Development

This project uses godep for managing dependencies. Godep has some quirks so it is worth reading a tutorial or two. The trickiest part is that you check out the sellsword to a location inside your GOPATH/src. This author locates the sellsword code at $GOPATH/src/github.com/bryanwb/sellsword.

To install godep, go get github.com/tools/godep

I also recommend setting the GOBIN environment variable to $GOPATH/bin

License and Author

Apache 2.0, Copyright 2015 Bryan W. Berry

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Logger *log.Logger
View Source
var Version = "0.0.3"

Functions

func GetTermPrinter

func GetTermPrinter(colorName color.Attribute) func(...interface{}) string

func GetTermPrinterF

func GetTermPrinterF(colorName color.Attribute) func(string, ...interface{}) string

Types

type App

type App struct {
	Name            string
	EnvType         string `yaml:"type"`
	Path            string
	Root            string
	Target          string
	Definition      string
	Variables       []string
	VariableNames   []string
	ExportVariables map[string]string
	LoadAction      string `yaml:"load"`
	UnloadAction    string `yaml:"unload"`
}

func NewApp

func NewApp(name string, sswHome string) (*App, error)

NewApp is the constructor for New Apps

func (*App) Current

func (a *App) Current() (*Env, error)

func (*App) EnumerateExportVars

func (a *App) EnumerateExportVars() []string
func (a *App) Link(envName string) error

func (*App) ListEnvs

func (a *App) ListEnvs() []*Env

func (*App) Load

func (a *App) Load() error

func (*App) MakeCurrent

func (a *App) MakeCurrent(envName string) error

func (*App) MakeUnsetExportVars

func (a *App) MakeUnsetExportVars() string

func (*App) NewEnv

func (a *App) NewEnv(envName string) (*Env, error)

func (*App) ParseExportVars

func (a *App) ParseExportVars() error
func (a *App) Unlink() error

func (*App) Unload

func (a *App) Unload() error

func (*App) UnsetExportVars

func (a *App) UnsetExportVars()

type AppSet

type AppSet struct {
	Apps []*App
	Home string
}

func NewAppSet

func NewAppSet(home string) (*AppSet, error)

func (*AppSet) FindApps

func (as *AppSet) FindApps(appNames ...string) error

func (*AppSet) ListApps

func (as *AppSet) ListApps(appNames []string)

type Env

type Env struct {
	Name            string
	Path            string
	Current         bool
	EnvType         string
	ExportVariables map[string]string
	Variables       map[string]string
}

func NewDirectoryEnv

func NewDirectoryEnv(name string, basePath string) (*Env, error)

NewDirectoryEnv is a factory method that properly initializes the Env struct for the env type of Directory

func NewEnv

func NewEnv(name string, basePath string, exportVars map[string]string, vars []string,
	envType string) (*Env, error)

func NewEnvironmentEnv

func NewEnvironmentEnv(name string, basePath string, exportVars map[string]string, vars []string) (*Env, error)

NewEnvironmentEnv is a factory method that properly initializes the Env struct for the env type of Environment

func (*Env) Construct

func (e *Env) Construct() error

*Constructs* a new environment, not to be confused w/ the Constructor NewEnv In case of environment type, queries user for values Not implemented for other types yet

func (*Env) Load

func (e *Env) Load()

func (*Env) MakeExportStatements

func (e *Env) MakeExportStatements() string

This is a separate function from PrintExports to make it easier to test

func (*Env) PopulateExportVars

func (e *Env) PopulateExportVars() error

func (*Env) PrintExports

func (e *Env) PrintExports()

func (*Env) Save

func (e *Env) Save() error

Directories

Path Synopsis
Godeps
_workspace/src/github.com/fatih/color
Package color is an ANSI color package to output colorized or SGR defined output to the standard output.
Package color is an ANSI color package to output colorized or SGR defined output to the standard output.
_workspace/src/github.com/mattn/go-isatty
Package isatty implements interface to isatty
Package isatty implements interface to isatty
_workspace/src/github.com/shiena/ansicolor
Package ansicolor provides color console in Windows as ANSICON.
Package ansicolor provides color console in Windows as ANSICON.
_workspace/src/github.com/shiena/ansicolor/ansicolor
The ansicolor command colors a console text by ANSI escape sequence like wac.
The ansicolor command colors a console text by ANSI escape sequence like wac.
_workspace/src/github.com/spf13/cobra
Package cobra is a commander providing a simple interface to create powerful modern CLI interfaces.
Package cobra is a commander providing a simple interface to create powerful modern CLI interfaces.
_workspace/src/github.com/spf13/pflag
pflag is a drop-in replacement for Go's flag package, implementing POSIX/GNU-style --flags.
pflag is a drop-in replacement for Go's flag package, implementing POSIX/GNU-style --flags.
_workspace/src/gopkg.in/yaml.v2
Package yaml implements YAML support for the Go language.
Package yaml implements YAML support for the Go language.
cmd

Jump to

Keyboard shortcuts

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