Documentation
¶
Overview ¶
Package interpolate expands shell-style parameters, such as $NAME and ${NAME:-default}, in strings. It implements POSIX parameter expansion and some bash extensions, and reads values from an Env.
Supported expansions:
$NAME or ${NAME} the value of NAME, or an empty string if NAME is unset
${NAME:-word} word if NAME is unset or empty
${NAME-word} word if NAME is unset
${NAME:?message} an error if NAME is unset or empty
${NAME?message} an error if NAME is unset
${NAME:+word} word if NAME is set and not empty, otherwise an empty string
${NAME+word} word if NAME is set, otherwise an empty string
${#NAME} the length of the value in characters
${NAME:offset} the value from offset
${NAME:offset:length} at most length characters of the value from offset
Write $$ or \$ for a literal dollar sign, and \} for a literal closing brace in a word. A $ that is not followed by a letter, an underscore, or { is kept as-is, so $1 and $(command) do not change.
Interpolate and Identifiers are safe for concurrent use if the Env is. A Parser is not.
Index ¶
- func Identifiers(str string) ([]string, error)
- func Interpolate(env Env, str string) (string, error)
- type AlternateExpansion
- type AlternateNonEmptyExpansion
- type EmptyValueExpansion
- type Env
- type Expansion
- type Expression
- type ExpressionItem
- type LengthExpansion
- type Parser
- type RequiredExpansion
- type RequiredNonEmptyExpansion
- type SubstringExpansion
- type UnsetValueExpansion
- type VariableExpansion
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Identifiers ¶
Identifiers parses the identifiers from any expansions in the provided string
Example ¶
package main
import (
"fmt"
"log"
"github.com/mfridman/interpolate"
)
func main() {
identifiers, err := interpolate.Identifiers("${HOST:-localhost}:${PORT:?port is required} $1")
if err != nil {
log.Fatal(err)
}
fmt.Println(identifiers)
}
Output: [HOST PORT]
func Interpolate ¶
Interpolate takes a set of environment and interpolates it into the provided string using shell script expansions
Example ¶
package main
import (
"fmt"
"log"
"github.com/mfridman/interpolate"
)
func main() {
env := interpolate.NewSliceEnv([]string{
"NAME=James",
})
output, err := interpolate.Interpolate(env, "Hello... ${NAME} welcome to the ${ANOTHER_VAR:-🏖}")
if err != nil {
log.Fatal(err)
}
fmt.Println(output)
}
Output: Hello... James welcome to the 🏖
Types ¶
type AlternateExpansion ¶ added in v0.1.0
type AlternateExpansion struct {
Identifier string
Content Expression
}
AlternateExpansion returns the expansion of Content if the env is set, and an empty string otherwise. This is ${VAR+word}.
func (AlternateExpansion) Expand ¶ added in v0.1.0
func (e AlternateExpansion) Expand(env Env) (string, error)
func (AlternateExpansion) Identifiers ¶ added in v0.1.0
func (e AlternateExpansion) Identifiers() []string
type AlternateNonEmptyExpansion ¶ added in v0.1.0
type AlternateNonEmptyExpansion struct {
Identifier string
Content Expression
}
AlternateNonEmptyExpansion returns the expansion of Content if the env is set and not empty, and an empty string otherwise. This is ${VAR:+word}.
func (AlternateNonEmptyExpansion) Expand ¶ added in v0.1.0
func (e AlternateNonEmptyExpansion) Expand(env Env) (string, error)
func (AlternateNonEmptyExpansion) Identifiers ¶ added in v0.1.0
func (e AlternateNonEmptyExpansion) Identifiers() []string
type EmptyValueExpansion ¶
type EmptyValueExpansion struct {
Identifier string
Content Expression
}
EmptyValueExpansion returns either the value of an env, or a default value if it's unset or null
func (EmptyValueExpansion) Identifiers ¶
func (e EmptyValueExpansion) Identifiers() []string
type Env ¶
Env is an interface for getting environment variables by name and returning a boolean indicating whether the variable was found.
func NewMapEnv ¶
NewMapEnv creates an Env from a map of environment variables.
Example ¶
package main
import (
"fmt"
"log"
"github.com/mfridman/interpolate"
)
func main() {
env := interpolate.NewMapEnv(map[string]string{
"USER": "llama",
"EMPTY": "",
})
output, err := interpolate.Interpolate(env, "${USER:+--user=$USER} ${EMPTY:-default} ${#USER}")
if err != nil {
log.Fatal(err)
}
fmt.Println(output)
}
Output: --user=llama default 5
func NewSliceEnv ¶
NewSliceEnv creates an Env from a slice of environment variables in the form "key=value".
This can be used with os.Environ to create an Env.
type Expansion ¶
type Expansion interface {
// Expand expands the expansion using variables from env.
Expand(env Env) (string, error)
// Identifiers returns any variable names referenced within the expansion.
Identifiers() []string
}
An expansion is something that takes in ENV and returns a string or an error
type Expression ¶
type Expression []ExpressionItem
Expression is a collection of either Text or Expansions
func (Expression) Expand ¶
func (e Expression) Expand(env Env) (string, error)
Expand expands all expansions in the expression using variables from env, and joins the results with the text.
func (Expression) Identifiers ¶
func (e Expression) Identifiers() []string
Identifiers returns the variable names referenced by all expansions in the expression, in order. A name can occur more than once.
type ExpressionItem ¶
ExpressionItem models either an Expansion or Text. Either/Or, never both.
func (ExpressionItem) String ¶
func (i ExpressionItem) String() string
type LengthExpansion ¶ added in v0.1.0
type LengthExpansion struct {
Identifier string
}
LengthExpansion returns the number of characters in an env value, or 0 if it is unset. This is ${#VAR}.
func (LengthExpansion) Expand ¶ added in v0.1.0
func (e LengthExpansion) Expand(env Env) (string, error)
func (LengthExpansion) Identifiers ¶ added in v0.1.0
func (e LengthExpansion) Identifiers() []string
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser takes a string and parses out a tree of structs that represent text and Expansions
func (*Parser) Parse ¶
func (p *Parser) Parse() (Expression, error)
Parse expansions out of the internal text and return them as a tree of Expressions. Each call parses the whole input again.
type RequiredExpansion ¶
type RequiredExpansion struct {
Identifier string
Message Expression
}
RequiredExpansion returns an env value, or an error if it is unset. This is ${VAR?message}.
func (RequiredExpansion) Identifiers ¶
func (e RequiredExpansion) Identifiers() []string
type RequiredNonEmptyExpansion ¶ added in v0.1.0
type RequiredNonEmptyExpansion struct {
Identifier string
Message Expression
}
RequiredNonEmptyExpansion returns an env value, or an error if it is unset or empty. This is ${VAR:?message}.
func (RequiredNonEmptyExpansion) Expand ¶ added in v0.1.0
func (e RequiredNonEmptyExpansion) Expand(env Env) (string, error)
func (RequiredNonEmptyExpansion) Identifiers ¶ added in v0.1.0
func (e RequiredNonEmptyExpansion) Identifiers() []string
type SubstringExpansion ¶
SubstringExpansion returns a substring (or slice) of the env. This is ${VAR:offset} or ${VAR:offset:length}.
func (SubstringExpansion) Identifiers ¶
func (e SubstringExpansion) Identifiers() []string
type UnsetValueExpansion ¶
type UnsetValueExpansion struct {
Identifier string
Content Expression
}
UnsetValueExpansion returns either the value of an env, or a default value if it's unset
func (UnsetValueExpansion) Identifiers ¶
func (e UnsetValueExpansion) Identifiers() []string
type VariableExpansion ¶
type VariableExpansion struct {
Identifier string
}
VariableExpansion represents either $VAR or ${VAR}, our simplest expansion
func (VariableExpansion) Identifiers ¶
func (e VariableExpansion) Identifiers() []string