envi

package module
v2.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

README

envi

Installation

go get github.com/Clarilab/envi/v2

Importing

import "github.com/Clarilab/envi/v2"

Available functions

	// FromMap loads the given key-value pairs and loads them into the local map.
	FromMap(map[string]string)

	// LoadEnv loads the given keys from environment.
	LoadEnv(vars ...string)

	// LoadYAMLFilesFromEnvPaths loads yaml files from the paths in the given environment variables.
	LoadYAMLFilesFromEnvPaths(vars ...string) error 

	// LoadYAMLFilesFromEnvPaths loads json files from the paths in the given environment variables.
	LoadJSONFilesFromEnvPaths(vars ...string) error

	// LoadYAMLFilesFromEnvPaths loads the file content from the path in the given environment variable to the value of the given key.
	LoadFileFromEnvPath(key string, envPath string) error 

	// LoadFile loads a string value under given key from a file.
	LoadFile(key, filePath string) error

	// LoadJSON loads key-value pairs from one or many json blobs.
	LoadJSON(...[]byte) error

	// LoadAndWatchJSONFile loads key-value pairs from a json file,
	// then watches that file and reloads it when it changes.
	// Accepts optional callback functions that are executed
	// after the file was reloaded. Returns and error when something
	// goes wrong. When no error is returned, returns a close function
	// that should be deferred in the calling function, and an error
	// channel where errors that occur during the file watching get sent.
	LoadAndWatchJSONFile(path string, callbacks ...func() error) (error, func() error, <-chan error)

	// LoadJSONFile loads key-value pairs from a json file.
	LoadJSONFile(path string) error

	// LoadJSONFiles loads key-value pairs from one or more json files.
	LoadJSONFiles(...string) error

	// LoadYAML loads key-value pairs from one or many yaml blobs.
	LoadYAML(...[]byte) error

	// LoadAndWatchYAMLFile loads key-value pairs from a yaml file,
	// then watches that file and reloads it when it changes.
	// Accepts optional callback functions that are executed
	// after the file was reloaded. Returns and error when something
	// goes wrong. When no error is returned, returns a close function
	// that should be deferred in the calling function, and an error
	// channel where errors that occur during the file watching get sent.
	LoadAndWatchYAMLFile(path string, callbacks ...func() error,) (error, func() error, <-chan error)

	// LoadYAMLFile loads key-value pairs from a yaml file.
	LoadYAMLFile(path string) error 

	// LoadYAMLFiles loads key-value pairs from one or more yaml files.
	LoadYAMLFiles(...string) error

	// EnsureVars checks, if all given keys have a non-empty value.
	EnsureVars(...string) error

	// ToEnv writes all key-value pairs to the environment.
	ToEnv()

	// ToMap returns a map, containing all key-value pairs.
	ToMap() map[string]string
Examples

If you want to load key-values pairs from one or more json files into a map, you can use envi something like this.

e := envi.NewEnvi()
err := e.LoadJSONFiles("./path/to/my/file.json", "./path/to/another/file.json")

myEnvVars := e.ToMap()

lookMom := myEnvVars["LOOK_MOM"]

To load environment variables something like this can be useful.

e := envi.NewEnvi()
err := e.LoadEnv("HOME", "LOOK_MOM")

myEnvVars := e.ToMap()

lookMom := myEnvVars["LOOK_MOM"]

In many cases you want to ensure, that variables have a non empty value. This can be checked by using EnsureVars().

e := envi.NewEnvi()
err := e.LoadEnv("HOME", "LOOK_MOM")

