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. Expand reads values from an Env, and Variables lists the variables in a string.
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.
A required expansion that fails returns an *UnsetVariableError. Use WithNoUnset to make a reference to an unset variable an error too, the same as set -u in bash.
Expand and Variables are safe for concurrent use if the Env is.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Expand ¶ added in v0.2.0
Expand returns s with each parameter expansion replaced by its value, and each escape such as $$ replaced by its text. Values come from env. A nil env has no variables.
Expand returns an error if s is not valid, and an *UnsetVariableError if a variable that must have a value does not have one.
Example ¶
package main
import (
"fmt"
"log"
"github.com/mfridman/interpolate"
)
func main() {
env := interpolate.NewListEnv("NAME=James")
output, err := interpolate.Expand("Hello ${NAME}, welcome to the ${PLACE:-🏖}", env)
if err != nil {
log.Fatal(err)
}
fmt.Println(output)
}
Output: Hello James, welcome to the 🏖
func Variables ¶ added in v0.2.0
Variables returns the names of the variables that s refers to, in the order of their first use, with no repeats. It includes the variables in the words and messages of expansions, such as FALLBACK in ${NAME:-$FALLBACK}, and does not include escaped references such as $$NAME.
Variables returns an error if s is not valid.
Example ¶
package main
import (
"fmt"
"log"
"github.com/mfridman/interpolate"
)
func main() {
names, err := interpolate.Variables("${HOST:-localhost}:${PORT:?port is required} $1 $HOST")
if err != nil {
log.Fatal(err)
}
fmt.Println(names)
}
Output: [HOST PORT]
Types ¶
type Env ¶
type Env interface {
// Lookup returns the value of the variable with the given name, and whether the variable is
// set. A variable that is set can have an empty value.
Lookup(name string) (value string, ok bool)
}
Env is a source of variable values.
func NewListEnv ¶ added in v0.2.0
NewListEnv returns an Env with the variables in pairs, which use the "NAME=value" form of os.Environ. Pairs without "=" are ignored. If a name occurs more than once, the last pair wins.
On Windows, names are case insensitive.
func NewMapEnv ¶
NewMapEnv returns an Env with the variables in m. It copies m.
On Windows, names are case insensitive. If keys in m differ only in case, the last key in sorted order wins.
Example ¶
package main
import (
"fmt"
"log"
"github.com/mfridman/interpolate"
)
func main() {
env := interpolate.NewMapEnv(map[string]string{
"USER": "llama",
"EMPTY": "",
})
output, err := interpolate.Expand("${USER:+--user=$USER} ${EMPTY:-default} ${#USER}", env)
if err != nil {
log.Fatal(err)
}
fmt.Println(output)
}
Output: --user=llama default 5
type LookupFunc ¶ added in v0.2.0
LookupFunc is a function that is an Env. For example, LookupFunc(os.LookupEnv) reads the environment of the process.
Example ¶
package main
import (
"fmt"
"log"
"os"
"github.com/mfridman/interpolate"
)
func main() {
env := interpolate.LookupFunc(os.LookupEnv)
output, err := interpolate.Expand("${INTERPOLATE_EXAMPLE_UNSET:-default}", env)
if err != nil {
log.Fatal(err)
}
fmt.Println(output)
}
Output: default
type Option ¶ added in v0.2.0
type Option interface {
// contains filtered or unexported methods
}
Option configures Expand.
func WithNoUnset ¶ added in v0.2.0
func WithNoUnset() Option
WithNoUnset makes a reference to an unset variable an error, the same as set -u (nounset) in bash. It applies to $NAME, ${NAME}, ${#NAME}, ${NAME:offset} and ${NAME:offset:length}. Expansions that handle an unset variable, such as ${NAME:-word} and ${NAME+word}, do not change. The error is an *UnsetVariableError with the message "not set".
Example ¶
package main
import (
"fmt"
"github.com/mfridman/interpolate"
)
func main() {
env := interpolate.NewMapEnv(map[string]string{"SCHEMA": "app"})
_, err := interpolate.Expand("CREATE TABLE $SCHEMA.users AS $body$", env, interpolate.WithNoUnset())
fmt.Println(err)
}
Output: $body: not set
type UnsetVariableError ¶ added in v0.2.0
type UnsetVariableError struct {
// Name is the name of the variable.
Name string
// Message is the expanded message from the expansion, or a default message such as "not set".
Message string
}
UnsetVariableError is the error for a variable that must have a value but does not. It comes from ${NAME?message}, from ${NAME:?message}, and from a reference to an unset variable with WithNoUnset.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/mfridman/interpolate"
)
func main() {
_, err := interpolate.Expand("${PORT:?port is required}", nil)
var unsetErr *interpolate.UnsetVariableError
if errors.As(err, &unsetErr) {
fmt.Println(unsetErr.Name, "-", unsetErr.Message)
}
}
Output: PORT - port is required
func (*UnsetVariableError) Error ¶ added in v0.2.0
func (e *UnsetVariableError) Error() string
Error returns the error in the form "$NAME: message".