config

package
v0.3.0-82-gcd852a2 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 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 except in compliance with the License.

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.

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 except 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 (
	DefaultRuntimeDir    = "/var/kraftkit"
	DefaultEventsPidFile = "/var/kraftkit/events.pid"
)

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 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 `yaml:"user" env:"KRAFTKIT_AUTH_%s_USER" long:"auth-%s-user"`
	Token     string `yaml:"token" env:"KRAFTKIT_AUTH_%s_TOKEN" long:"auth-%s-token"`
	Endpoint  string `yaml:"endpoint" env:"KRAFTKIT_AUTH_%s_ENDPOINT" long:"auth-%s-endpoint"`
	VerifySSL bool   `yaml:"verify_ssl" env:"KRAFTKIT_AUTH_%s_VERIFY_SSL" long:"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   `yaml:"no_prompt" env:"KRAFTKIT_NO_PROMPT" long:"no-prompt" usage:"Do not prompt for user interaction" default:"false"`
	NoParallel     bool   `yaml:"no_parallel" env:"KRAFTKIT_NO_PARALLEL" long:"no-parallel" usage:"Do not run internal tasks in parallel" default:"true"`
	NoEmojis       bool   `yaml:"no_emojis" env:"KRAFTKIT_NO_EMOJIS" long:"no-emojis" usage:"Do not use emojis in any console output" default:"true"`
	Editor         string `yaml:"editor" env:"KRAFTKIT_EDITOR" long:"editor" usage:"Set the text editor to open when prompt to edit a file"`
	GitProtocol    string `yaml:"git_protocol" env:"KRAFTKIT_GIT_PROTOCOL" long:"git-protocol" usage:"Preferred Git protocol to use" default:"https"`
	Pager          string `yaml:"pager,omitempty" env:"KRAFTKIT_PAGER" long:"pager" usage:"System pager to pipe output to"`
	HTTPUnixSocket string `` /* 165-byte string literal not displayed */
	RuntimeDir     string `` /* 148-byte string literal not displayed */
	DefaultPlat    string `` /* 139-byte string literal not displayed */
	DefaultArch    string `` /* 147-byte string literal not displayed */
	EventsPidFile  string `` /* 173-byte string literal not displayed */

	Paths struct {
		Plugins   string `yaml:"plugins,omitempty" env:"KRAFTKIT_PATHS_PLUGINS" long:"plugins-dir" usage:"Path to KraftKit plugin directory"`
		Config    string `yaml:"config,omitempty" env:"KRAFTKIT_PATHS_CONFIG" long:"config-dir" usage:"Path to KraftKit config file"`
		Manifests string `yaml:"manifests,omitempty" env:"KRAFTKIT_PATHS_MANIFESTS" long:"manifests-dir" usage:"Path to Unikraft manifest cache"`
		Sources   string `yaml:"sources,omitempty" env:"KRAFTKIT_PATHS_SOURCES" long:"sources-dir" usage:"Path to Unikraft component cache"`
	} `yaml:"paths,omitempty"`

	Log struct {
		Level      string `yaml:"level" env:"KRAFTKIT_LOG_LEVEL" long:"log-level" usage:"Log level verbosity" default:"info"`
		Timestamps bool   `yaml:"timestamps" env:"KRAFTKIT_LOG_TIMESTAMPS" long:"log-timestamps" usage:"Enable log timestamps"`
		Type       string `yaml:"type" env:"KRAFTKIT_LOG_TYPE" long:"log-type" usage:"Log type" default:"fancy"`
	} `yaml:"log"`

	Unikraft struct {
		Mirrors   []string `yaml:"mirrors" env:"KRAFTKIT_UNIKRAFT_MIRRORS" long:"with-mirror" usage:"Paths to mirrors of Unikraft component artifacts"`
		Manifests []string `yaml:"manifests" env:"KRAFTKIT_UNIKRAFT_MANIFESTS" long:"with-manifest" usage:"Paths to package or component manifests"`
	} `yaml:"unikraft"`

	Auth map[string]AuthConfig `yaml:"auth,omitempty" noattribute:"true"`

	Aliases map[string]map[string]string `yaml:"aliases" noattribute:"true"`
}

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