config

package
v0.4.1-88-g144abcf Latest Latest
Warning

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

Go to latest
Published: Mar 28, 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 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 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 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 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 := io.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 DataDir

func DataDir() string

Data path precedence 1. XDG_DATA_HOME 2. LocalAppData (windows only) 3. HOME

func Default

func Default[C any](key string) string

func DefaultConfigFile added in v0.4.1

func DefaultConfigFile() string

func G added in v0.2.0

func G[T any](ctx context.Context) *T

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

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[C any](ctx context.Context, cfgm *ConfigManager[C]) 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 ConfigDetail

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

func ConfigDetails

func ConfigDetails() []ConfigDetail

type ConfigManager

type ConfigManager[C any] struct {
	Config     *C
	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 M added in v0.2.0

func M[T any](ctx context.Context) *ConfigManager[T]

FromContext returns the Config Manager for kraftkit in the context, or an inert configuration that results in default values.

func NewConfigManager

func NewConfigManager[C any](c *C, opts ...ConfigManagerOption[C]) (*ConfigManager[C], error)

func (*ConfigManager[C]) AddFeeder

func (cm *ConfigManager[C]) AddFeeder(f Feeder) *ConfigManager[C]

AddFeeder adds a feeder that provides configuration data.

func (*ConfigManager[C]) Feed

func (cm *ConfigManager[C]) Feed() error

Feed binds configuration data from added feeders to the added structs.

func (*ConfigManager[C]) SetupListener

func (cm *ConfigManager[C]) SetupListener(fallback func(err error)) *ConfigManager[C]

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[C]) Write

func (cm *ConfigManager[C]) Write(merge bool) error

type ConfigManagerOption

type ConfigManagerOption[C any] func(cm *ConfigManager[C]) error

func WithDefaultConfigFile

func WithDefaultConfigFile[C any]() ConfigManagerOption[C]

func WithFeeder

func WithFeeder[C any](feeder Feeder) ConfigManagerOption[C]

func WithFile

func WithFile[C any](file string, forceCreate bool) ConfigManagerOption[C]

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 KraftKit added in v0.4.1

type KraftKit 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 NewDefaultKraftKitConfig added in v0.4.1

func NewDefaultKraftKitConfig() (*KraftKit, error)

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