envim

package module
v0.0.0-...-2d893a9 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2021 License: BSD-3-Clause Imports: 2 Imported by: 0

README

envim - Go environment variables for human

Build status Go Report Card GoDoc

Why envim?

Just another (better) way for managing environment variables from Go code, but written for human.

Installation

go get -u github.com/cuonglm/envim

Usage

package main

import (
	"fmt"

	env "github.com/cuonglm/envim"
)

func main() {

	// Clean environment variables
	env.Clear()

	// Set a variable
	_ = env.Set("foo", "foo")
	_ = env.Set("fooo", "")
	_ = env.Set("GOPATH", "/home/cuonglm/go")
	_ = env.Set("GOROOT", "/home/cuonglm/sources/go")

	// Get a variable
	fmt.Println(env.Get("foo"))
	fmt.Println(env.Get("fooo"))

	// Check environment variable is set
	fmt.Println(env.IsSet("foo"))
	fmt.Println(env.IsSet("NotSetVar"))

	// Unset a variable
	_ = env.Unset("fooo")

	// Get all variables into map
	fmt.Printf("%+v\n", env.Map())

	// Like Map(), but variable with prefix only
	fmt.Printf("%+v\n", env.MapWithPrefix("GO"))

	// Update environment variables from a map
	// skipped invalid
	m := map[string]string{"bar": "bar", "=": "equal"}
	env.FromMap(m)
	fmt.Printf("%+v\n", env.Map())
}

Run that file give you:

$ go run envim_example.go
foo

true
false
map[foo:foo GOPATH:/home/cuonglm/go GOROOT:/home/cuonglm/sources/go]
map[GOROOT:/home/cuonglm/sources/go GOPATH:/home/cuonglm/go]
map[foo:foo GOPATH:/home/cuonglm/go GOROOT:/home/cuonglm/sources/go bar:bar]

Author

Cuong Manh Le cuong.manhle.vn@gmail.com

License

See LICENSE

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clear

func Clear()

Clear cleans environment

func FromMap

func FromMap(m map[string]string)

FromMap populates environment with key-value pair from map m A pair with invalid key/value will be skipped

func Get

func Get(key string) string

Get gets value of environment variable named key Returns empty string if variable is not set

func IsSet

func IsSet(key string) bool

IsSet check variable is set or not

func Map

func Map() map[string]string

Map returns a map contains environment variable key-value pair

func MapWithPrefix

func MapWithPrefix(prefix string) map[string]string

MapWithPrefix like Map(), but only contains key start with prefix

func Set

func Set(key, value string) error

Set sets environment variable named key new value. Returns an error if occurred.

func Unset

func Unset(key string) error

Unset unsets an environment variable Returns an error if occurred.

Types

This section is empty.

Jump to

Keyboard shortcuts

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