Documentation ¶
Overview ¶
SPDX-License-Identifier: BSD-3-Clause Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors. Licensed under the BSD-3-Clause License (the "License"). You may not use this file expect in compliance with the License.
Index ¶
- Constants
- Variables
- func AllowedValues(key string) []string
- func ConfigDir() string
- func ConfigFile() string
- func DataDir() string
- func Default(key string) string
- func Defaults() map[string]string
- func HomeDirPath(subdir string) (string, error)
- func StateDir() string
- func WithConfigManager(ctx context.Context, cfgm *ConfigManager) context.Context
- type AuthConfig
- type Config
- type ConfigDetail
- type ConfigManager
- type ConfigManagerOption
- type Feeder
- type YamlFeeder
Constants ¶
const ( KRAFTKIT_CONFIG_DIR = "KRAFTKIT_CONFIG_DIR" XDG_CONFIG_HOME = "XDG_CONFIG_HOME" XDG_STATE_HOME = "XDG_STATE_HOME" XDG_DATA_HOME = "XDG_DATA_HOME" APP_DATA = "AppData" LOCAL_APP_DATA = "LocalAppData" )
const ( DefaultNoPrompt = "false" DefaultNoParallel = "true" DefaultNoEmojis = "false" DefaultGitProtocol = "https" DefaultLogLevel = "info" DefaultLogTimestamps = "false" DefaultLogType = "fancy" DefaultRuntimeDir = "/var/kraftkit" DefaultEventsPidFile = "/var/kraftkit/events.pid" DefaultManifestIndex = "https://manifests.kraftkit.sh/index.yaml" )
Variables ¶
var ( // G is an alias for FromContext. // // We may want to define this locally to a package to get package tagged // config. G = ConfigFromContext // M is an alias for FromContext M = FromContext // C is the default configuration manager C, _ = NewConfigManager() )
var BackupConfigFile = func(filename string) error { return os.Rename(filename, filename+".bak") }
var ReadConfigFile = func(filename string) ([]byte, error) { f, err := os.Open(filename) if err != nil { return nil, pathError(err) } defer f.Close() data, err := ioutil.ReadAll(f) if err != nil { return nil, err } return data, nil }
var WriteConfigFile = func(filename string, data []byte) error { err := os.MkdirAll(filepath.Dir(filename), 0o771) if err != nil { return pathError(err) } cfgFile, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o600) if err != nil { return err } defer cfgFile.Close() _, err = cfgFile.Write(data) return err }
Functions ¶
func AllowedValues ¶
func ConfigDir ¶
func ConfigDir() string
Config path precedence 1. KRAFTKIT_CONFIG_DIR 2. XDG_CONFIG_HOME 3. AppData (windows only) 4. HOME
func ConfigFile ¶
func ConfigFile() string
func DataDir ¶
func DataDir() string
Data path precedence 1. XDG_DATA_HOME 2. LocalAppData (windows only) 3. HOME
func HomeDirPath ¶
func StateDir ¶
func StateDir() string
State path precedence 1. XDG_STATE_HOME 2. LocalAppData (windows only) 3. HOME
func WithConfigManager ¶ added in v0.2.0
func WithConfigManager(ctx context.Context, cfgm *ConfigManager) context.Context
WithConfigManager returns a new context with the provided logger. Use in combination with logger.WithField(s) for great effect.
Types ¶
type AuthConfig ¶
type AuthConfig struct { User string `json:"user" yaml:"user" env:"KRAFTKIT_AUTH_%s_USER"` Token string `json:"token" yaml:"token" env:"KRAFTKIT_AUTH_%s_TOKEN"` Endpoint string `json:"endpoint" yaml:"endpoint" env:"KRAFTKIT_AUTH_%s_ENDPOINT"` VerifySSL bool `json:"verify_ssl" yaml:"verify_ssl" env:"KRAFTKIT_AUTH_%s_VERIFY_SSL"` }
AuthConfig represents a very abstract representation of authentication used by some service. Most APIs and services which can be authenticated have the defined four parameters found within AuthConfig.
type Config ¶
type Config struct { NoPrompt bool `json:"no_prompt" yaml:"no_prompt" env:"KRAFTKIT_NO_PROMPT"` NoParallel bool `json:"no_parallel" yaml:"no_parallel" env:"KRAFTKIT_NO_PARALLEL"` Emojis bool `json:"no_emojis" yaml:"no_emojis" env:"KRAFTKIT_NO_EMOJIS"` Editor string `json:"editor" yaml:"editor,omitempty" env:"KRAFTKIT_EDITOR"` Browser string `json:"browser" yaml:"browser,omitempty" env:"KRAFTKIT_BROWSER"` GitProtocol string `json:"git_protocol" yaml:"git_protocol" env:"KRAFTKIT_GIT_PROTOCOL"` Pager string `json:"pager" yaml:"pager,omitempty" env:"KRAFTKIT_PAGER"` HTTPUnixSocket string `json:"http_unix_socket" yaml:"http_unix_socket,omitempty" env:"KRAFTKIT_HTTP_UNIX_SOCKET"` RuntimeDir string `json:"runtime_dir" yaml:"runtime_dir" env:"KRAFTKIT_RUNTIME_DIR"` DefaultPlat string `json:"default_plat" yaml:"default_plat" env:"KRAFTKIT_DEFAULT_PLAT"` DefaultArch string `json:"default_arch" yaml:"default_arch" env:"KRAFTKIT_DEFAULT_ARCH"` EventsPidFile string `json:"events_pidfile" yaml:"events_pidfile" env:"KRAFTKIT_EVENTS_PIDFILE"` Paths struct { Plugins string `json:"plugins" yaml:"plugins,omitempty" env:"KRAFTKIT_PATHS_PLUGINS"` Config string `json:"config" yaml:"config,omitempty" env:"KRAFTKIT_PATHS_CONFIG"` Manifests string `json:"manifests" yaml:"manifests,omitempty" env:"KRAFTKIT_PATHS_MANIFESTS"` Sources string `json:"sources" yaml:"sources,omitempty" env:"KRAFTKIT_PATHS_SOURCES"` } `json:"paths" yaml:"paths,omitempty"` Log struct { Level string `json:"level" yaml:"level" env:"KRAFTKIT_LOG_LEVEL"` Timestamps bool `json:"timestamps" yaml:"timestamps" env:"KRAFTKIT_LOG_TIMESTAMPS"` Type string `json:"type" yaml:"type" env:"KRAFTKIT_LOG_TYPE"` } `json:"log" yaml:"log"` Unikraft struct { Mirrors []string `json:"mirrors" yaml:"mirrors" env:"KRAFTKIT_UNIKRAFT_MIRRORS"` Manifests []string `json:"manifests" yaml:"manifests" env:"KRAFTKIT_UNIKRAFT_MANIFESTS"` } `json:"unikraft" yaml:"unikraft"` Auth map[string]AuthConfig `json:"auth" yaml:"auth,omitempty"` Aliases map[string]map[string]string `json:"aliases" yaml:"aliases"` }
func ConfigFromContext ¶ added in v0.2.0
ConfigFromContext returns the config for kraftkit in the context, or an inert configuration that results in default values.
func NewDefaultConfig ¶
type ConfigDetail ¶
func ConfigDetails ¶
func ConfigDetails() []ConfigDetail
type ConfigManager ¶
ConfigManager uses the package facilities, there should be at least one instance of it. It holds the configuration feeders and structs.
func FromContext ¶ added in v0.2.0
func FromContext(ctx context.Context) *ConfigManager
FromContext returns the Config Manager for kraftkit in the context, or an inert configuration that results in default values.
func NewConfigManager ¶
func NewConfigManager(opts ...ConfigManagerOption) (*ConfigManager, error)
func (*ConfigManager) AddFeeder ¶
func (cm *ConfigManager) AddFeeder(f Feeder) *ConfigManager
AddFeeder adds a feeder that provides configuration data.
func (*ConfigManager) Feed ¶
func (cm *ConfigManager) Feed() error
Feed binds configuration data from added feeders to the added structs.
func (*ConfigManager) SetupListener ¶
func (cm *ConfigManager) SetupListener(fallback func(err error)) *ConfigManager
SetupListener adds an OS signal listener to the Config instance. The listener listens to the `SIGHUP` signal and refreshes the Config instance. It would call the provided fallback if the refresh process failed.
func (*ConfigManager) Write ¶
func (cm *ConfigManager) Write(merge bool) error
type ConfigManagerOption ¶
type ConfigManagerOption func(cm *ConfigManager) error
func WithDefaultConfigFile ¶
func WithDefaultConfigFile() ConfigManagerOption
func WithFeeder ¶
func WithFeeder(feeder Feeder) ConfigManagerOption
func WithFile ¶
func WithFile(file string, forceCreate bool) ConfigManagerOption
type Feeder ¶
type Feeder interface { // Feed gets a struct and feeds it using configuration data. Feed(structure interface{}) error // Write the structure to the feeder Write(structure interface{}, merge bool) error }
Feeder is an interface for configuration Feeders that provide configuration data.
type YamlFeeder ¶
type YamlFeeder struct {
File string
}
YamlFeeder feeds using a YAML file.
func (YamlFeeder) Feed ¶
func (f YamlFeeder) Feed(structure interface{}) error
func (YamlFeeder) Write ¶
func (yf YamlFeeder) Write(structure interface{}, merge bool) error