withenv

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2022 License: BlueOak-1.0.0 Imports: 10 Imported by: 2

README

withenv: runs commands with extra environment settings

It's just like envdir, but for go, and it reads files.

Goals

When I looked around, I couldn't find a tool for a small need: to run an application quickly with appropriate environment variables set, when no shell is available.

This particular use case helps running handlers in sensu-go, while secrets are not exposed to the API / web UI. Information about a file on a system is less threatening than seeing the actual secret set by an environment variable, which is not even redacted by the user interface.

Usage

usage: withenv PATH COMMAND [ARGS...]

Set environment variables defined in file PATH, and run COMMAND.

  -v       print program version
  -h       this help
Arguments:
  PATH     path to a file containing variable declarations in a KEY=VAL
           format. Spaces around KEY and VAL are NOT stripped. VAL has
                   variable expansion.
  COMMAND  next executable in line, which will be run with the newly set
           environment variables.
  ARGS...  any command line items are taken to COMMAND after variable
           expansion.

This tool can be used in sensu-go, chaining other commands for checks or handlers. Currently, both in asset and in Bonsai form, albeit this is still in the works.

This project is licensed under Blue Oak Model License v1.0.0. It is not registered either at OSI or GNU, therefore GitHub is widely looking at the other direction. However, this is the license I'm most happy with: you can read and understand it with no legal degree, and there are no hidden or cryptic meanings in it.

The project is also governed with Contributor Covenant's Code of Conduct in mind. I'm not copying it here, as a pledge for taking the verbatim version by the word, and we are not going to modify it in any way.

Any issues?

Open a ticket, perhaps a pull request. We support GitHub Flow. You might want to fork this project first.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDeclError = errors.New("env declaration error")
)

Functions

This section is empty.

Types

type Env

type Env struct {
	Vars map[string]string
	sync.RWMutex
}

Env contains environment variables for command

func New

func New() *Env

New returns a new, empty Env

func (*Env) Child

func (env *Env) Child(out, err io.Writer, cmd string, args ...string) error

Child runs a given command as a child process, and returns its return value

func (*Env) Environ

func (env *Env) Environ() []string

Environ returns a slice of strings with environment declarations

func (*Env) Exec

func (env *Env) Exec(cmd string, args ...string) error

Exec runs a given command, replacing the current executable. It returns only if exec is not possible.

func (*Env) Expand

func (env *Env) Expand(s string) string

Expand does variable expansion for strings provided by replacing `${var}` and `$var` with values set in Env. Undefined variables replaced by an empty string.

func (*Env) Get

func (env *Env) Get(key string) (string, bool)

Get retrieves an environment value by key

func (*Env) GetOrDefault

func (env *Env) GetOrDefault(key, deflt string) string

GetOrDefault retrieves an env. value by key, if exists. It returns default value otherwise.

func (*Env) Load

func (env *Env) Load(envs []string) error

Load loads environment settings from slice

func (*Env) LoadMap

func (env *Env) LoadMap(envs map[string]string)

LoadMap loads environment settings from string map

func (*Env) Read

func (env *Env) Read(fn string) error

Read loads Env data from file

func (*Env) Run

func (env *Env) Run(cmd string, args ...string) error

Run runs a given command, waiting for execution, then returns the subprocess' return value.

func (*Env) Set

func (env *Env) Set(key, val string)

Set sets an environment key with a value

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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