config

package
v0.3.0-17-g4ff5271 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2023 License: BSD-3-Clause Imports: 13 Imported by: 6

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

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

View Source
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()
)
View Source
var BackupConfigFile = func(filename string) error {
	return os.Rename(filename, filename+".bak")
}
View Source
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
}
View Source
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 AllowedValues(key string) []string

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 Default

func Default(key string) string

func Defaults

func Defaults() map[string]string

func HomeDirPath

func HomeDirPath(subdir string) (string, error)

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

func ConfigFromContext(ctx context.Context) *Config

ConfigFromContext returns the config for kraftkit in the context, or an inert configuration that results in default values.

func NewDefaultConfig

func NewDefaultConfig() (*Config, error)

type ConfigDetail

type ConfigDetail struct {
	Key           string
	Description   string
	AllowedValues []string
}

func ConfigDetails

func ConfigDetails() []ConfigDetail

type ConfigManager

type ConfigManager struct {
	Config     *Config
	ConfigFile string
	Feeders    []Feeder
}

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

Jump to

Keyboard shortcuts

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