Documentation
¶
Overview ¶
Package config provides a configuration system that registers a global service similar to the provider extension's context services. The config store is based on the Lookup interface and can retrieve values by qualified names delimited by periods, using a "dig" algorithm to traverse hierarchical names.
Index ¶
- Constants
- Variables
- func ContextValue(v contextValue) cli.Action
- func FlagsAndArgs() cli.Action
- func Load() cli.Action
- func PrintEnv(vars ...string) cli.Action
- func Reload() cli.Action
- func SetAdditionalFile(filename ...*cli.File) cli.Action
- func SetConfigDir(diropt ...string) cli.Action
- func SetEnvValue(nv ...*cli.NameValue) cli.Action
- func SetProfile(name ...string) cli.Action
- func SetValue(nv ...*cli.NameValue) cli.Action
- func SetWorkingDir(diropt ...string) cli.Action
- func SetupWorkspace() cli.Action
- func SetupWorkspaceLink() cli.Action
- type Config
- func (c *Config) Apply(opts ...Option)
- func (c *Config) BigFloat(name any) *big.Float
- func (c *Config) BigInt(name any) *big.Int
- func (c *Config) Bool(name any) bool
- func (c *Config) Bytes(name any) []byte
- func (c *Config) Duration(name any) time.Duration
- func (c *Config) File(name any) *cli.File
- func (c *Config) FileSet(name any) *cli.FileSet
- func (c *Config) FindProfileNames(ctx context.Context) []string
- func (c *Config) Float32(name any) float32
- func (c *Config) Float64(name any) float64
- func (c *Config) Has(name any) bool
- func (c *Config) IP(name any) net.IP
- func (c *Config) Int(name any) int
- func (c *Config) Int8(name any) int8
- func (c *Config) Int16(name any) int16
- func (c *Config) Int32(name any) int32
- func (c *Config) Int64(name any) int64
- func (c *Config) Interface(name any) (any, bool)
- func (c *Config) List(name any) []string
- func (c *Config) Load(ctx context.Context) (Store, error)
- func (c *Config) Map(name any) map[string]string
- func (c *Config) NameValue(name any) *cli.NameValue
- func (c *Config) NameValues(name any) []*cli.NameValue
- func (c *Config) Paths(ctx context.Context) ([]string, error)
- func (c *Config) Pipeline() cli.Action
- func (c *Config) Profile() string
- func (c *Config) Regexp(name any) *regexp.Regexp
- func (c *Config) Reload(ctx context.Context) error
- func (c *Config) Resolve() ([]IdiomaticLocation, error)
- func (c *Config) Store() Store
- func (c *Config) String(name any) string
- func (c *Config) URL(name any) *url.URL
- func (c *Config) Uint(name any) uint
- func (c *Config) Uint8(name any) uint8
- func (c *Config) Uint16(name any) uint16
- func (c *Config) Uint32(name any) uint32
- func (c *Config) Uint64(name any) uint64
- func (c *Config) Value(name any) any
- func (c *Config) Workspace() *Workspace
- type EnvMap
- type EnvProvider
- type IdiomaticLocation
- type IdiomaticLocationProvider
- type Layer
- type Loader
- type Location
- type Option
- func AddAdditionalFile(filename string) Option
- func WithAction(v cli.Action) Option
- func WithDefaultAction() Option
- func WithEnvValue(name string, envvar string) Option
- func WithLoader(l Loader) Option
- func WithLocation(l Location) Option
- func WithProfile(name string) Option
- func WithStore(s Store) Option
- func WithValue(name, value string) Option
- type Options
- type ReloadableStore
- type Store
- type Value
- type Values
- func (v Values) BigFloat(k any) *big.Float
- func (v Values) BigInt(k any) *big.Int
- func (v Values) Bool(k any) bool
- func (v Values) Bytes(k any) []byte
- func (v Values) Duration(k any) time.Duration
- func (v Values) File(k any) *cli.File
- func (v Values) FileSet(k any) *cli.FileSet
- func (v Values) Float32(k any) float32
- func (v Values) Float64(k any) float64
- func (v Values) Has(k any) bool
- func (v Values) IP(k any) net.IP
- func (v Values) Int(k any) int
- func (v Values) Int8(k any) int8
- func (v Values) Int16(k any) int16
- func (v Values) Int32(k any) int32
- func (v Values) Int64(k any) int64
- func (v Values) Interface(k any) (any, bool)
- func (v Values) List(k any) []string
- func (v Values) Map(k any) map[string]string
- func (v Values) NameValue(k any) *cli.NameValue
- func (v Values) NameValues(k any) []*cli.NameValue
- func (v Values) Regexp(k any) *regexp.Regexp
- func (v Values) String(k any) string
- func (v Values) URL(k any) *url.URL
- func (v Values) Uint(k any) uint
- func (v Values) Uint8(k any) uint8
- func (v Values) Uint16(k any) uint16
- func (v Values) Uint32(k any) uint32
- func (v Values) Uint64(k any) uint64
- func (v Values) Value(k any) any
- type Workspace
- func (w *Workspace) Apply(opts ...WorkspaceOption)
- func (w *Workspace) ConfigDir() string
- func (w *Workspace) Dir() string
- func (w *Workspace) Env() iter.Seq2[string, string]
- func (w *Workspace) FS() fs.FS
- func (w *Workspace) Files(diropt ...string) iter.Seq2[string, fs.DirEntry]
- func (w *Workspace) FprintEnv(out io.Writer, vars ...string) error
- func (w *Workspace) Getenv(name string) string
- func (w *Workspace) LoadFiles(diropt ...string) iter.Seq[any]
- func (w *Workspace) Pipeline() cli.Action
- func (w *Workspace) PrintEnv(vars ...string) error
- type WorkspaceFileLoaderFunc
- type WorkspaceFinder
- type WorkspaceOption
- func WithConfigDir(dir string) WorkspaceOption
- func WithEnvProvider(env EnvProvider) WorkspaceOption
- func WithFS(f fs.FS) WorkspaceOption
- func WithFileLoader(loader WorkspaceFileLoaderFunc) WorkspaceOption
- func WithFinder(finder WorkspaceFinder) WorkspaceOption
- func WithWalkDirFunc(fn fs.WalkDirFunc) WorkspaceOption
Constants ¶
const ( BigFloat = marshal.BigFloat BigInt = marshal.BigInt Bool = marshal.Bool Bytes = marshal.Bytes Duration = marshal.Duration File = marshal.File FileSet = marshal.FileSet Float32 = marshal.Float32 Float64 = marshal.Float64 Int = marshal.Int Int16 = marshal.Int16 Int32 = marshal.Int32 Int64 = marshal.Int64 Int8 = marshal.Int8 IP = marshal.IP List = marshal.List Map = marshal.Map NameValue = marshal.NameValue NameValues = marshal.NameValues Regexp = marshal.Regexp String = marshal.String Uint = marshal.Uint Uint16 = marshal.Uint16 Uint32 = marshal.Uint32 Uint64 = marshal.Uint64 Uint8 = marshal.Uint8 URL = marshal.URL )
The various types that the configuration system supports
const DefaultWorkspaceFinder defaultWorkspaceFinder = iota
DefaultWorkspaceFinder provides the default logic for finding a workspace
Variables ¶
var SkipFile = errors.New("skip this file")
SkipFile is returned by the walker function to indicate a file should not be returned during the workspace scan
Functions ¶
func ContextValue ¶
ContextValue provides an action that sets the given value into the context. The only supported type is *Workspace.
func FlagsAndArgs ¶ added in v0.14.1
FlagsAndArgs is an action which provides the default flags to the application. Despite its name, which is conventianal, this action provides no args.
func Load ¶ added in v0.15.0
Load loads the configuration, returning an error if it fails to load. Any binding that reads from the configuration store should wait until loading is completed, which ensures that the load error happens as early as possible and not in the context of a specific value being consumed.
func PrintEnv ¶ added in v0.17.0
PrintEnv prints out the environment, optionally specifying the vars to print out. When used in the Uses pipeline, it also provides useful initialization to the command it is used on.
func SetAdditionalFile ¶ added in v0.14.2
SetAdditionalFile sets an additional file in the configuration
func SetConfigDir ¶ added in v0.14.2
SetConfigDir sets the workspace config directory as an action. When it is used within the Uses pipeline, it initializes reasonable defaults for a flag and will set the directory within the workspace in the Before timing. The flag will either use the directory specified by diropt or its own value if diropt is not specified.
func SetEnvValue ¶ added in v0.15.0
SetEnvValue sets an additional value in the configuration
func SetProfile ¶ added in v0.14.1
SetProfile sets up the profile and provides reasonable defaults for initializing a flag.
func SetWorkingDir ¶
SetWorkingDir sets the current directory. It is typical within apps that work with workspaces to allow the user to set the current directory. \
func SetupWorkspace ¶
SetupWorkspace provides the action that sets up the workspace, which locks in the workspace directories.
func SetupWorkspaceLink ¶ added in v0.15.0
SetupWorkspaceLink ensures a link between the workspace and Config. It should be present in the initializer action of both. It is part of the default action for both.
Types ¶
type Config ¶ added in v0.14.1
type Config struct {
// Action specifies the action which defines the action to run when this value
// is added to a pipeline. Typically, this is an initializer set via WithDefaultAction
cli.Action
// contains filtered or unexported fields
}
Config provides the configuration system, typically retrieved from the context.
func FromContext ¶ added in v0.14.1
FromContext retrieves the configuration store from the context
func New ¶ added in v0.14.1
New creates a new configuration within the context By default, adding the Config to the pipeline adds it as a context service, which is required for most use cases, adds flags, and loads the configuration.
func (*Config) FindProfileNames ¶ added in v0.14.1
FindProfileNames detects the profiles
func (*Config) Load ¶ added in v0.15.0
Load loads the configuration if it has not been loaded yet. If it has, the error returned may be a cached result from a previous failure.
func (*Config) NameValue ¶ added in v0.14.1
NameValue obtains the value and converts it to a name-value pair
func (*Config) NameValues ¶ added in v0.14.1
NameValues obtains the value and converts it to a list of name-value pairs
func (*Config) Pipeline ¶ added in v0.14.1
Pipeline retrieves the configuration action as a pipeline
func (*Config) Reload ¶ added in v0.15.0
Reload loads the configuration or reloads it. This operation does not re-apply bindings that may have been setup.
func (*Config) Resolve ¶ added in v0.14.1
func (c *Config) Resolve() ([]IdiomaticLocation, error)
Resolve determines the locations of config in terms of idiomaitic locations, if available
func (*Config) Workspace ¶ added in v0.15.0
Workspace obtains the workspace which was encountered when a workspace was initialized. The Config and the Workspace can be initialized in either order to ensure a linkage that is returned from this method. The function SetupWorkspaceLink returns the action that ensures this link and it is available from the default action.
type EnvProvider ¶ added in v0.17.0
type EnvProvider interface {
// Environ obtains the environment variables
Environ() iter.Seq2[string, string]
}
EnvProvider provides the environment of a workspace, which is generally a set of environment variables that the workspace either depends upon or implicitly defines for its configuration behavior.
type IdiomaticLocation ¶ added in v0.14.0
type IdiomaticLocation interface {
Location
// Layer gets the layer where the location is found
Layer() Layer
// OS gets the operating system the location supports
OS() string
// Arch gets the architecture the location supports
Arch() string
}
IdiomaticLocation defines a location which exists in a particular layer
type IdiomaticLocationProvider ¶ added in v0.14.1
type IdiomaticLocationProvider interface {
Location
// Resolve determines the locations
Resolve(Options) ([]IdiomaticLocation, error)
// FindProfileNames locates available profile names
FindProfileNames(context.Context) []string
}
IdiomaticLocationProvider provides an interface for how idiomatic locations are detected
type Layer ¶ added in v0.14.0
type Layer int
Layer identifies layers in the configuration system
type Location ¶ added in v0.14.0
type Location interface {
// Paths resolves the file paths for this location
Paths(context.Context) ([]string, error)
}
Location defines a representation of configuration file locations
func ParseLocation ¶ added in v0.14.2
ParseLocation parses a location string and returns the corresponding Location implementation. The location type is determined by the suffix of the input string:
- Ending with "/" indicates a directory location (enumerates files in the directory)
- Ending with "/..." indicates a directory tree location (walks entire hierarchy)
- No special suffix indicates a file location
All locations support environment variable expansion. In addition to OS env vars, these special variables are supported:
- cli:app - the app name
- cli:cache - the user cache directory for the app
- cli:workspace - the workspace directory
- cli:workspace.config - the workspace ConfigDir directory
- GOOS - populated from runtime.GOOS
- GOARCH - populated from runtime.GOARCH
If a variable cannot be resolved (e.g., no workspace in context), the path will not be yielded from Paths.
type Option ¶ added in v0.14.1
Option defines an option for initialization of the configuration.
func AddAdditionalFile ¶ added in v0.14.2
AddAdditionalFile adds an additional file to load
func WithAction ¶ added in v0.14.1
WithAction sets the Action to use
func WithDefaultAction ¶ added in v0.14.1
func WithDefaultAction() Option
WithDefaultAction sets the default action
func WithEnvValue ¶ added in v0.15.0
WithEnvValue sets a configuration value via env var
func WithLoader ¶ added in v0.15.0
WithLoader specifies a loader that can be used to generate the store.
func WithLocation ¶ added in v0.14.1
WithLocation sets the configuration location to use
func WithProfile ¶ added in v0.14.1
WithProfile sets the profile name
type Options ¶ added in v0.14.1
type Options struct {
// Profile names the profile to load
Profile string
// AdditionalFiles specifies additional configuration files to load
AdditionalFiles []string
// Values were specified as additional values at the command line
Values []Value
}
Options provides the options for the configuration system
type ReloadableStore ¶ added in v0.15.0
type ReloadableStore interface {
Store
// Reload will reload the store in place. Generally, clients don't call
// this method directly. Use Config.Reload which will provide the appropriate
// services to the context.
Reload(context.Context) error
}
ReloadableStore provides a store which can be reloaded.
type Store ¶ added in v0.14.0
Store provides configuration storage based on the Lookup interface. It retrieves values by qualified names delimited by periods, using a "dig" algorithm to traverse hierarchical names.
func FromValues ¶ added in v0.15.0
FromValues creates a store from the specified values
type Values ¶ added in v0.15.0
Values provides a config Store which uses simple key-value pairs typed as strings. The underlying lookup automatically provides conversions.
func (Values) NameValues ¶ added in v0.15.0
type Workspace ¶
type Workspace struct {
// Action provides the action to provide when the workspace is
// added to a pipeline.
cli.Action
// contains filtered or unexported fields
}
Workspace provides the root directory of a project-specific workspace. The workspace provides two capabilities:
- The workspace finder locates the root directory of a project. By default, the workspace is found by searching up the directory hierarchy until it finds a directory which contains a configuration sentinel, typically a directory which the same name as the app with a leading period. For example, this is what Git does when it finds the .git directory.
- The workspace can load files. These files could be application files or configuration files.
func NewWorkspace ¶
func NewWorkspace(opts ...WorkspaceOption) *Workspace
NewWorkspace creates a workspace with the default action, which registers the flags and adds the workspace to the context.
func WorkspaceFromContext ¶
WorkspaceFromContext gets the Workspace from the context otherwise panics
func (*Workspace) Apply ¶
func (w *Workspace) Apply(opts ...WorkspaceOption)
Apply applies options to the workspace
func (*Workspace) ConfigDir ¶
ConfigDir gets the directory where configuration is stored. Typically, this has the same name as the app with a leading dot, e.g. $WORKSPACE_DIR/.app.
func (*Workspace) Dir ¶
Dir gets the workspace directory, which is the root of all content in the workspace. The directory is set to the current working directory at the time the app starts, sometime in the Before pipeline.
func (*Workspace) Files ¶
Files enumerates all files in the workspace which match the filters. The result contains the name of the file and a io/fs.DirEntry.
func (*Workspace) FprintEnv ¶ added in v0.17.0
FprintEnv prints the environment to the specified writer. If any variable is named then just its value is printed. However, if no variables are named then all are printed out using the syntax of shell variable assignment for the operating system.
func (*Workspace) LoadFiles ¶
LoadFiles loads all files in the workspace which match the filters. This method is good for a single pass, not dynamic workspaces where files are expected to change while the app is running. The type of the items returned from this depend upon what was set as the loader. When no loader is set, the result will be fs.File.
func (*Workspace) Pipeline ¶ added in v0.14.0
Pipeline obtains the pipeline for initializing the workspace
type WorkspaceFileLoaderFunc ¶
WorkspaceFileLoaderFunc defines the function for loading files in a workspace.
type WorkspaceFinder ¶
type WorkspaceFinder interface {
// FindWorkspacePath locates the workspace. Param cwd represents the
// the current working directory
FindWorkspacePath(c context.Context, f fs.FS, cwd string) (string, error)
}
WorkspaceFinder locates the workspace
type WorkspaceOption ¶
WorkspaceOption provides an option for setting up the Workspace. This is also a cli.Action, which means it can be used within pipelines. Typically, when used within the Action pipeline, it sets the option on the workspace. When used within the Uses pipeline, it may also configure a flag or other behavior.
func WithConfigDir ¶
func WithConfigDir(dir string) WorkspaceOption
WithConfigDir sets the workspace config directory. This can be used as an option to NewWorkspace or Workspace.Apply, but when it is, the diropt must be specified. It can also be used as an Action.
func WithEnvProvider ¶ added in v0.17.0
func WithEnvProvider(env EnvProvider) WorkspaceOption
WithEnvProvider sets the environment provider on the workspace
func WithFS ¶
func WithFS(f fs.FS) WorkspaceOption
WithFS specifies the file system to use for the workspace. By default, the workspace with use os.DirFS corresponding to the workspace directory.
func WithFileLoader ¶
func WithFileLoader(loader WorkspaceFileLoaderFunc) WorkspaceOption
WithFileLoader is an option that determines how to read files in a workspace.
func WithFinder ¶
func WithFinder(finder WorkspaceFinder) WorkspaceOption
WithFinder is an option that determines how to finder the workspace
func WithWalkDirFunc ¶
func WithWalkDirFunc(fn fs.WalkDirFunc) WorkspaceOption
WithWalkDirFunc specifies the function that walks the directory for files. The function is used in a call to io/fs.WalkDir, where it can return io/fs.SkipDir and io/fs.SkipAll, with the same behavior. It can also return SkipFile introduced by this package to indicate that a file is skipped in the result of the Workspace.Files and Workspace.LoadFiles methods.