env

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2024 License: Apache-2.0 Imports: 10 Imported by: 8

README

godoc codecov Go Report Card

env - environment variable utilities

env is a collection of utilities for managing and interacting with os.Environ related things.

Installation

> go get github.com/go-corelibs/env@latest

Examples

Environ

func main() {
    value := env.String("KeyName", "Default Value")
    // if "KeyName" does not exist, value == "Default Value"
    env.Set("KeyName", "1")
    number := env.Get("KeyName", 10)
    // "KeyName" exists and number == int(1)
}

Go-CoreLibs

Go-CoreLibs is a repository of shared code between the Go-Curses and Go-Enjin projects.

License

Copyright 2024 The Go-CoreLibs Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use file except in compliance with the License.
You may obtain a copy of the license at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Overview

Package env provides a collection of environment variable utilities

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendPATH added in v1.1.0

func AppendPATH(path string) (err error)

AppendPATH moves or adds the given path to the end of the PATH environment variable

func Bool

func Bool(key string, def bool) (state bool)

Bool is a wrapper around the Default Env.Bool

func Clear

func Clear()

Clear is a wrapper around the Default Env.Clear

func Environ

func Environ() (variables []string)

Environ is a wrapper around the Default Env.Env

func Expand

func Expand(input string) (expanded string)

Expand is a wrapper around the Default Env.Expand

func Export

func Export() (err error)

Export is a wrapper around the Default Env.Export

func Float

func Float(key string, def float64) (decimal float64)

Float is a wrapper around the Default Env.Float

func Get

func Get(key string) (value string, present bool)

Get is a wrapper around the Default Env.Get

func Import

func Import(environment []string)

Import is a wrapper around the Default Env.Import

func Int

func Int(key string, def int) (number int)

Int is a wrapper around the Default Env.Int

func Len

func Len() (count int)

Len is a wrapper around the Default Env.Len

func PATH added in v1.1.0

func PATH() (path string)

PATH returns the current PATH environment

func PATHS added in v1.1.0

func PATHS() (paths []string)

PATHS returns a slice of paths from the current PATH environment

func PrependPATH added in v1.1.0

func PrependPATH(path string) (err error)

PrependPATH moves or adds the given path to the start of the PATH environment variable

func PrunePATH added in v1.1.0

func PrunePATH(path string) (err error)

PrunePATH removes the given path from the PATH environment variable

func Set

func Set(key, value string)

Set is a wrapper around the Default Env.Set

func String

func String(key string, def string) (value string)

String is a wrapper around the Default Env.String

func TidyPATH added in v1.1.0

func TidyPATH() (err error)

TidyPATH removes all paths from the PATH environment variable that do not actually exist on the filesystem or cannot be resolved to their absolute, cleaned, paths

Types

type Env

type Env interface {
	// Len returns the number of variables present
	Len() (count int)
	// Clear deletes all Env variables
	Clear()
	// Environ returns a copy of all the underlying keys and values in the
	// form of "key=value"
	Environ() (variables []string)
	// Clone returns a complete duplicate of the current Env, changes to
	// a clone do not have any effect upon the original Env
	Clone() (clone Env)
	// Export updates the actual os environment, calling os.Setenv for each
	// variable present. Export stops at the first error
	Export() (err error)
	// Import updates the Env instance with the given `environment`
	// variables, in the form of "key=value". Inputs missing the equal sign are
	// ignored and all values have any quotes trimmed. Quotations are detected
	// when the first and last characters are the same and are one of the
	// following: backtick (&96;), quote (') or double quote (")
	Import(environment []string)
	// Include applies all variables within the others to this Env instance.
	// Note that keys are not deleted and any existing keys are clobbered by
	// the others, in the order the others are given
	Include(others ...Env)
	// WriteEnvDir makes the given directory path if it doesn't exist already
	// and then for each key/value pair in the Env, creates a file named with
	// the key and the value as the contents
	WriteEnvDir(path string) (err error)
	// Expand replaces all `$key` and `${key}` references in the `input` string
	// with their corresponding `key` values. Any references not present within
	// the Env are replaced with empty strings
	Expand(input string) (expanded string)
	// Get looks for the variable `key` and if `present` returns the exact
	// `value`
	Get(key string) (value string, present bool)
	// Set updates the Env `key` with the given `value`
	Set(key, value string)
	// Unset removes the `key` from the Env
	Unset(key string)
	// Bool transforms the value associated with `key` into a boolean state. If
	// the value is not a detectable state, `def` is returned. Detection is
	// handled by the github.com/go-corelibs/strings package IsTrue and IsFalse
	// functions
	Bool(key string, def bool) (state bool)
	// Int uses strconv.Atoi to transform the value associated with `key` and
	// if not present or strconv.Atoi encountered an error, returns `def`
	Int(key string, def int) (number int)
	// Float uses strconv.ParseFloat to transform the value associated with
	// `key` and if not present or strconv.ParseFloat encountered an error,
	// returns `def`
	Float(key string, def float64) (decimal float64)
	// String uses strings.TrimSpace to transform the value associated with
	// `key` and if not present, returns `def`
	String(key string, def string) (value string)
}

Env is an abstraction around the standard os package env related things with some conveniences added like Env.Int and Env.Float

func Clone

func Clone() (clone Env)

Clone is a wrapper around the Default Env.Clone

func Default

func Default() (env Env)

Default returns a package global Env instance, populated with the existing os.Environ variables

func New

func New() (env Env)

New constructs a new Env instance with no variables present

func NewImport added in v1.1.0

func NewImport(environ []string) (env Env)

NewImport constructs a new Env instance and calls Import with the given `environ` slice

Jump to

Keyboard shortcuts

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