Documentation
¶
Index ¶
- Variables
- func EnvGet(env []string, key string) string
- func EnvGetDefault(env []string, key, def string) string
- func EnvLookup(env []string, key string) (string, bool)
- func EnvOrOs(env []string) []string
- func EnvSet(env []string, key, val string) []string
- func EnvSplit(env []string) map[string]string
- func EnvUnset(env []string, key string) []string
- func NowUTC() time.Time
- func SetFrom(env []string, src map[string]string) []string
- type Clock
- type Env
- func (env *Env) EnvAll() []string
- func (env *Env) EnvClone() *Env
- func (env *Env) EnvGet(key string) string
- func (env *Env) EnvLookup(key string) (string, bool)
- func (env *Env) EnvSet(key, value string)
- func (env *Env) EnvSetFrom(src map[string]string)
- func (env *Env) EnvSetWith(src []string)
- func (env *Env) EnvUnset(key string)
- type Environ
- type IO
- type Option
- type Ring
- func (rng *Ring) Args() []string
- func (rng *Ring) Clock() func() time.Time
- func (rng *Ring) Clone() *Ring
- func (rng *Ring) FS() (fs.FS, error)
- func (rng *Ring) MetaAll() map[string]any
- func (rng *Ring) MetaDelete(key string)
- func (rng *Ring) MetaGet(key string) any
- func (rng *Ring) MetaLookup(key string) (any, bool)
- func (rng *Ring) MetaSet(key string, value any)
- func (rng *Ring) Name() string
- func (rng *Ring) SetArgs(args []string) *Ring
- type Streamer
Constants ¶
This section is empty.
Variables ¶
var ( // ErrReqMeta indicates a required metadata key is missing. ErrReqMeta = errors.New("required ring metadata key") // ErrInvMeta indicates a metadata key is invalid due to an incorrect type, // format, or value. ErrInvMeta = errors.New("invalid ring metadata key") // ErrNoFsAccess is returned when [Ring] has no filesystem access. ErrNoFsAccess = errors.New("no filesystem access") )
Sentinel errors.
Functions ¶
func EnvGet ¶
EnvGet retrieves the value of the "env" variable named by the key. It returns the value, which will be empty if the variable is not present. To distinguish between an empty value and an unset value, use EnvLookup.
func EnvGetDefault ¶
EnvGetDefault retrieves the value of the "env" variable named by the key. If the key is not present in the environment, it will return def value.
func EnvLookup ¶
EnvLookup retrieves the value of the "env" variable named by the key. If the variable is present in the "env", the value (which may be empty) is returned and the boolean is true. Otherwise, the returned value will be empty and the boolean will be false.
func EnvOrOs ¶
EnvOrOs returns "env" if it's not nil, otherwise returns os.Environ.
func EnvSplit ¶
EnvSplit parses os.Environ results and returns it as a key value map.
Types ¶
type Env ¶
type Env struct {
// contains filtered or unexported fields
}
Env implements Environ, storing environment variables.
func NewEnv ¶
NewEnv creates a new Env initialized with the given environment variables. If env is nil, an empty map is allocated. The input slice should contain "key=value" strings, as produced by os.Environ.
func (*Env) EnvAll ¶
EnvAll returns environment as a slice of "key=value" entries. It returns nil when the environment is empty.
func (*Env) EnvGet ¶
EnvGet retrieves the value of the environment variable named by the key from the given env slice. Returns the value or an empty string if not set. To distinguish between an empty value and an unset value, use Env.EnvLookup.
func (*Env) EnvLookup ¶
EnvLookup retrieves the value of the environment variable named by the key from the given env slice. Returns the value (which may be empty) and true if the variable exists, or an empty string and false if it does not.
func (*Env) EnvSetFrom ¶
EnvSetFrom sets multiple environment variables from the given map. Overwrites existing variables with the same key.
func (*Env) EnvSetWith ¶ added in v0.4.0
EnvSetWith sets multiple environment variables from the given slice. The input slice should contain "key=value" strings, as produced by os.Environ. Overwrites existing variables with the same key.
type Environ ¶
type Environ interface {
// EnvLookup retrieves the value of the variable named by the key. If the
// variable is present in the environment, the value (which may be empty)
// is returned and the boolean is true. Otherwise, the returned value will
// be empty and the boolean will be false.
EnvLookup(key string) (string, bool)
// EnvGet retrieves the value of the variable named by the key. It returns
// the value, which will be empty if the variable is not present. To
// distinguish between an empty value and an unset value, use
// [Environ.EnvLookup].
EnvGet(key string) string
// EnvSet sets variable.
EnvSet(key, value string)
// EnvUnset unsets a single environment variable.
EnvUnset(key string)
// EnvAll returns environment as a slice of "key=value" entries. It
// returns nil when the environment is empty.
EnvAll() []string
}
Environ defines an interface for managing environment variables.
type IO ¶
type IO struct {
// contains filtered or unexported fields
}
IO represents program standard I/O streams.
func NewIO ¶
func NewIO() *IO
NewIO returns a new instance of the IO struct with os.Stdin, os.Stdout, and os.Stderr as default values for the stdin, stdout, and stderr fields respectively.
func (*IO) IOClone ¶ added in v0.4.0
IOClone creates a copy of the current IO instance with identical streams.
type Option ¶
type Option func(*Ring)
Option configures a Ring during creation with New.
func WithArgs ¶
WithArgs configures a Ring with the given program arguments (excluding the program name).
type Ring ¶
type Ring struct {
// contains filtered or unexported fields
}
Ring represents a program execution context, encapsulating standard I/O streams, environment variables, arguments, a clock, and metadata.
func New ¶
New creates a new Ring with the provided options.
If no options are specified, it defaults to:
- Standard I/O: os.Stdin, os.Stdout, os.Stderr
- Environment: os.Environ
- Clock: NowUTC
- Args: os.Args[1:]
- Name: os.Args[0]
- Metadata: empty map
- Filesystem: no access.
Example:
rng := New(
WithEnv([]string{"KEY=value"}),
WithArgs([]string{"-a", "arg1"}),
)
func (*Ring) Clone ¶ added in v0.4.0
Clone creates a deep copy of the Ring instance (except metadata structure).
Changes to metadata will be visible in all clones.
func (*Ring) FS ¶ added in v0.5.0
FS returns a hierarchical file system associated with the instance.
func (*Ring) MetaDelete ¶
MetaDelete removes the metadata value associated with the given key. If the key does not exist, the method has no effect.
func (*Ring) MetaGet ¶ added in v0.2.0
MetaGet retrieves the metadata value associated with the given key. If the key exists, it returns the value, which may be nil or empty. If the key does not exist, it returns nil.
func (*Ring) MetaLookup ¶ added in v0.2.0
MetaLookup retrieves the metadata value associated with the given key. If the key exists in the metadata, it returns the value (which may be nil or empty) and true. If the key does not exist, it returns nil and false.