config

package
v0.14.2 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: BSD-3-Clause Imports: 19 Imported by: 0

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

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

Variables

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

func ContextValue(v contextValue) cli.Action

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

func FlagsAndArgs() cli.Action

FlagsAndArgs is an action which provides the default flags to the application. Despite its name, which is conventianal, this action provides no args.

func SetAdditionalFile added in v0.14.2

func SetAdditionalFile(filename ...*cli.File) cli.Action

SetAdditionalFile sets an additional file in the configuration

func SetConfigDir added in v0.14.2

func SetConfigDir(diropt ...string) cli.Action

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 SetProfile added in v0.14.1

func SetProfile(name ...string) cli.Action

SetProfile sets up the profile and provides reasonable defaults for initializing a flag.

func SetWorkingDir

func SetWorkingDir(diropt ...string) cli.Action

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

func SetupWorkspace() cli.Action

SetupWorkspace provides the action that sets up the workspace, which locks in the workspace directories.

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

func FromContext(ctx context.Context) *Config

FromContext retrieves the configuration store from the context

func New added in v0.14.1

func New(opts ...Option) *Config

New creates a new configuration within the context

func (*Config) Apply added in v0.14.1

func (c *Config) Apply(opts ...Option)

Apply will apply the given options to the config

func (*Config) BigFloat added in v0.14.1

func (c *Config) BigFloat(name any) *big.Float

BigFloat obtains the BigFloat for the specified name

func (*Config) BigInt added in v0.14.1

func (c *Config) BigInt(name any) *big.Int

BigInt obtains the BigInt for the specified name

func (*Config) Bool added in v0.14.1

func (c *Config) Bool(name any) bool

Bool obtains the value and converts it to a bool

func (*Config) Bytes added in v0.14.1

func (c *Config) Bytes(name any) []byte

Bytes obtains the bytes for the specified name

func (*Config) Duration added in v0.14.1

func (c *Config) Duration(name any) time.Duration

Duration obtains the Duration for the specified name

func (*Config) File added in v0.14.1

func (c *Config) File(name any) *cli.File

File obtains the File for the specified name

func (*Config) FileSet added in v0.14.1

func (c *Config) FileSet(name any) *cli.FileSet

FileSet obtains the FileSet for the specified name

func (*Config) FindProfileNames added in v0.14.1

func (c *Config) FindProfileNames(ctx context.Context) []string

FindProfileNames detects the profiles

func (*Config) Float32 added in v0.14.1

func (c *Config) Float32(name any) float32

Float32 obtains the float32 for the specified name

func (*Config) Float64 added in v0.14.1

func (c *Config) Float64(name any) float64

Float64 obtains the float64 for the specified name

func (*Config) Has added in v0.14.1

func (c *Config) Has(name any) bool

Has determines whether the configuration value exists

func (*Config) IP added in v0.14.1

func (c *Config) IP(name any) net.IP

IP obtains the IP for the specified name

func (*Config) Int added in v0.14.1

func (c *Config) Int(name any) int

Int obtains the int for the specified name

func (*Config) Int8 added in v0.14.1

func (c *Config) Int8(name any) int8

Int8 obtains the int8 for the specified name

func (*Config) Int16 added in v0.14.1

func (c *Config) Int16(name any) int16

Int16 obtains the int16 for the specified name

func (*Config) Int32 added in v0.14.1

func (c *Config) Int32(name any) int32

Int32 obtains the int32 for the specified name

func (*Config) Int64 added in v0.14.1

func (c *Config) Int64(name any) int64

Int64 obtains the int64 for the specified name

func (*Config) Interface added in v0.14.1

func (c *Config) Interface(name any) (any, bool)

Interface obtains the raw value without dereferencing

func (*Config) List added in v0.14.1

func (c *Config) List(name any) []string

List obtains the value and converts it to a string slice

func (*Config) Map added in v0.14.1

func (c *Config) Map(name any) map[string]string

Map obtains the map for the specified name

func (*Config) NameValue added in v0.14.1

func (c *Config) NameValue(name any) *cli.NameValue

NameValue obtains the value and converts it to a name-value pair

func (*Config) NameValues added in v0.14.1

func (c *Config) NameValues(name any) []*cli.NameValue

NameValues obtains the value and converts it to a list of name-value pairs

func (*Config) Paths added in v0.14.1

func (c *Config) Paths(ctx context.Context) ([]string, error)

