Documentation
¶
Overview ¶
Package environment provides environment related functions, options, and logging for os-level capabilities.
Example ¶
package main
import (
"context"
"log/slog"
"os"
"github.com/x-ethr/environment"
)
func main() {
ctx, level := context.Background(), slog.LevelInfo
// Log all environment variables
environment.Log(ctx, level)
// Log only specific environment variable(s)
environment.Log(ctx, level, func(o *environment.Options) {
o.Variables = []string{
"PATH",
}
})
// Log only specific environment variable(s), and warn if one of the specified variable(s) was set to an empty string
environment.Log(ctx, level, func(o *environment.Options) {
_ = os.Setenv("TEST", "") // --> for example purposes
o.Variables = []string{
"TEST",
}
o.Warnings.Empty = true
})
// Log only specific environment variable(s), and warn if one of the specified variable(s) wasn't found
environment.Log(ctx, level, func(o *environment.Options) {
_ = os.Unsetenv("TEST") // --> for example purposes
o.Variables = []string{
"TEST",
}
o.Warnings.Missing = true
})
}
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Options ¶
type Options struct {
Variables []string // Variables represents an array of environment variables (as returned by [os.Environ]), to selectively log.
Warnings *Warnings // Warnings represents logging options relating to [slog.Warn] logs. Defaults to a non-nil [Warnings] reference with all attributes set to false.
}
Options is the configuration structure optionally mutated via the Variadic constructor used throughout the package.
type Variadic ¶
type Variadic func(o *Options)
Variadic represents a functional constructor for the Options type. Typical callers of Variadic won't need to perform nil checks as all implementations first construct an Options reference using packaged default(s).
type Warnings ¶
type Warnings struct {
Empty bool // Empty represents a logging option to warn if a given environment variable is set to any empty string. Requires [Options.Variables]. Defaults to false.
Missing bool // Missing represents a logging option to warn if a given environment variable isn't found. Requires [Options.Variables]. Defaults to false.
}
Click to show internal directories.
Click to hide internal directories.