package kvfile

import "github.com/docker/cli/pkg/kvfile"

Package kvfile provides utilities to parse line-delimited key/value files such as used for label-files and env-files.

File format

key/value files use the following syntax:

Interpolation, substitution, and escaping

Both keys and values are used as-is; no interpolation, substitution or escaping is supported, and quotes are considered part of the key or value. Whitespace in values (including leading and trailing) is preserved. Given that the file format is line-delimited, neither key, nor value, can contain newlines.

Key/Value pairs

Key/Value pairs take the following format:

KEY[=<VALUE>]

KEY is required and may not contain whitespaces or NUL characters. Any other character (except for the "=" delimiter) are accepted, but it is recommended to use a subset of the POSIX portable character set, as outlined in Environment Variables.

VALUE is optional, but may be empty. If no value is provided (i.e., no equal sign ("=") is present), the KEY is omitted in the result, but some functions accept a lookup-function to provide a default value for the given key.

Index

Functions

func Parse

func Parse(filename string, lookupFn func(key string) (value string, found bool)) ([]string, error)

Parse parses a line-delimited key/value pairs separated by equal sign. It accepts a lookupFn to lookup default values for keys that do not define a value. An error is produced if parsing failed, the content contains invalid UTF-8 characters, or a key contains whitespaces.

func ParseFromReader

func ParseFromReader(r io.Reader, lookupFn func(key string) (value string, found bool)) ([]string, error)

ParseFromReader parses a line-delimited key/value pairs separated by equal sign. It accepts a lookupFn to lookup default values for keys that do not define a value. An error is produced if parsing failed, the content contains invalid UTF-8 characters, or a key contains whitespaces.

Source Files

kvfile.go

Version
v28.1.1+incompatible (latest)
Published
Apr 18, 2025
Platform
linux/amd64
Imports
8 packages
Last checked
1 hour ago

Tools for package owners.