config

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// KittyCADConfigDirEnvVar is the environment variable that can be used to override
	// the default config directory.
	KittyCADConfigDirEnvVar = "KITTYCAD_CONFIG_DIR"
	// KittyCADShorthandDir is the directory name used for the config and state.
	KittyCADShorthandDir = "kittycad"
	// KittyCADWindowsDir is the directory name used for the config and state on Windows.`
	KittyCADWindowsDir = "KittyCAD CLI"
	// XDGConfigHomeEnvVar is the environment variable for the XDG_CONFIG_HOME.
	XDGConfigHomeEnvVar = "XDG_CONFIG_HOME"
	// XDGStateHomeEnvVar is the environment variable for the XDG_STATE_HOME.
	XDGStateHomeEnvVar = "XDG_STATE_HOME"
	// XDGDataHomeEnvVar is the environment variable for the XDG_DATA_HOME.
	XDGDataHomeEnvVar = "XDG_DATA_HOME"
	// AppData is the directory for AppData on Windows.
	AppData = "AppData"
	// LocalAppData is the directory for LocalAppData on Windows.
	LocalAppData = "LocalAppData"
)
View Source
const (
	// KittyCADHostEnvVar is the environment variable name for the host.
	KittyCADHostEnvVar = "KITTYCAD_HOST"
	// KittyCADTokenEnvVar is the environment variable name for the token.
	KittyCADTokenEnvVar = "KITTYCAD_TOKEN"
	// KittyCADAPITokenEnvVar is the environment variable name for the API token.
	KittyCADAPITokenEnvVar = "KITTYCAD_API_TOKEN"
	// KittyCADDefaultHost is the default host.
	KittyCADDefaultHost = "api.kittycad.io"
)

Variables

View Source
var BackupConfigFile = func(filename string) error {
	return os.Rename(filename, filename+".bak")
}

BackupConfigFile backs up the config file.

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
}

ReadConfigFile reads the config file.

View Source
var WriteConfigFile = func(filename string, data []byte) error {
	err := os.MkdirAll(filepath.Dir(filename), 0771)
	if err != nil {
		return pathError(err)
	}

	cfgFile, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
	if err != nil {
		return err
	}
	defer cfgFile.Close()

	_, err = cfgFile.Write(data)
	return err
}

WriteConfigFile writes the config file.

Functions

func AuthTokenFromEnv

func AuthTokenFromEnv(hostname string) (string, string)

AuthTokenFromEnv returns the auth token and the environment variable name.

func AuthTokenProvidedFromEnv

func AuthTokenProvidedFromEnv() bool

AuthTokenProvidedFromEnv returns true if the auth token is provided.

func DataDir

func DataDir() string

DataDir returns the path to the data directory. Data path precedence 1. XDG_DATA_HOME 2. LocalAppData (windows only) 3. HOME

func Dir

func Dir() string

Dir returns the path to the config directory. Config path precedence 1. KITTYCAD_CONFIG_DIR 2. XDG_CONFIG_HOME 3. AppData (windows only) 4. HOME

func File

func File() string

File returns the path to the config file.

func HomeDirPath

func HomeDirPath(subdir string) (string, error)

HomeDirPath returns the path to the home directory.

func HostsConfigFile

func HostsConfigFile() string

HostsConfigFile returns the path to the hosts config file.

func IsHostEnv

func IsHostEnv(src string) bool

IsHostEnv returns true if the environment variable is equal to the source.

func NewBlankRoot

func NewBlankRoot() *yaml.Node

NewBlankRoot initializes a config file pre-populated with comments and default values.

func StateDir

func StateDir() string

StateDir returns the path to the state directory. State path precedence 1. XDG_CONFIG_HOME 2. LocalAppData (windows only) 3. HOME

func StubBackupConfig

func StubBackupConfig() func()

StubBackupConfig stubs the backup config file.

func StubWriteConfig

func StubWriteConfig(wc io.Writer, wh io.Writer) func()

StubWriteConfig stubs the write config file.

func ValidateKey

func ValidateKey(key string) error

ValidateKey validates a key.

func ValidateValue

func ValidateValue(key, value string) error

ValidateValue validates a value for a key.

Types

type AliasConfig

type AliasConfig struct {
	Map
	Parent Config
}

AliasConfig is a config file that stores aliases.

func (*AliasConfig) Add

func (a *AliasConfig) Add(alias, expansion string) error

Add adds an alias to the config.

func (*AliasConfig) All

func (a *AliasConfig) All() map[string]string

All returns all aliases in the config.

func (*AliasConfig) Delete

