Skip to content
/ env Public

Tiny library designed to simplify interactions with the environment

License

Notifications You must be signed in to change notification settings

jcouture/env

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Tiny library designed to simplify interactions with the environment

Resources

License

Stars

Watchers

Forks