package interpolate
import "github.com/mfridman/interpolate"
Index ¶
- func Identifiers(str string) ([]string, error)
- func Interpolate(env Env, str string) (string, error)
- type EmptyValueExpansion
- func (e EmptyValueExpansion) Expand(env Env) (string, error)
- func (e EmptyValueExpansion) Identifiers() []string
- type Env
- type Expansion
- type Expression
- type ExpressionItem
- type Parser
- type RequiredExpansion
- func (e RequiredExpansion) Expand(env Env) (string, error)
- func (e RequiredExpansion) Identifiers() []string
- type SubstringExpansion
- func (e SubstringExpansion) Expand(env Env) (string, error)
- func (e SubstringExpansion) Identifiers() []string
- type UnsetValueExpansion
- func (e UnsetValueExpansion) Expand(env Env) (string, error)
- func (e UnsetValueExpansion) Identifiers() []string
- type VariableExpansion
Examples ¶
Functions ¶
func Identifiers ¶
Indentifiers parses the identifiers from any expansions in the provided string
func Interpolate ¶
Interpolate takes a set of environment and interpolates it into the provided string using shell
script expansions
Code:play
Output: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)
}
Hello... James welcome to the 🏖
Types ¶
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) Expand ¶
func (e EmptyValueExpansion) Expand(env Env) (string, error)
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.
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 ¶
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)
func (Expression) Identifiers ¶
func (e Expression) Identifiers() []string
type ExpressionItem ¶
ExpressionItem models either an Expansion or Text. Either/Or, never both.
func (ExpressionItem) String ¶
func (i ExpressionItem) String() 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 NewParser ¶
NewParser returns a new instance of a Parser
func (*Parser) Parse ¶
func (p *Parser) Parse() (Expression, error)
Parse expansions out of the internal text and return them as a tree of Expressions
type RequiredExpansion ¶
type RequiredExpansion struct { Identifier string Message Expression }
RequiredExpansion returns an env value, or an error if it is unset
func (RequiredExpansion) Expand ¶
func (e RequiredExpansion) Expand(env Env) (string, error)
func (RequiredExpansion) Identifiers ¶
func (e RequiredExpansion) Identifiers() []string
type SubstringExpansion ¶
SubstringExpansion returns a substring (or slice) of the env
func (SubstringExpansion) Expand ¶
func (e SubstringExpansion) Expand(env Env) (string, error)
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) Expand ¶
func (e UnsetValueExpansion) Expand(env Env) (string, error)
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) Expand ¶
func (e VariableExpansion) Expand(env Env) (string, error)
func (VariableExpansion) Identifiers ¶
func (e VariableExpansion) Identifiers() []string
Source Files ¶
env.go interpolate.go parser.go
- Version
- v0.0.2 (latest)
- Published
- Dec 22, 2023
- Platform
- windows/amd64
- Imports
- 7 packages
- Last checked
- now –
Tools for package owners.