func (a *AliasConfig) Delete(alias string) error

Delete deletes an alias from the config.

func (*AliasConfig) Get

func (a *AliasConfig) Get(alias string) (string, bool)

Get returns the expansion of the alias.

type Config

type Config interface {
	Get(string, string) (string, error)
	GetWithSource(string, string) (string, string, error)
	Set(string, string, string) error
	UnsetHost(string)
	Hosts() ([]string, error)
	DefaultHost() (string, error)
	DefaultHostWithSource() (string, string, error)
	Aliases() (*AliasConfig, error)
	CheckWriteable(string, string) error
	Write() error
}

Config is the interface for a configuration file.

func InheritEnv

func InheritEnv(c Config) Config

InheritEnv returns true if the environment variable is inherited.

func NewBlankConfig

func NewBlankConfig() Config

NewBlankConfig initializes a config file pre-populated with comments and default values.

func NewConfig

func NewConfig(root *yaml.Node) Config

NewConfig initializes a Config from a yaml node.

func NewFromString

func NewFromString(str string) Config

NewFromString initializes a Config from a yaml string.

func ParseDefaultConfig

func ParseDefaultConfig() (Config, error)

ParseDefaultConfig parses the default config file.

type Entry

type Entry struct {
	KeyNode   *yaml.Node
	ValueNode *yaml.Node
	Index     int
}

Entry represents a single entry in a Map.

type HostConfig

type HostConfig struct {
	Map
	Host string
}

HostConfig represents a single host entry in the hosts.yml file.

type InvalidValueError

type InvalidValueError struct {
	ValidValues []string
}

InvalidValueError is an error that occurs when a value is not valid for a key.

func (InvalidValueError) Error

func (e InvalidValueError) Error() string

Error implements the error interface.

type Map

type Map struct {
	Root *yaml.Node
}

Map implements a low-level get/set config that is backed by an in-memory tree of yaml nodes. It allows us to interact with a yaml-based config programmatically, preserving any comments that were present when the yaml was parsed.

func (*Map) Empty

func (cm *Map) Empty() bool

Empty returns true if the Map is empty.

func (*Map) FindEntry

func (cm *Map) FindEntry(key string) (*Entry, error)

FindEntry returns the Entry for the given key.

func (*Map) GetStringValue

func (cm *Map) GetStringValue(key string) (string, error)

GetStringValue returns the value of the given key as a string.

func (*Map) RemoveEntry

func (cm *Map) RemoveEntry(key string)

RemoveEntry removes the entry with the given key.

func (*Map) SetStringValue

func (cm *Map) SetStringValue(key, value string) error

SetStringValue sets the value of the given key to the given string.

type NotFoundError

type NotFoundError struct {
	// contains filtered or unexported fields
}

NotFoundError is returned when a key is not found in a Map.

type Option

type Option struct {
	Key           string
	Description   string
	DefaultValue  string
	AllowedValues []string
}

Option is a configuration option.

func Options

func Options() []Option

Options returns a list of all config options.

type ReadOnlyEnvError

type ReadOnlyEnvError struct {
	Variable string
}

ReadOnlyEnvError is an error that is returned when an environment is read only.

func (*ReadOnlyEnvError) Error

func (e *ReadOnlyEnvError) Error() string

Error returns a string representation of the error.

type Stub

type Stub map[string]string

Stub is a stub implementation of the Config interface.

func (Stub) Aliases

func (c Stub) Aliases() (*AliasConfig, error)

Aliases returns the aliases of the given key.

func (Stub) CheckWriteable

func (c Stub) CheckWriteable(host, key string) error

CheckWriteable checks if the config is writeable.

func (Stub) DefaultHost

func (c Stub) DefaultHost() (string, error)

DefaultHost returns the default host.

func (Stub) DefaultHostWithSource

func (c Stub) DefaultHostWithSource() (string, string, error)

DefaultHostWithSource returns the default host.

func (Stub) Get

func (c Stub) Get(host, key string) (string, error)

Get returns the value of the given key.

func (Stub) GetWithSource

func (c Stub) GetWithSource(host, key string) (string, string, error)

GetWithSource returns the value of the given key.

func (Stub) Hosts

func (c Stub) Hosts() ([]string, error)

Hosts returns the list of hosts.

func (Stub) Set

func (c Stub) Set(host, key, value string) error

Set sets the value of the given key.

func (Stub) UnsetHost

func (c Stub) UnsetHost(hostname string)

UnsetHost removes the given host from the list of hosts.

func (Stub) Write

func (c Stub) Write() error

Write writes the config to the given writer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