config

package
v0.9.1-12-g059c86f1 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2024 License: BSD-3-Clause Imports: 19 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 (
	DefaultManifestIndex = "https://manifests.kraftkit.sh/index.yaml"
)

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 FetchConfigDirFromArgs added in v0.7.0

func FetchConfigDirFromArgs(args []string) (path string)

FetchConfigDirFromArgs returns the path to the alternate config directory that can be set via the --config-dir flag. This needs to be fetched before flags are populated with AttributeFlags to ensure that the function is called only once.

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 GetKraftCloudTokenAuthConfig added in v0.7.1

func GetKraftCloudTokenAuthConfig(auth AuthConfig) string

GetKraftCloudTokenAuthConfig is a utility method which returns the token of the KraftCloud user based on an AuthConfig.

func HomeDirPath

func HomeDirPath(subdir string) (string, error)

func HydrateKraftCloudAuthInContext added in v0.7.1

func HydrateKraftCloudAuthInContext(ctx context.Context) (context.Context, error)

HydrateKraftCloudAuthInContext saturates the context with an additional KraftCloud-specific information.

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" default:"true"`
}

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.

func GetKraftCloudAuthConfig added in v0.7.9

func GetKraftCloudAuthConfig(ctx context.Context, flagToken string) (*AuthConfig, error)

GetKraftCloudLogin is a utility method which retrieves credentials of a KraftCloud user from the given context returning it in AuthConfig format.

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   `` /* 126-byte string literal not displayed */
	NoEmojis       bool   `yaml:"no_emojis" env:"KRAFTKIT_NO_EMOJIS" long:"no-emojis" usage:"Do not use emojis in any console output" default:"true"`
	NoCheckUpdates bool   `` /* 128-byte string literal not displayed */
	NoColor        bool   `yaml:"no_color" env:"KRAFTKIT_NO_COLOR" long:"no-color" usage:"Disable color output"`
	NoWarnSudo     bool   `yaml:"no_warn_sudo" env:"KRAFTKIT_NO_WARN_SUDO" long:"no-warn-sudo" usage:"Do not warn on running via sudo" default:"false"`
	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" default:"cat"`
	Qemu           string `yaml:"qemu,omitempty" env:"KRAFTKIT_QEMU" long:"qemu" usage:"Path to QEMU executable" default:""`
	HTTPUnixSocket string `` /* 165-byte string literal not displayed */
	RuntimeDir     string `yaml:"runtime_dir" env:"KRAFTKIT_RUNTIME_DIR" long:"runtime-dir" usage:"Directory for placing runtime files (e.g. pidfiles)"`
	DefaultPlat    string `` /* 139-byte string literal not displayed */
	DefaultArch    string `` /* 147-byte string literal not displayed */
	ContainerdAddr string `` /* 141-byte string literal not displayed */
	EventsPidFile  string `` /* 138-byte string literal not displayed */
	BuildKitHost   string `yaml:"buildkit_host" env:"KRAFTKIT_BUILDKIT_HOST" long:"buildkit-host" usage:"Path to the buildkit host" default:""`

	Paths struct {
		Plugins   string `yaml:"plugins,omitempty" env:"KRAFTKIT_PATHS_PLUGINS" long:"plugins-dir" usage:"Path to KraftKit plugin directory"`
		Config    string `yaml:"-" env:"KRAFTKIT_PATHS_CONFIG" long:"config-dir" usage:"Path to KraftKit config directory"`
		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 `` /* 157-byte string literal not displayed */
		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. Choice of: [fancy, basic, json]" 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