/interpolate

Interpolate $STRINGS in ${OTHER_STRINGS:-true}

Primary LanguageGoOtherNOASSERTION

Interpolate

GoDoc

A golang library for parameter expansion (like ${BLAH} or $BLAH) in strings from environment variables. An implementation of POSIX Parameter Expansion, plus some other basic operations that you'd expect in a shell scripting environment like bash.

Installation

go get -u github.com/buildkite/interpolate

Usage

package main

import (
  "github.com/buildkite/interpolate"
  "fmt"
)

func main() {
	env := interpolate.NewSliceEnv([]string{
		"HELLO_WORLD=🦀",
	})

	output, _ := interpolate.Interpolate(env, "Buildkite... ${HELLO_WORLD} ${ANOTHER_VAR:-🏖}")
	fmt.Println(output)
}

// Output: Buildkite... 🦀 🏖

Supported Expansions

${parameter} or $parameter
Use value. If parameter is set, then it shall be substituted; otherwise it will be blank
${parameter:-[word]}
Use default values. If parameter is unset or null, the expansion of word (or an empty string if word is omitted) shall be substituted; otherwise, the value of parameter shall be substituted.
${parameter-[word]}
Use default values when not set. If parameter is unset, the expansion of word (or an empty string if word is omitted) shall be substituted; otherwise, the value of parameter shall be substituted.
${parameter:[offset]}
Use the substring of parameter after offset. A negative offset must be separated from the colon with a space, and will select from the end of the string. If the value is out of bounds, an empty string will be substituted.
${parameter:[offset]:[length]}
Use the substring of parameter after offset of given length. A negative offset must be separated from the colon with a space, and will select from the end of the string. If the offset is out of bounds, an empty string will be substituted. If the length is greater than the length then the entire string will be returned.
${parameter:?[word]}
Indicate Error if Null or Unset. If parameter is unset or null, the expansion of word (or a message indicating it is unset if word is omitted) shall be returned as an error.

License

Licensed under MIT license, in LICENSE.