Documentation
¶
Overview ¶
Package envinput resolves configuration values ("inputs") passed to a container or process through environment variables or files.
For an input named "api_token" the value is looked up in this order:
- The environment variable INPUT_API_TOKEN, if set.
- The file named by the environment variable INPUT_API_TOKEN_FILE.
- A file named "api_token" inside the directory named by INPUTS_DIR.
- The default value, if any.
Setting both INPUT_API_TOKEN and INPUT_API_TOKEN_FILE is an error. By default a supplied-but-empty value is treated as not supplied at all (see EmptyMode) and a single final newline is trimmed from file-based values (see NewlineMode).
Index ¶
Constants ¶
const DefaultMaxBytes = 1 << 20
DefaultMaxBytes is the file size limit applied when FilePolicy.MaxBytes is zero.
const InputsDirVar = "INPUTS_DIR"
InputsDirVar is the environment variable naming the directory that is searched for per-input files as the third resolution step.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type EmptyMode ¶
type EmptyMode string
EmptyMode controls how a supplied-but-empty value is handled. Empty values are common accidents in container deployments: `docker run -e INPUT_FOO` without a value, or compose interpolation from an unset variable, both set the variable to an empty string.
const ( // EmptyUnset treats an empty value as if the input were not supplied, // falling back to the default. This is the behavior of the zero value. EmptyUnset EmptyMode = "unset" // EmptyValue accepts an empty value as a value like any other. EmptyValue EmptyMode = "value" // EmptyError rejects an empty value with an error. EmptyError EmptyMode = "error" )
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error is returned for invalid input values and misconfigured inputs. Errors from the operating system (for example a missing or unreadable file) are returned as-is instead.
type FilePolicy ¶
type FilePolicy struct {
// MaxBytes caps the file size. Zero or negative means DefaultMaxBytes.
MaxBytes int64
// Newline controls whether a single final newline is trimmed (the
// default) or kept.
Newline NewlineMode
// AllowedRoots, when non-empty, restricts input files to descendants of
// the listed directories (for example /run/secrets). The file path is
// fully resolved — symbolic links included — before the containment
// check, so links cannot escape a root. Roots that do not exist are
// ignored.
AllowedRoots []string
// RejectSymlinks refuses paths whose final component is a symbolic
// link. Note that Kubernetes secret mounts present files as symlinks,
// so this must stay off for those.
RejectSymlinks bool
}
FilePolicy configures how file-based values (INPUT_<NAME>_FILE and INPUTS_DIR) are loaded. The zero value reads regular files of up to DefaultMaxBytes anywhere on the filesystem, requires their contents to be valid UTF-8, and trims a single final newline.
type LookupFunc ¶
LookupFunc looks up an environment variable, reporting whether it is set. os.LookupEnv satisfies this signature.
type Name ¶
type Name struct {
// contains filtered or unexported fields
}
Name is a validated input name. The zero value is not usable; construct names with ParseName or MustParseName.
A name must match [_a-zA-Z][_a-zA-Z0-9]* and must not end in "_file" (in any case combination): the INPUT_<NAME>_FILE environment variable is reserved for pointing an input at a file, so a logical input named "token_file" would collide with the file variant of "token". Inputs whose value is a file path should use a "_path" suffix instead.
func MustParseName ¶
MustParseName is ParseName, panicking on invalid names. It is intended for names known at compile time.
func (Name) EnvironmentKey ¶
EnvironmentKey returns the environment variable holding the input's value: "INPUT_" followed by the uppercased name. It returns "" for the zero Name.
func (Name) FileEnvironmentKey ¶
FileEnvironmentKey returns the environment variable naming the file the input's value is read from: EnvironmentKey plus a "_FILE" suffix. It returns "" for the zero Name.
type NewlineMode ¶
type NewlineMode string
NewlineMode controls how a final newline in file-based values is handled. It never applies to values from environment variables.
const ( // NewlineTrim removes a single trailing "\n" or "\r\n". This is the // behavior of the zero value: files written by editors, echo, or // secret managers almost always carry a final newline that is not part // of the value. NewlineTrim NewlineMode = "trim" // NewlineKeep keeps the file contents exactly as read. NewlineKeep NewlineMode = "keep" )
type Resolved ¶
type Resolved[T any] struct { // Value holds the parsed value. For the dynamically typed Resolve, T is // any: string for TypeString, bool for TypeBoolean, int64 for // TypeInteger, the result of unmarshalling (numbers as json.Number) for // TypeJSON, the unparsed Spec.Default when Source is SourceDefault, and // nil when an optional input was not supplied and had no default. For // ResolveAs[T] it is a T (the zero T when an optional input was not // supplied and had no default). Value T // Source reports where the value came from. Source Source // Secret is copied from the spec. Secret bool }
Resolved is the result of resolving an input.
func Resolve ¶
Resolve resolves a dynamically typed input against the process environment: Resolved.Value holds the Go type corresponding to Spec.Type. Use ResolveAs when the wanted type is known at compile time.
func ResolveAs ¶
ResolveAs resolves an input against the process environment, parsed into T. The spec's Type field is not needed: string, bool, int and int64 select their obvious parsers, and any other T — including structs, maps, and any itself — is unmarshalled from JSON.
func ResolveAsWith ¶
func ResolveAsWith[T any](spec Spec, lookup LookupFunc) (Resolved[T], error)
ResolveAsWith is ResolveAs with a custom environment lookup.
func ResolveWith ¶
func ResolveWith(spec Spec, lookup LookupFunc) (Resolved[any], error)
ResolveWith is Resolve with a custom environment lookup. Files referenced by the environment are still read from the real filesystem.
type Source ¶
type Source string
Source reports where a resolved input value came from.
func LookupSource ¶
LookupSource reports the source Resolve would read the input from, without reading or parsing the value. It returns ok == false when the input is not supplied and resolution would fall back to the default. Because the value is not read, EmptyMode does not apply: an input supplied as an empty string or an empty file still reports ok == true.
func LookupSourceWith ¶
func LookupSourceWith(name Name, lookup LookupFunc) (Source, bool, error)
LookupSourceWith is LookupSource with a custom environment lookup.
type Spec ¶
type Spec struct {
// Name is the validated input name; see ParseName.
Name Name
// Type selects how the raw value is parsed by Resolve. The zero value
// means TypeString. ResolveAs ignores it (the type parameter decides)
// but rejects a Type that contradicts the type parameter.
Type Type
// Required makes resolution fail when the input is not supplied and no
// Default is set.
Required bool
// Default is used when the input is not supplied. Resolve returns it
// as-is, without parsing; ResolveAs requires it to be assignable to the
// type parameter. A nil Default means the input has no default.
Default any
// Secret marks the input as sensitive: error messages will not contain
// the value, and Resolved.String prints a placeholder instead of it.
Secret bool
// Validate, when set, is called with the parsed value — or with the
// default, when the default is used — and can reject it by returning an
// error. The error is reported with the input name prepended. For
// secret inputs the hook must not include the value in its errors;
// envinput cannot redact them.
Validate func(value any) error
// Empty controls how a supplied-but-empty value is handled.
Empty EmptyMode
// File configures how file-based values (INPUT_<NAME>_FILE and
// INPUTS_DIR) are loaded.
File FilePolicy
}
Spec describes a single input to resolve.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
envinput
command
Command envinput resolves container inputs from environment variables, files, or an inputs directory.
|
Command envinput resolves container inputs from environment variables, files, or an inputs directory. |
|
Package manifest loads YAML manifests that declare the inputs a container expects, and checks all of them against the environment at once — typically at container start, so every misconfigured input is reported in a single pass instead of one failure at a time.
|
Package manifest loads YAML manifests that declare the inputs a container expects, and checks all of them against the environment at once — typically at container start, so every misconfigured input is reported in a single pass instead of one failure at a time. |