common

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2022 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// default logger configuration.
	LoggingConfig = log.LstdFlags | log.Ldate | log.Ltime | log.LUTC | log.Lmicroseconds

	// the default name of the config file.
	CfgFileDefault = ".mkactl"
	// the default (relative) path below the local config path root that will
	// contain cloned control repositories.
	CfgPathRepo = "repo"
	// the default gitlab endpoint address.
	CfgAddressDefault = "code.syseleven.de:443"

	// config key: protocol version.
	CfgKeyAPIVersion = "apiVersion"

	// the environment variable containing the path to the kubectl configuration file.
	EnvVarKubeconfig = clientcmd.RecommendedConfigPathEnvVar
	// the default env var pointing to the editor app on the CLI.
	EnvVarEditor = "EDITOR"

	// for paginated results, max value.
	GitlabMaxList = 100
	// the gitlab group on code.syseleven.de containing building blocks repositories.
	GitlabGroupBuildingBlocks = 121
	// gitlab project label added to control repositories.
	GitlabProjectLabel = "control-repo"
	// the default gitlabCI configuration file for repositories.
	GitlabProjectCIFile = ".gitlab-ci.yml"

	// path depth for local clones below the repo root directory.
	LocalRepositoryPathDepth = 4

	// filesystem permission for user read-write access.
	FSPermUserOnly = 0o600
	// filesystem permission for user read-write as well as group and other read-only access.
	FSPermAllRO = 0o644

	// for tabular output extra padding size for cell entries.
	FormatTablePadding = 2

	// for masked credentials, max mask length.
	PasswordMaskLength = 12

	// random part length (in generated names).
	RandomPartLength = 12

	// minimum display size for variable-length values.
	MinDisplayNameLength = 6
	// maximum display size for variable-length values.
	MaxDisplayNameLength = 48

	GitStatusMsgOK  = "clean"
	GitStatusMsgNOK = "dirty"

	// blank display values.
	BlankVal = ""

	// exit codes.
	ExitError = 1

	// misc.
	YamlFileGlob = `(?i).*\.ya?ml`
)

Variables

View Source
var (
	// project base CI template.
	//go:embed res/gitlab-ci.yaml.tmpl
	GitlabProjectCIBaseTemplate string

	// building block CI template.
	//go:embed res/gitlab-ci.bb-inc.yaml.tmpl
	GitlabProjectCIBlockTemplate string

	// to match invalid env var names.
	InvalidEnvVar = regexp.MustCompile(`[^a-zA-Z_]+`)

	// to match keys for sensitive data for redaction.
	Redactable = regexp.MustCompile(`(?i)pass|token\S*`)
)
View Source
var (
	// configuration related errors.
	ErrCfgVersionMismatch = errors.New("invalid api version in configuration")
	ErrCfgValTypeMismatch = errors.New("invalid value type in configuration")

	// repository related errors.
	ErrNoRepo         = errors.New("no such control repository")
	ErrNoGLRepo       = errors.New("no such gitlab repository")
	ErrNoCIDefinition = errors.New("no CI definitions found in repository")
	ErrCorruptRepo    = errors.New("cloned repository unusable")
	ErrNoRemotes      = errors.New("no remotes for cloned repository")
	ErrNoTags         = errors.New("no tags for repository")
	ErrNoChanges      = errors.New("no changes in repository")

	// gitlab related errors.
	ErrNoAddress     = errors.New("missing gitlab address")
	ErrNoCredentials = errors.New("missing gitlab access token or user name")
	ErrNoAuth        = errors.New("missing authentication")
	ErrNoPermissions = errors.New("missing sufficient gitlab permissions")
	ErrFoundMultiple = errors.New("found multiple repositories")
	ErrNoGroup       = errors.New("no such gitlab group")

	// runtime errors.
	ErrUnsupported           = errors.New("unsupported platform or environment")
	ErrBlockRef              = errors.New("wrongly addressed building block")
	ErrNoSelection           = errors.New("no argument given and no repository selected")
	ErrNoDir                 = errors.New("this is not a directory")
	ErrNoSuchAccount         = errors.New("no such account configured")
	ErrArgMismatch           = errors.New("invalid combination of arguments")
	ErrMismatchedPathElement = errors.New("invalid repository path element")
	ErrNil                   = errors.New("argument is nil")

	// internal errors.
	ErrInternal = errors.New("unexpected behaviour, please report")
	ErrFound    = errors.New("found")
)

Functions

func BoolPtr

func BoolPtr(b bool) *bool

returns a pointer to the given bool.

func EnsureDataRoot

func EnsureDataRoot(cmd ConfiguredCommand) string

EnsureDataRoot makes sure the local data path (default: ~/.config/mkactl) exists.

func EnsureRepoBasePath added in v0.6.0

func EnsureRepoBasePath(cmd ConfiguredCommand) string

EnsureRepoBasePath makes sure the (default) local configuration path containing git repositories exists.

func GitlabPageSize