Paths resolves the file paths for config

func (*Config) Pipeline added in v0.14.1

func (c *Config) Pipeline() cli.Action

Pipeline retrieves the configuration action as a pipeline

func (*Config) Profile added in v0.14.1

func (c *Config) Profile() string

Profile retrieves the profile name

func (*Config) Regexp added in v0.14.1

func (c *Config) Regexp(name any) *regexp.Regexp

Regexp obtains the Regexp for the specified name

func (*Config) Resolve added in v0.14.1

func (c *Config) Resolve(opts Options) ([]IdiomaticLocation, error)

Resolve determines the locations of config in terms of idiomaitic locations, if available

func (*Config) Store added in v0.14.1

func (c *Config) Store() Store

Store provides the configuration store

func (*Config) String added in v0.14.1

func (c *Config) String(name any) string

String obtains the value and converts it to a string

func (*Config) URL added in v0.14.1

func (c *Config) URL(name any) *url.URL

URL obtains the URL for the specified name

func (*Config) Uint added in v0.14.1

func (c *Config) Uint(name any) uint

Uint obtains the uint for the specified name

func (*Config) Uint8 added in v0.14.1

func (c *Config) Uint8(name any) uint8

Uint8 obtains the uint8 for the specified name

func (*Config) Uint16 added in v0.14.1

func (c *Config) Uint16(name any) uint16

Uint16 obtains the uint16 for the specified name

func (*Config) Uint32 added in v0.14.1

func (c *Config) Uint32(name any) uint32

Uint32 obtains the uint32 for the specified name

func (*Config) Uint64 added in v0.14.1

func (c *Config) Uint64(name any) uint64

Uint64 obtains the uint64 for the specified name

func (*Config) Value added in v0.14.1

func (c *Config) Value(name any) any

Value obtains the value and converts it to Value

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

const (
	LayerUnspecified Layer = -1
	LayerIntrinsic   Layer = 0
	LayerSystem      Layer = 2
	LayerUser        Layer = 4
	LayerWorkspace   Layer = 6
	LayerProfile     Layer = 8
	LayerAdditional  Layer = 10
)

Layers where a location exists

func (Layer) String added in v0.14.0

func (l Layer) String() string

type Loader added in v0.14.0

type Loader interface {
	Load(context.Context) (Store, error)
}

Loader loads 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

func ParseLocation(s string) Location

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: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

type Option interface {
	cli.Action
	// contains filtered or unexported methods
}

Option defines an option for initialization of the configuration.

func AddAdditionalFile added in v0.14.2

func AddAdditionalFile(filename string) Option

AddAdditionalFile adds an additional file to load

func WithAction added in v0.14.1

func WithAction(v cli.Action) Option

WithAction sets the Action to use

func WithDefaultAction added in v0.14.1

func WithDefaultAction() Option

WithDefaultAction sets the default action

func WithLocation added in v0.14.1

func WithLocation(l Location) Option

WithLocation sets the configuration location to use

func WithProfile added in v0.14.1

func WithProfile(name string) Option

WithProfile sets the profile name

func WithStore added in v0.14.1

func WithStore(s Store) Option

WithStore sets the configuration store to use

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
}

Options provides the options for the configuration system

type Store added in v0.14.0

type Store interface {
	cli.Lookup
	Has(name any) bool
}

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.

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:

  1. 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.
  2. 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

func WorkspaceFromContext(ctx context.Context) *Workspace

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

func (w *Workspace) ConfigDir() string

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

func (w *Workspace) Dir() string

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

func (w *Workspace) FS() fs.FS

FS gets the file system that represents the workspace.

func (*Workspace) Files

func (w *Workspace) Files(diropt ...string) iter.Seq2[string, fs.DirEntry]

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

func (w *Workspace) LoadFiles(diropt ...string) iter.Seq[any]

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

func (w *Workspace) Pipeline() cli.Action

Pipeline obtains the pipeline for initializing the workspace

type WorkspaceFileLoaderFunc

type WorkspaceFileLoaderFunc func(root fs.FS, name string, d fs.DirEntry) (any, error)

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

var DefaultWorkspaceFinder WorkspaceFinder = new(defaultWorkspaceFinder)

DefaultWorkspaceFinder provides the default logic for finding a workspace

type WorkspaceOption

type WorkspaceOption interface {
	cli.Action
	// contains filtered or unexported methods
}

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 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.

Jump to

Keyboard shortcuts

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