envsubst

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2017 License: MIT Imports: 3 Imported by: 75

README

envsubst Build status License GoDoc

Environment variables substitution for Go. see docs below

Installation:
$ go get github.com/a8m/envsubst/cmd/envsubst
Using via cli
$ envsubst < input.tmpl > output.text
$ echo 'welcome $HOME ${USER:=a8m}' | envsubst
$ envsubst -help
Imposing restrictions

There are two command line flags with which you can cause the substitution to stop with an error code, should the restriction associated with the flag not be met. This can be handy if you want to avoid creating e.g. configuration files with unset or empty parameters. The flags and their restrictions are:

Flag Meaning
-no-unset fail if a variable is not set
-no-empty fail if a variable is set but empty

These flags can be combined to form tighter restrictions.

Using envsubst programmatically ?

You can take a look on _example/main or see the example below.

package main

import (
	"fmt"
	"github.com/a8m/envsubst"
)

func main() {
    input := "welcom $HOME"
    str, err := envsubst.String(input)
    // ...
    buf, err := envsubst.Bytes([]byte(input))
    // ...
    buf, err := envsubst.ReadFile("filename")
}
Docs

api docs here: GoDoc

Expression Meaning
${var} Value of var (same as $var)
${var-$DEFAULT} If var not set, evaluate expression as $DEFAULT
${var:-$DEFAULT} If var not set or is empty, evaluate expression as $DEFAULT
${var=$DEFAULT} If var not set, evaluate expression as $DEFAULT
${var:=$DEFAULT} If var not set or is empty, evaluate expression as $DEFAULT
${var+$OTHER} If var set, evaluate expression as $OTHER, otherwise as empty string
${var:+$OTHER} If var set, evaluate expression as $OTHER, otherwise as empty string
table taken from here
See also
  • os.ExpandEnv(s string) string - only supports $var and ${var} notations
License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bytes

func Bytes(b []byte) ([]byte, error)

Bytes returns the bytes represented by the parsed template after processing it. If the parser encounters invalid input, it returns an error describing the failure.

func BytesRestricted

func BytesRestricted(b []byte, noUnset, noEmpty bool) ([]byte, error)

BytesRestricted returns the bytes represented by the parsed template after processing it. If the parser encounters invalid input, or a restriction is violated, it returns an error describing the failure.

func ReadFile

func ReadFile(filename string) ([]byte, error)

ReadFile call io.ReadFile with the given file name. If the call to io.ReadFile failed it returns the error; otherwise it will call envsubst.Bytes with the returned content.

func ReadFileRestricted

func ReadFileRestricted(filename string, noUnset, noEmpty bool) ([]byte, error)

ReadFileRestricted calls io.ReadFile with the given file name. If the call to io.ReadFile failed it returns the error; otherwise it will call envsubst.Bytes with the returned content.

func String

func String(s string) (string, error)

String returns the parsed template string after processing it. If the parser encounters invalid input, it returns an error describing the failure.

func StringRestricted

func StringRestricted(s string, noUnset, noEmpty bool) (string, error)

StringRestricted returns the parsed template string after processing it. If the parser encounters invalid input, or a restriction is violated, it returns an error describing the failure.

Types

This section is empty.

Directories

Path Synopsis
cmd
envsubst command
envsubst command line tool
envsubst command line tool
Most of the code in this package taken from golang/text/template/parse
Most of the code in this package taken from golang/text/template/parse

Jump to

Keyboard shortcuts

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