config

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2020 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package config manages CLI configurations for your DC/OS clusters.

Each configured cluster has an associated TOML configuration file which can be parsed, updated, or displayed through the Config struct.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrConfigNotFound = errors.New("no match found")

ErrConfigNotFound means that the manager cannot find a config using a name/id.

View Source
var (
	ErrNoConfigPath = errors.New("no path specified for the config")
)

Errors related to the Config.

View Source
var ErrNotAttached = errors.New("no cluster is attached")

ErrNotAttached indicates that no cluster is attached.

View Source
var ErrTooManyConfigs = errors.New("multiple matches found")

ErrTooManyConfigs means that more than one config has been found for a given search.

Functions

func Keys

func Keys() map[string]string

Keys returns the possible config keys.

Types

type Cluster

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

Cluster is a subset representation of a DC/OS CLI configuration.

It is a proxy struct on top of a config which provides user-friendly getters and setters for common configurations such as "core.dcos_url" or "core.ssl_verify". It leverages Go types as much as possible.

func NewCluster

func NewCluster(conf *Config) *Cluster

NewCluster returns a new cluster for a given config, if omitted it uses an empty config.

func (*Cluster) ACSToken

func (c *Cluster) ACSToken() string

ACSToken returns the token generated by authenticating to DC/OS using the Admin Router Access Control Service.

func (*Cluster) Config

func (c *Cluster) Config() *Config

Config returns the cluster's config.

func (*Cluster) Dir

func (c *Cluster) Dir() string

Dir returns the cluster's directory.

func (*Cluster) ID

func (c *Cluster) ID() string

ID returns the ID of the cluster.

func (*Cluster) Name

func (c *Cluster) Name() string

Name returns the custom name for the cluster.

func (*Cluster) SetACSToken

func (c *Cluster) SetACSToken(acsToken string)

SetACSToken sets the token generated by authenticating to DC/OS using the Admin Router Access Control Service.

func (*Cluster) SetID

func (c *Cluster) SetID(id string)

SetID sets a custom id for the cluster.

func (*Cluster) SetName

func (c *Cluster) SetName(name string)

SetName sets a custom name for the cluster.

func (*Cluster) SetTLS

func (c *Cluster) SetTLS(tls TLS)

SetTLS returns the configuration for TLS clients.

func (*Cluster) SetTimeout

func (c *Cluster) SetTimeout(timeout time.Duration)

SetTimeout sets the HTTP request timeout once the connection is established.

func (*Cluster) SetURL

func (c *Cluster) SetURL(url string)

SetURL sets the public master URL of the DC/OS cluster.

func (*Cluster) TLS

func (c *Cluster) TLS() (TLS, error)

CheckTLS returns error if provided certificate is invalid

func (*Cluster) Timeout

func (c *Cluster) Timeout() time.Duration

Timeout returns the HTTP request timeout once the connection is established.

func (*Cluster) URL

func (c *Cluster) URL() string

URL returns the public master URL of the DC/OS cluster.

type Config

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

Config is the backend for Config data. It aggregates multiple sources (env vars, TOML document) and is able to get/set/unset key(s) in the TOML document.

func Empty

func Empty() *Config

Empty returns an empty config store.

func New

func New(opts Opts) *Config

New creates a Config based on functional options.

func (*Config) Fs

func (c *Config) Fs() afero.Fs

Fs returns the filesystem for the store.

func (*Config) Get

func (c *Config) Get(key string) interface{}

Get returns a value from the Config using a key.

func (*Config) Keys

func (c *Config) Keys() []string

Keys returns all the keys in the Config.

func (*Config) LoadPath

func (c *Config) LoadPath(path string) error

LoadPath populates the store based on a path to a TOML file. If the file doesn't exist, an empty one is created.

Example
package main

import (
	"fmt"

	"github.com/dcos/dcos-cli/pkg/config"
)

func main() {
	store := config.Empty()
	err := store.LoadPath("/path/to/config.toml")
	if err != nil {
		// Handle error.
	}

	// Displays the DC/OS URL if it exists in the file.
	fmt.Println(store.Get("core.dcos_url"))

	// Change the cluster name.
	store.Set("cluster.name", "my-new-cluster-name")
}
Output:

func (*Config) LoadReader

func (c *Config) LoadReader(reader io.Reader) error

LoadReader populates the store based on an io.Reader containing TOML data.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/dcos/dcos-cli/pkg/config"
)

func main() {
	store := config.Empty()
	err := store.LoadReader(strings.NewReader(`
[core]

dcos_url = "https://example.com"
dcos_acs_token = "token_zj8Tb0vhQw"
ssl_verify = "/path/to/dcos_ca.crt"

[cluster]
name = "my-cluster"
`))
	if err != nil {
		// Handle error.
	}

	fmt.Println(store.Get("core.dcos_url"))       // https://example.com
	fmt.Println(store.Get("core.dcos_acs_token")) // "token_zj8Tb0vhQw"
	fmt.Println(store.Get("core.cluster_name"))   // "my-cluster"
}
Output:

func (*Config) LoadTree

func (c *Config) LoadTree(tree *toml.Tree)

LoadTree populates the store with a TOML tree.

func (*Config) Path

func (c *Config) Path() string

Path returns the path to the Config.

func (*Config) Persist

func (c *Config) Persist() error

Persist flushes the in-memory TOML tree representation to the path associated to the Config.

func (*Config) Set

func (c *Config) Set(key string, val interface{}) (err error)

Set sets a key in the store.

func (*Config) SetPath

func (c *Config) SetPath(path string)

SetPath assigns a path to the Config.

func (*Config) ToMap

func (c *Config) ToMap() map[string]interface{}

ToMap recursively generates a representation of the config using Go built-in structures.

func (*Config) Unset

func (c *Config) Unset(key string)

Unset deletes a given key from the Config.

type Manager

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

Manager is able to retrieve, create, and delete configs.

func NewManager

func NewManager(opts ManagerOpts) *Manager

NewManager creates a new config manager.

func (*Manager) All

func (m *Manager) All() (configs []*Config)

All retrieves all configs.

func (*Manager) Attach

func (m *Manager) Attach(config *Config) error

Attach sets a given config as the current one. This is done by adding an `attached` file next to it. If another config is already attached, the file gets moved.

func (*Manager) Current

func (m *Manager) Current() (*Config, error)

Current retrieves the current config.

The lookup order is : - DCOS_CLUSTER is defined and is the name/ID of a configured cluster. - An attached file exists alongside a configured cluster, OR there is a single configured cluster.

func (*Manager) Find

func (m *Manager) Find(name string, strict bool) (*Config, error)

Find finds a config by cluster name or ID, `strict` indicates whether or not the search string can also be a cluster ID prefix.

func (*Manager) Save

func (m *Manager) Save(config *Config, id string, caBundle []byte) error

Save saves a config to the disk under the given cluster ID folder.

type ManagerOpts

type ManagerOpts struct {
	// Fs is an abstraction for the filesystem. All filesystem operations
	// for the manager should be done through it instead of the os package.
	Fs afero.Fs

	// EnvLookup is the function used to lookup environment variables.
	// When not set it defaults to os.LookupEnv.
	EnvLookup func(key string) (string, bool)

	// Dir is the root directory for the config manager.
	Dir string
}

ManagerOpts are functional options for a Manager.

type Opts

type Opts struct {
	// EnvWhitelist is a map of config keys and environment variables.
	// When present, these env vars take precedence over the values in the toml.Tree.
	EnvWhitelist map[string]string

	// EnvLookup is the function used to lookup environment variables.
	// When not set it defaults to os.LookupEnv.
	EnvLookup func(key string) (string, bool)

	// Fs is an abstraction for the filesystem. All filesystem operations
	// for the store should be done through it instead of the os package.
	Fs afero.Fs
}

Opts are functional options for a Config.

type SSLError

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

func NewSSLError

func NewSSLError(e error) *SSLError

func (*SSLError) Error

func (e *SSLError) Error() string

type TLS

type TLS struct {
	// Insecure specifies if server certificates should be accepted without verification.
	//
	// Skipping verification against the system's CA bundle or a cluster-specific CA is highly discouraged
	// and should only be done during testing/development.
	Insecure bool

	// Path to the root CA bundle.
	RootCAsPath string

	// A pool of root CAs to verify server certificates against.
	RootCAs *x509.CertPool
}

TLS holds the configuration for TLS clients.

func (*TLS) String

func (tls *TLS) String() string

String creates a string from a TLS struct.

Jump to

Keyboard shortcuts

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