Skip to content

Latest commit

 

History

History
95 lines (63 loc) · 1.66 KB

README.md

File metadata and controls

95 lines (63 loc) · 1.66 KB

Overview

env is a tiny library designed to simplify interactions with the environment in which your application runs. It provides an easy-to-use API for fetching environment variables, setting them, and performing various other environment-related tasks.

It exists mainly to support functionality of nv.

Installation

~> go get github.com/jcouture/env

Features

  • Clear the environment with exceptions
  • Check if an environment variable exists
  • Retrieve all environment variables
  • Set environment variables dynamically
  • And more!

Usage

Important note: this library works on a copy of your environment, just like Go’s stdlib.

Import the library

import "github.com/jcouture/env"

Clear the environment

Clears the environment but will skip any (optional) variable names passed as parameter.

env.Clear("HOME")

Check if a variable exists

if !env.Exists("FOO") {
  fmt.Printf("FOO not found\n")
}

Retrieve all variables

vars := env.Getvars()

for k, v := range vars {
  fmt.Printf("%v=%v\n", k, v)
}

Retrive all variable names

names := env.Getnames(env.Getvars())

for _, n := range names {
  fmt.Printf("%v\n", n)
}

Join two maps together to override variables

base := env.Getvars()

override := map[string]string{
  "PATH": "/usr/bin:/bin:/usr/sbin:/sbin",
  "HOME": "/home/user",
}

base = env.Join(base, override)

Set variables

vars := map[string]string{
  "SHELL": "/usr/bin/zsh",
  "TERM":  "xterm-256color",
}

env.Setvars(vars)

License

env is released under the MIT license. See LICENSE for details.