env

package module
v0.1.12 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2023 License: MIT Imports: 6 Imported by: 3

README

Deprecated; this package is no longer maintained.

env

Build Status Go Report Card GoDoc License MIT

Load environment variables into Go types, with fallback values.

This package is meant to be used when configuring a twelve-factor app.

The currently supported types are bool, []byte, time.Duration, float64, int, string, []string and *url.URL

Installation

go get -u github.com/TV4/env

Usage

package main

import (
	"fmt"
	"net/url"

	"github.com/TV4/env"
)

func main() {
	fmt.Println(
		env.Base64Bytes("BASE64_BYTES", []byte{1, 2, 3}),
		env.Bool("BOOL", false),
		env.Bytes("BYTES", []byte{4, 2}),
		env.Duration("DURATION", 250000),
		env.Float64("FLOAT64", float64(2.5)),
		env.Int("INT", 1337),
		env.String("STRING", "Foobar"),
		env.Strings("STRINGS", []string{"Foo", "Bar"}),
		env.URL("URL", &url.URL{Scheme: "http", Host: "example.com"}),
	)
}
go run example.go
[1 2 3] false [4 2] 250µs 2.5 1337 Foobar [Foo Bar] http://example.com

BASE64_BYTES=Zm9v BOOL=true BYTES=foo DURATION=24m FLOAT64=11.2 INT=2600 STRING=hello STRINGS=a,b URL=http://c7.se/ go run example.go
[102 111 111] true [102 111 111] 24m0s 11.2 2600 hello [a b] http://c7.se/

License (MIT)

Copyright (c) 2015-2019 TV4

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Deprecated: This package is no longer maintained.

Package env loads environment variables into Go types, with fallback values.

Installation

Just go get the package:

go get -u github.com/TV4/env

Usage

A small usage example

package main

import (
	"fmt"
	"net/url"

	"github.com/TV4/env"
)

func main() {
	fmt.Println(
		env.Bool("BOOL", false),
		env.Bytes("BYTES", []byte{4, 2}),
		env.Duration("DURATION", 250000),
		env.Float64("FLOAT64", float64(2.5)),
		env.Int("INT", 1337),
		env.String("STRING", "Foobar"),
		env.Strings("STRINGS", []string{"Foo", "Bar"}),
		env.URL("URL", &url.URL{Scheme: "http", Host: "example.com"}),
	)
}

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultClient = NewClient(os.Getenv)

DefaultClient is the default client backed by os.Getenv

Functions

func Base64Bytes added in v0.1.3

func Base64Bytes(key string, fallback []byte) []byte

Base64Bytes returns a slice of bytes from the ENV decoded using base64.StdEncoding, or fallback variable

func Bool

func Bool(key string, fallback bool) bool

Bool returns a bool from the ENV, or fallback variable

Example
package main

import (
	"fmt"
	"os"

	"github.com/TV4/env"
)

func main() {
	os.Setenv("BOOL", "t")

	fmt.Println(env.Bool("BOOL", false))
}
Output:

true

func Bytes

func Bytes(key string, fallback []byte) []byte

Bytes returns a slice of bytes from the ENV, or fallback variable

Example
package main

import (
	"fmt"
	"os"

	"github.com/TV4/env"
)

func main() {
	os.Setenv("BYTES", "foo")

	fmt.Printf("%s", env.Bytes("BYTES", nil))
}
Output:

foo

func Duration

func Duration(key string, fallback time.Duration) time.Duration

Duration returns a duration from the ENV, or fallback variable

Example
package main

import (
	"fmt"
	"os"

	"github.com/TV4/env"
)

func main() {
	os.Setenv("DURATION", "23s")

	fmt.Printf("%s", env.Duration("DURATION", 0))
}
Output:

23s

func Float64

func Float64(key string, fallback float64) float64

Float64 returns a float64 from the ENV, or a fallback variable

Example
package main

import (
	"fmt"
	"os"

	"github.com/TV4/env"
)

func main() {
	os.Setenv("FLOAT64", "1.23")

	fmt.Println(env.Float64("FLOAT64", 0))
}
Output:

1.23

func Func

func Func(v map[string]string) func(string) string

Func creates a func(string) string backed by a map[string]string

func Int

func Int(key string, fallback int) int

Int returns an int from the ENV, or fallback variable

Example
package main

import (
	"fmt"
	"os"

	"github.com/TV4/env"
)

func main() {
	os.Setenv("INT", "345")

	fmt.Println(env.Int("INT", 0))
}
Output:

345

func String

func String(key, fallback string) string

String returns a string from the ENV, or fallback variable

Example
package main

import (
	"fmt"
	"os"

	"github.com/TV4/env"
)

func main() {
	os.Setenv("STRING", "foo bar")

	fmt.Println(env.String("STRING", ""))
}
Output:

foo bar

func Strings

func Strings(key string, fallback []string, seps ...string) []string

Strings returns a slice of strings from the ENV, or fallback variable

Example
package main

import (
	"fmt"
	"os"

	"github.com/TV4/env"
)

func main() {
	os.Setenv("STRINGS", "foo,bar,baz")

	fmt.Println(env.Strings("STRINGS", []string{}))
}
Output:

[foo bar baz]

func URL

func URL(key string, fallback *url.URL) *url.URL

URL returns a URL from the ENV, or fallback URL if missing/invalid

Example
package main

import (
	"fmt"
	"net/url"
	"os"

	"github.com/TV4/env"
)

func main() {
	os.Setenv("URL", "http://example.com/foo")

	fmt.Println(env.URL("URL", &url.URL{Host: "fallback"}).String())
}
Output:

http://example.com/foo

Types

type Client

type Client interface {
	Base64Bytes(key string, fallback []byte) []byte
	Bool(key string, fallback bool) bool
	Bytes(key string, fallback []byte) []byte
	Duration(key string, fallback time.Duration) time.Duration
	Float64(key string, fallback float64) float64
	Int(key string, fallback int) int
	String(key, fallback string) string
	Strings(key string, fallback []string, seps ...string) []string
	URL(key string, fallback *url.URL) *url.URL
}

Client is the env client interface

func MapClient added in v0.1.1

func MapClient(v map[string]string) Client

MapClient creates a new Client backed by provided map[string]string

func NewClient

func NewClient(f func(string) string) Client

NewClient creates a new Client backed by provided func(string) string

type Map added in v0.1.1

type Map map[string]string

Map is a map[string]string

Jump to

Keyboard shortcuts

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