func GitlabPageSize(cmd ConfiguredCommand) (i int)

gets the configured page size for gitlab paging requests from the context account or account defaults if not set, defaults to 100.

func IntPtr

func IntPtr(i int) *int

returns a pointer to the given int.

func OsOpen

func OsOpen(url string)

opens the default application to handle URLs for the current environment.

func RandomString

func RandomString(length int) string

func Redact added in v0.6.0

func Redact(v interface{})

Redacts iterates recursively over a given interface and redacts any sensitive data. If a certain field is sensitive is depending on if the field name is part of the sensitive fields slice.

func StrPtr

func StrPtr(v string) *string

returns a pointer to the given string.

func UniqueStrings

func UniqueStrings(ss []string) []string

transforms the given slice of strings to a new one only containing unique strings from the given slice.

Types

type AccountCommand added in v0.4.0

type AccountCommand interface {
	Credentials() *GitlabCredentials
}

a command supporting the --account flag.

type AccountConfig added in v0.4.0

type AccountConfig struct {
	PageSize                             int
	Address, User, Password, Token, Name *ConfigValue
}

a (gitlab) account parameters record.

func (*AccountConfig) AsCredentials added in v0.4.0

func (a *AccountConfig) AsCredentials() *GitlabCredentials

func (*AccountConfig) Equals added in v0.4.0

func (a *AccountConfig) Equals(o *AccountConfig) bool

type Accounts added in v0.4.0

type Accounts []*AccountConfig

func (Accounts) AddOrRemove added in v0.8.0

func (as Accounts) AddOrRemove(ac *AccountConfig, remove bool) (Accounts, error)

adds (or overrides if an account with the given name already exists) the given account to the returned collection. NB: does not modify this collection.

func (Accounts) MaxNameLen added in v0.4.0

func (as Accounts) MaxNameLen() int

returns the length of the longest account name in this collection of accounts.

type BaseCommand

type BaseCommand struct {
	CobraCmd *cobra.Command
}

the base data type for all commands, wraps a cobra.Command.

type BlockName

type BlockName string

represents a building block name.

func (*BlockName) Exists

func (a *BlockName) Exists(ps []*gitlab.Project) *gitlab.Project

for this BlockName finds its gitlab repository in the given list of gitlab repositories returns nil if no such project exists.

func (BlockName) Ptr

func (a BlockName) Ptr() *BlockName

Ptr returns a pointer to the given BlockName.

func (*BlockName) Slug added in v0.8.0

func (a *BlockName) Slug() string

blockname reduced to a valid env var name part (aka slug).

type BlockRef

type BlockRef struct {
	Block     BlockName
	Ref, Slug string
}

a building block reference, a block name and version reference.

type BlockRefs

type BlockRefs struct {
	Blocks []*BlockRef
}

a collection of block references.

func (*BlockRefs) Contain

func (refs *BlockRefs) Contain(block BlockName) bool

returns true if the given block name is contained in this block references collection.

func (*BlockRefs) MaxNameLen

func (refs *BlockRefs) MaxNameLen() int

returns the length of the longest block name in the block references collection.

func (*BlockRefs) RefVersion

func (refs *BlockRefs) RefVersion(block BlockName) string

returns the version reference for the given block name in this block references collection or the empty string if the given block name is not contained.

func (*BlockRefs) String

func (refs *BlockRefs) String() string

returns a string representation of this blockref collection.

type Config added in v0.4.0

type Config struct {
	APIVersion      ConfigVersion
	Context         *ConfigContext
	AccountDefaults *AccountConfig
	Accounts        Accounts
}

represents a complete application configuration.

func ReadOrInitConfig added in v0.4.0

func ReadOrInitConfig(file string, cmd ConfiguredCommand) (*Config, *bytes.Buffer)

ReadOrInitConfig reads the configuration from the given file or - if omitted - from the default location if no configuration is provided and none can be found at the default location, a minimal config file with defaults is written to the default location.

func (*Config) AccountCredentials added in v0.4.0

func (c *Config) AccountCredentials(account string) *GitlabCredentials

returns the account for the given name (or in the current context if name is empty) will return nil if unset.

func (*Config) AddAccounts added in v0.4.0

func (c *Config) AddAccounts(vals Accounts)

convenience, calls c.AddOrRemoveAccount(a, false) for each given account record.

func (*Config) AddOrRemoveAccount added in v0.8.0

func (c *Config) AddOrRemoveAccount(val *AccountConfig, remove bool)

implements ConfigManager.AddOrRemoveAccount: adds an account record, will also fill in a blank name and possibly auto-select it if it is the only record.

func (*Config) Equals added in v0.4.0

func (c *Config) Equals(o *Config) bool

func (*Config) FindAccount added in v0.4.0

func (c *Config) FindAccount(name string) *AccountConfig

implements ConfigManager.FindAccount.

func (*Config) Flush added in v0.4.0

func (c *Config) Flush()

Flush updates the currently used config file with the current configuration object.

func (*Config) Marshal added in v0.4.0