err := e.EnsureVars("HOME")
if err != nil {
  // the error contains a list of missing vars.
  fmt.Println(err.MissingVars)

  // the Error() method prints the missing vars, too.
  fmt.Println(err
}

Secretfile Example

A json file should look like this.

{
    "SHELL": "csh",
    "PAGER": "more"
}

A basic yaml file like this.

SHELL: bash
PAGER: less

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EnvVarNotFoundError added in v2.1.0

type EnvVarNotFoundError struct {
	// contains filtered or unexported fields
}

EnvVarNotFoundError says, that a given Environment Variable is not found.

func (*EnvVarNotFoundError) Error added in v2.1.0

func (e *EnvVarNotFoundError) Error() string

Error implements the error interface.

type Envi

type Envi struct {
	// contains filtered or unexported fields
}

Envi is a config loader to load all sorts of configuration files.

func NewEnvi

func NewEnvi() *Envi

NewEnvi creates a new Envi instance.

func (*Envi) EnsureVars

func (envi *Envi) EnsureVars(requiredVars ...string) error

EnsureVars checks, if all given keys have a non-empty value.

func (*Envi) FromMap

func (envi *Envi) FromMap(vars map[string]string)

FromMap loads the given key-value pairs and loads them into the local map.

func (*Envi) LoadAndWatchJSONFile added in v2.2.0

func (envi *Envi) LoadAndWatchJSONFile(path string, callback ...func() error) (error, func() error, <-chan error)

LoadAndWatchJSONFile loads key-value pairs from a json file, then watches that file and reloads it when it changes. Accepts optional callback functions that are executed after the file was reloaded. Returns and error when something goes wrong. When no error is returned, returns a close function that should be deferred in the calling function, and an error channel where errors that occur during the file watching get sent.

func (*Envi) LoadAndWatchYAMLFile added in v2.2.0

func (envi *Envi) LoadAndWatchYAMLFile(path string, callbacks ...func() error) (error, func() error, <-chan error)

LoadAndWatchYAMLFile loads key-value pairs from a yaml file, then watches that file and reloads it when it changes. Accepts optional callback functions that are executed after the file was reloaded. Returns and error when something goes wrong. When no error is returned, returns a close function that should be deferred in the calling function, and an error channel where errors that occur during the file watching get sent.

func (*Envi) LoadEnv

func (envi *Envi) LoadEnv(vars ...string)

LoadEnv loads the given keys from the environment variables.

func (*Envi) LoadFile added in v2.0.1

func (envi *Envi) LoadFile(key, filePath string) error

LoadFile loads a string value under given key from a file.

func (*Envi) LoadFileFromEnvPath added in v2.1.0

func (envi *Envi) LoadFileFromEnvPath(key string, envPath string) error

LoadYAMLFilesFromEnvPaths loads the file content from the path in the given environment variable to the value of the given key.

func (*Envi) LoadJSON

func (envi *Envi) LoadJSON(blobs ...[]byte) error

LoadJSON loads key-value pairs from one or many json blobs.

func (*Envi) LoadJSONFile added in v2.1.0

func (envi *Envi) LoadJSONFile(path string) error

LoadJSONFile loads key-value pairs from a json file.

func (*Envi) LoadJSONFiles

func (envi *Envi) LoadJSONFiles(paths ...string) error

LoadJSONFiles loads key-value pairs from one or more json files.

func (*Envi) LoadJSONFilesFromEnvPaths added in v2.1.0

func (envi *Envi) LoadJSONFilesFromEnvPaths(vars ...string) error

LoadYAMLFilesFromEnvPaths loads json files from the paths in the given environment variables.

func (*Envi) LoadYAML

func (envi *Envi) LoadYAML(blobs ...[]byte) error

LoadYAML loads key-value pairs from one or many yaml blobs.

func (*Envi) LoadYAMLFile added in v2.1.0

func (envi *Envi) LoadYAMLFile(path string) error

LoadYAMLFile loads key-value pairs from a yaml file.

func (*Envi) LoadYAMLFiles

func (envi *Envi) LoadYAMLFiles(paths ...string) error

LoadYAMLFiles loads key-value pairs from one or more yaml files.

func (*Envi) LoadYAMLFilesFromEnvPaths added in v2.1.0

func (envi *Envi) LoadYAMLFilesFromEnvPaths(vars ...string) error

LoadYAMLFilesFromEnvPaths loads yaml files from the paths in the given environment variables.

func (*Envi) ToEnv

func (envi *Envi) ToEnv()

ToEnv writes all key-value pairs to the environment.

func (*Envi) ToMap

func (envi *Envi) ToMap() map[string]string

ToMap returns a map, containing all key-value pairs.

type FailedToReadFileError added in v2.1.0

type FailedToReadFileError struct {
	// contains filtered or unexported fields
}

FailedToReadFileError says, that a given file could not be read.

func (*FailedToReadFileError) Error added in v2.1.0

func (e *FailedToReadFileError) Error() string

Error implements the error interface.

type RequiredEnvVarsMissing

type RequiredEnvVarsMissing struct {
	MissingVars []string
}

RequiredEnvVarsMissing says, that a required Environment Variable is not given.

func (*RequiredEnvVarsMissing) Error

func (e *RequiredEnvVarsMissing) Error() string

Jump to

Keyboard shortcuts

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