env

package
v1.2.0-beta Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2024 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Example (NewEntriesProvider_1)
// Invalid Prefix detection
_, err := NewEntriesProvider("asd")

if err == nil {
	panic("err must be returned")
}

fmt.Println("Got error:")
fmt.Println(err)
Output:

Got error:
"asd" : invalid prefix
Example (NewEntriesProvider_2)
// creates some env var for testing purpose only
err := os.Setenv("API-SOME-KEY1", "value1")
if err != nil {
	panic(err)
}

err = os.Setenv("API-SOME-KEY2", "value2")
if err != nil {
	panic(err)
}

err = os.Setenv("API-SOME-KEY3", "value3")

if err != nil {
	panic(err)
}

err = os.Setenv("OTHERPREFIX-SOME-KEY3", "value3")
if err != nil {
	panic(err)
}

// N.B the provider will only grab the environment variables that starts
// with the provided prefix.

provider, err := NewEntriesProvider("API")

if err != nil {
	panic(err)
}

fmt.Println("\nHere are the stringValues")

stringValues := provider.GetStringValues()

var keys []string
for s := range stringValues {
	keys = append(keys, s)
}

sort.Strings(keys)

for _, key := range keys {
	value := stringValues[key]
	fmt.Printf(
		"key:<%s>  location:<%s> value:<%s>\n",
		key,
		value.Location,
		value.Value,
	)
}
Output:

Here are the stringValues
key:<some-key1>  location:<env[API-SOME-KEY1]> value:<value1>
key:<some-key2>  location:<env[API-SOME-KEY2]> value:<value2>
key:<some-key3>  location:<env[API-SOME-KEY3]> value:<value3>
Example (NewEntriesProvider_3)
// creates some env var for testing purpose only
err := os.Setenv("APISOME-KEY1", "value1")
if err != nil {
	panic(err)
}

err = os.Setenv("API-SOME-_KEY2", "value2")
if err != nil {
	panic(err)
}

err = os.Setenv("API-SOME-kEY3", "value3")
if err != nil {
	panic(err)
}

// that key will not be scanned because prefix does not match.
err = os.Setenv("OTHERPREFIX-SOME-KEY3", "value3")
if err != nil {
	panic(err)
}

// that key will match perfectly, but malformed keys will abort the
// creation.
err = os.Setenv("API-SOME-KEY4", "value3")
if err != nil {
	panic(err)
}

_, err = NewEntriesProvider("API")
if err == nil {
	panic("must return an error")
}

fmt.Println("Got error:")
fmt.Println(err)
Output:

Got error:
"API-SOME-_KEY2", "API-SOME-kEY3" and "APISOME-KEY1" are ambiguous

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrAmbiguousKey = errors.New("is ambiguous")

ErrAmbiguousKey represent an error when a key starts with a valid prefix but with invalid syntax.

View Source
var ErrAmbiguousKeys = errors.New("are ambiguous")

ErrAmbiguousKeys represent an error when multiple env keys starts with a valid prefix but with invalid syntax.

View Source
var ErrInvalidPrefix = errors.New("invalid prefix")

ErrInvalidPrefix represents an error when creating the provider with an invalid prefix.

Functions

This section is empty.

Types

type EntriesProvider

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

EntriesProvider is an entries' provider that extract entries from environment variables.

func NewEntriesProvider

func NewEntriesProvider(prefix string) (*EntriesProvider, error)

NewEntriesProvider creates an entries provider based on environment variable scanning. It's sensitive to a prefix that *MUST* match this regexp '^[A-Z][A-Z\d]*$'.

func (*EntriesProvider) GetStringValues

func (e *EntriesProvider) GetStringValues() svalue.Values

GetStringValues implements sbased2.Provider interface.

Jump to

Keyboard shortcuts

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