ring

package
v0.5.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 15, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func EnvGet(env []string, key string) string

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

func EnvGetDefault(env []string, key, def string) string

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

func EnvLookup(env []string, key string) (string, bool)

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

func EnvOrOs(env []string) []string

EnvOrOs returns "env" if it's not nil, otherwise returns os.Environ.

func EnvSet

func EnvSet(env []string, key, val string) []string

EnvSet sets a single environment variable. Returns the modified slice.

func EnvSplit

func EnvSplit(env []string) map[string]string

EnvSplit parses os.Environ results and returns it as a key value map.

func EnvUnset

func EnvUnset(env []string, key string) []string

EnvUnset unsets a single environment variable. Returns the modified slice.

func NowUTC

func NowUTC() time.Time

NowUTC returns the current time in UTC, equivalent to time.Now().UTC().

func SetFrom

func SetFrom(env []string, src map[string]string) []string

SetFrom sets environment variables from src map. Always returns a new slice.

Types

type Clock

type Clock func() time.Time

Clock defines a function signature that returns the current time in UTC.

type Env

type Env struct {
	// contains filtered or unexported fields
}

Env implements Environ, storing environment variables.

func NewEnv

func NewEnv(env []string) *Env

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

func (env *Env) EnvAll() []string

EnvAll returns environment as a slice of "key=value" entries. It returns nil when the environment is empty.

func (*Env) EnvClone added in v0.4.0

func (env *Env) EnvClone() *Env

EnvClone returns a clone of the environment.

func (*Env) EnvGet

func (env *Env) EnvGet(key string) string

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

func (env *Env) EnvLookup(key string) (string, bool)

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) EnvSet

func (env *Env) EnvSet(key, value string)

EnvSet sets the environment variable named by the key to the given value.

func (*Env) EnvSetFrom

func (env *Env) EnvSetFrom(src map[string]string)

EnvSetFrom sets multiple environment variables from the given map. Overwrites existing variables with the same key.

func (*Env) EnvSetWith added in v0.4.0

func (env *Env) EnvSetWith(src []string)

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.

func (*Env) EnvUnset

func (env *Env) EnvUnset(key string)

EnvUnset unsets a single environment variable.

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

func (ios *IO) IOClone() *IO

IOClone creates a copy of the current IO instance with identical streams.

func (*IO) SetStderr

func (ios *IO) SetStderr(eout io.Writer)

SetStderr returns IO with the given standard error.

func (*IO) SetStdin

func (ios *IO) SetStdin(sin io.Reader)

SetStdin returns IO with the given standard input.

func (*IO) SetStdout

func (ios *IO) SetStdout(sout io.Writer)

SetStdout returns IO with the given standard output.

func (*IO) Stderr

func (ios *IO) Stderr() io.Writer

Stderr returns the standard error to use for a program.

func (*IO) Stdin

func (ios *IO) Stdin() io.Reader

Stdin returns the standard input to use for a program.

func (*IO) Stdout

func (ios *IO) Stdout() io.Writer

Stdout returns the standard output to use for a program.

type Option

type Option func(*Ring)

Option configures a Ring during creation with New.

func WithArgs

func WithArgs(args []string) Option

WithArgs configures a Ring with the given program arguments (excluding the program name).

func WithClock

func WithClock(clk Clock) Option

WithClock configures a Ring with a custom Clock function for time.

func WithEnv

func WithEnv(env []string) Option

WithEnv configures a Ring with the given environment variables.

func WithFS added in v0.5.0

func WithFS(filesystem fs.FS) Option

WithFS configures a Ring with access to read-only filesystem.

func WithMeta

func WithMeta(meta map[string]any) Option

WithMeta configures a Ring with the given metadata.

func WithName

func WithName(name string) Option

WithName configures a Ring with the given 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

func New(opts ...Option) *Ring

New creates a new Ring with the provided options.

If no options are specified, it defaults to:

Example:

rng := New(
  WithEnv([]string{"KEY=value"}),
  WithArgs([]string{"-a", "arg1"}),
)

func (*Ring) Args

func (rng *Ring) Args() []string

Args returns the program arguments, excluding the program name.

func (*Ring) Clock

func (rng *Ring) Clock() func() time.Time

Clock returns function returning current time in UTC.

func (*Ring) Clone added in v0.4.0

func (rng *Ring) Clone() *Ring

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

func (rng *Ring) FS() (fs.FS, error)

FS returns a hierarchical file system associated with the instance.

func (*Ring) MetaAll

func (rng *Ring) MetaAll() map[string]any

MetaAll returns metadata map.

func (*Ring) MetaDelete

func (rng *Ring) MetaDelete(key string)

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

func (rng *Ring) MetaGet(key string) any

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

func (rng *Ring) MetaLookup(key string) (any, bool)

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.

func (*Ring) MetaSet

func (rng *Ring) MetaSet(key string, value any)

MetaSet sets the metadata value for the given key. If the key already exists, its value is overwritten. The value may be any type, including nil.

func (*Ring) Name

func (rng *Ring) Name() string

Name returns program name.

func (*Ring) SetArgs added in v0.3.0

func (rng *Ring) SetArgs(args []string) *Ring

SetArgs sets the program arguments, excluding the program name.

type Streamer

type Streamer interface {
	Stdin() io.Reader  // Standard input.
	Stdout() io.Writer // Standard output.
	Stderr() io.Writer // Standard error.
}

Streamer defines an interface for accessing a program's standard I/O streams.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL