config

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: May 13, 2020 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package config manages global configuration parameters. It has a data type and helpers for getting information out of the runtime environment, and making it available to commands that need it.

Index

Constants

View Source
const (
	// EnvVarToken is the env var we look in for the Fastly API token.
	// gosec flagged this:
	// G101 (CWE-798): Potential hardcoded credentials
	// Disabling as we use the value in the command help output.
	/* #nosec */
	EnvVarToken = "FASTLY_API_TOKEN"

	// EnvVarEndpoint is the env var we look in for the API endpoint.
	EnvVarEndpoint = "FASTLY_API_ENDPOINT"
)
View Source
const DefaultEndpoint = "https://api.fastly.com"

DefaultEndpoint is the default Fastly API endpoint.

Variables

View Source
var FilePath = func() string {
	if dir, err := os.UserConfigDir(); err == nil {
		return filepath.Join(dir, "fastly", "config.toml")
	}
	if dir, err := os.UserHomeDir(); err == nil {
		return filepath.Join(dir, ".fastly", "config.toml")
	}
	panic("unable to deduce user config dir or user home dir")
}()

FilePath is the location of the fastly CLI application config file.

Functions

This section is empty.

Types

type Data

type Data struct {
	File File
	Env  Environment
	Flag Flag

	Client api.Interface // set by Run after parse
}

Data holds global-ish configuration data from all sources: environment variables, config files, and flags. It has methods to give each parameter to the components that need it, including the place the parameter came from, which is a requirement.

If the same parameter is defined in multiple places, it is resolved according to the following priority order: the config file (lowest priority), env vars, and then explicit flags (highest priority).

This package and its types are only meant for parameters that are applicable to most/all subcommands (e.g. API token) and are consistent for a given user (e.g. an email address). Otherwise, parameters should be defined in specific command structs, and parsed as flags.

func (*Data) Endpoint

func (d *Data) Endpoint() (string, Source)

Endpoint yields the API endpoint.

func (*Data) Token

func (d *Data) Token() (string, Source)

Token yields the Fastly API token.

func (*Data) Verbose

func (d *Data) Verbose() bool

Verbose yields the verbose flag, which can only be set via flags.

type Environment

type Environment struct {
	Token    string
	Endpoint string
}

Environment represents all of the configuration parmaeters that can come from environment variables.

func (*Environment) Read

func (e *Environment) Read(env map[string]string)

Read populates the fields from the provided environment.

type File

type File struct {
	Token            string `toml:"token"`
	Email            string `toml:"email"`
	Endpoint         string `toml:"endpoint"`
	LastVersionCheck string `toml:"last_version_check"`
}

File represents all of the configuration parameters that can end up in the config file. At some point, it may expand to include e.g. user profiles.

func (*File) Read

func (f *File) Read(filename string) error

Read the File and populate its fields from the filename on disk.

func (*File) Write

func (f *File) Write(filename string) error

Write the File to filename on disk.

type Flag

type Flag struct {
	Token    string
	Verbose  bool
	Endpoint string
}

Flag represents all of the configuration parameters that can be set with explicit flags. Consumers should bind their flag values to these fields directly.

type Source

type Source uint8

Source enumerates where a config parameter is taken from.

const (
	// SourceUndefined indicates the parameter isn't provided in any of the
	// available sources, similar to "not found".
	SourceUndefined Source = iota

	// SourceFile indicates the parameter came from a config file.
	SourceFile

	// SourceEnvironment indicates the parameter came from an env var.
	SourceEnvironment

	// SourceFlag indicates the parameter came from an explicit flag.
	SourceFlag

	// SourceDefault indicates the parameter came from a program default.
	SourceDefault

	// DirectoryPermissions is the default directory permissions for the config file directory.
	DirectoryPermissions = 0700

	// FilePermissions is the default file permissions for the config file.
	FilePermissions = 0600
)

Jump to

Keyboard shortcuts

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