func (c *Config) Marshal(raw bool) []byte

Marshal returns the current config as a YaML string.

func (*Config) ReadAccountsFromViper added in v0.6.0

func (c *Config) ReadAccountsFromViper()

"reads" all accounts from the externalized configuration for configuration version v1.

func (*Config) SetContext added in v0.4.0

func (c *Config) SetContext(ctx *ConfigContext)

implements ConfigManager.SetContext.

func (*Config) SetDefaults added in v0.4.0

func (c *Config) SetDefaults(val *AccountConfig)

implements ConfigManager.SetDefaults.

type ConfigContext added in v0.4.0

type ConfigContext struct {
	Account      *AccountConfig
	Repository   string
	LocalBaseDir string
}

the current configuration context.

func (*ConfigContext) Equals added in v0.4.0

func (c *ConfigContext) Equals(o *ConfigContext) bool

type ConfigManager added in v0.4.0

type ConfigManager interface {
	// overrides values in the configuration default
	// section with non-empty ones from the given record
	SetDefaults(val *AccountConfig)

	// overrides values in the configuration context
	// section from the given context
	SetContext(ctx *ConfigContext)

	// adds an account parameters record to the configuration
	AddOrRemoveAccount(val *AccountConfig, remove bool)

	// finds the account with the given name
	// if provided param is the empty string, returns
	// the currently selected account (if set)
	FindAccount(name string) *AccountConfig
}

generic management tasks for configuration settings.

type ConfigValue

type ConfigValue struct {
	Encode bool
	Value  string
}

an encodable value in the application configuration.

func (*ConfigValue) Equals added in v0.4.0

func (c *ConfigValue) Equals(o *ConfigValue) bool

type ConfigVersion added in v0.4.0

type ConfigVersion int

the apiVersion of the serialized configuration.

const (
	// available API versions enum.
	V0 ConfigVersion = iota
	V1
	LATEST = V1
)

func (ConfigVersion) Marshal added in v0.4.0

func (v ConfigVersion) Marshal() string

type ConfiguredCommand added in v0.4.0

type ConfiguredCommand interface {
	VerboseCommand
	GetConfig() *Config
}

a command that is verbose and also has a config.

type GitlabCredentials

type GitlabCredentials struct {
	Token, User, Pass, Address string
}

gitlab authentication parameters.

func (*GitlabCredentials) GetAddress added in v0.6.0

func (creds *GitlabCredentials) GetAddress() *net.TCPAddr

returns a valid tcp address or panics.

func (*GitlabCredentials) UpdateFromConfig

func (creds *GitlabCredentials) UpdateFromConfig(cmd ConfiguredCommand) *GitlabCredentials

updates all missing fields in this object from the current configuration object from the current context account if set and falling back to account defaults if set returns this object for chaining.

func (*GitlabCredentials) VerifyAuthExists

func (creds *GitlabCredentials) VerifyAuthExists() error

VerifyAuthExists verifies sufficient gitlab credentials (user/password or token) are provided.

type MockCommand added in v0.4.0

type MockCommand struct {
	Config     *Config
	Group, UID int
	Repo       string
}

this is for testing purposes.

func (*MockCommand) GetConfig added in v0.4.0

func (c *MockCommand) GetConfig() *Config

func (*MockCommand) IsVerbose added in v0.4.0

func (c *MockCommand) IsVerbose() bool

func (*MockCommand) TargetRepository added in v0.4.0

func (c *MockCommand) TargetRepository(_ *GitlabCredentials) *RepoInfo

func (*MockCommand) Verbosef added in v0.4.0

func (c *MockCommand) Verbosef(format string, args ...interface{})

type RepoCommand

type RepoCommand interface {
	TargetRepository(*GitlabCredentials) *RepoInfo
}

a command targeting a control repository.

type RepoInfo added in v0.6.0

type RepoInfo struct {
	Name, Path string
	Group, UID *int
	Instance   string
}

holds information on local clones.

func (*RepoInfo) String added in v0.6.0

func (r *RepoInfo) String() string

for debugging purposes.

func (*RepoInfo) Valid added in v0.6.0

func (r *RepoInfo) Valid() bool

a valid info object has all of its fields set.

type RepoInfos added in v0.6.0

type RepoInfos []*RepoInfo

a collection of local repo clone infos.

func (*RepoInfos) DoFor added in v0.6.0

func (infos *RepoInfos) DoFor(name string, call func(i *RepoInfo) error) error

calls the given function for all repo infos with the given name returns the first error it encounters.

func (*RepoInfos) MaxNameLen added in v0.6.0

func (infos *RepoInfos) MaxNameLen() int

returns the length of the longest file name in this collection.

type Validator added in v0.6.0

type Validator func(string) bool

a string validating function.

type VerboseCommand

type VerboseCommand interface {
	Verbosef(format string, args ...interface{})
	IsVerbose() bool
}

a verbose command will emit verbose output if verbosity is true.

Jump to

Keyboard shortcuts

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