cogs

package module
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2021 License: MPL-2.0 Imports: 20 Imported by: 0

README

COGS: COnfiguration manaGement S

cogs is a cli tool that allows generation of configuration files through different references sources.

Sources of reference can include:

cogs allows one to deduplicate sources of truth by maintaining a source of reference (the cog file) that points to the location of values (such as port numbers and password strings).

installation:

With go:

Clone this repo and cd into it.

go build -o $GOPATH/bin/ ./cmd/cogs

Without go

PLatform can be Linux/Windows/Darwin:

PL="Darwin" VR="0.9.1" \
  curl -SLk \
  "github.com/Bestowinc/cogs/releases/download/v${VR}/cogs_${VR}_${PL}_x86_64.tar.gz" | \
  tar xvz -C /usr/local/bin cogs

help string:

COGS COnfiguration manaGement S

Usage:
  cogs gen <ctx> <cog-file> [options]

Options:
  -h --help        Show this screen.
  --version        Show version.
  --no-enc, -n     Skips fetching encrypted vars.
  --no-decrypt	   Skipts decrypting encrypted vars.
  --envsubst, -e   Perform environmental substitution on the given cog file.
  --keys=<key,>    Include specific keys, comma separated.
  --not=<key,>     Exclude specific keys, comma separated.
  --out=<type>     Configuration output type [default: json].
                   <type>: json, toml, yaml, dotenv, raw.

  --export, -x     If --out=dotenv: Prepends "export " to each line.
  --preserve, -p   If --out=dotenv: Preserves variable casing.
  --sep=<sep>      If --out=raw:    Delimits values with a <sep>arator.

cogs gen - outputs a flat and serialized K:V array

annotated spec:

 # every cog manifest should have a name key that corresponds to a string
name = "basic example"

# key value pairs for a context/ctx are defined under <ctx>.vars
# try running `cogs gen basic ./examples/1.basic.cog.toml` to see what output
# cogs generates
[basic.vars]
var = "var_value"
other_var = "other_var_value"

# if <var>.path is given a string value,
# cogs will look for the key name of <var> in the file that that corresponds to
# the <var>.path key,
# returning the corresponding value
manifest_var.path = "../test_files/manifest.yaml"
# try removing manifest_var from "./test_files/manifest.yaml" and see what happens

# some variables can set an explicit key name to look for instead of defaulting
# to look for the key name "<var>":
# if <var>.name is defined then cogs will look for a key name that matches <var>.name
look_for_manifest_var.path = "../test_files/manifest.yaml"
look_for_manifest_var.name = "manifest_var"

# dangling variable names should return an error
# uncomment the line below and run `cogs gen basic ./examples/1.basic.cog.toml`:
# empty_var.name = "some_name"

example data:

The example data (in ./examples) are ordered by increasing complexity and should be used as a tutorial. Run cogs gen on the files in the order below, then read the file to see how the underlying logic is used.

  1. basic example:
    • cogs gen basic 1.basic.cog.toml
  2. HTTP examples:
    • cogs gen get 2.http.cog.toml, GET example
    • cogs gen post 2.http.cog.toml, POST example:
  3. secret values and paths example:
    • gpg --import ./test_files/sops_functional_tests_key.asc should be run to import the test private key used for encrypted dummy data
    • cogs gen sops 3.secrets.cog.toml
  4. read types example:
    • cogs gen kustomize 4.read_types.cog.toml
  5. advanced patterns example:
    • cogs gen complex_json 5.advanced.cog.toml
  6. envsubst patterns example:
    • NVIM=nvim cogs gen envsubst 6.envsubst.cog.toml --envsubst

envsubst cheatsheet:

Expression Meaning
${var} Value of $var
${var-${DEFAULT}} If $var is not set, evaluate expression as ${DEFAULT}
${var:-${DEFAULT}} If $var is not set or is empty, evaluate expression as ${DEFAULT}
${var=${DEFAULT}} If $var is not set, evaluate expression as ${DEFAULT}
${var:=${DEFAULT}} If $var is not set or is empty, evaluate expression as ${DEFAULT}
$$var Escape expressions. Result will be the string $var
${var^} Uppercase first character of $var
${var^^} Uppercase all characters in $var
${var,} Lowercase first character of $var
${var,,} Lowercase all characters in $var
${#var} String length of $var
${var:n} Offset $var n characters from start
${var: -n} Offset $var n characters from end
${var:n:len} Offset $var n characters with max length of len
${var#pattern} Strip shortest pattern match from start
${var##pattern} Strip longest pattern match from start
${var%pattern} Strip shortest pattern match from end
${var%%pattern} Strip longest pattern match from end
${var/pattern/replacement} Replace as few pattern matches as possible with replacement
${var//pattern/replacement} Replace as many pattern matches as possible with replacement
${var/#pattern/replacement} Replace pattern match with replacement from $var start
${var/%pattern/replacement} Replace pattern match with replacement from $var end

Notes and references:

envsubst warning: make sure that any environmental substition declarations allow a file to be parsed as TOML without the usage of the --envsubst flag:

# valid envsubst definitions can be placed anywhere string values are valid
["${ENV}".vars]
thing = "${THING_VAR}"
# the `${ENV}` below creates a TOML read error
[env.vars]${ENV}
thing = "${THING_VAR}"

Further references

Documentation

Index

Constants

View Source
const (
	ErrNoEncAndNoDecrypt = errConst("NoEnc and NoDecrypt cannot both be true")
)

Errors raised by package x.

Variables

View Source
var DefaultMethod string = http.MethodGet

DefaultMethod uses GET for the default request type

View Source
var EnvSubst bool = false

EnvSubst decides whether to use environmental substitution or not

View Source
var NoDecrypt bool = false

NoDecrypt decides whether to decrypt encrypted values, not compatible with NoEnc

View Source
var NoEnc bool = false

NoEnc decides whether to handle encrypted variables

View Source
var RecursionLimit int = 12

RecursionLimit is the limit used to define when to abort successive traversals of gears

Functions

func InList added in v0.7.0

func InList(s string, ss []string) bool

InList verifies that a given string is in a string slice

func IsEnvFile added in v0.6.0

func IsEnvFile(path string) bool

IsEnvFile returns true if a given file path corresponds to a .env file

func IsJSONFile added in v0.6.0

func IsJSONFile(path string) bool

IsJSONFile returns true if a given file path corresponds to a JSON file

func IsSimpleValue added in v0.7.1

func IsSimpleValue(i interface{}) bool

IsSimpleValue is intended to see if the underlying value allows a flat map to be retained

func IsTOMLFile added in v0.6.0

func IsTOMLFile(path string) bool

IsTOMLFile returns true if a given file path corresponds to a TOML file

func IsYAMLFile added in v0.6.0

func IsYAMLFile(path string) bool

IsYAMLFile returns true if a given file path corresponds to a YAML file

func OutputCfg added in v0.6.0

func OutputCfg(link *Link, outputType Format) (interface{}, error)

OutputCfg returns the corresponding value for a given Link struct

func SimpleValueToString added in v0.7.2

func SimpleValueToString(i interface{}) (str string, err error)

SimpleValueToString converts an underlying type to a string, returning an error if it is not a simple value

Types

type CfgMap added in v0.7.0

type CfgMap map[string]interface{}

CfgMap is meant to represent a map with values of one or more unknown types

func Generate

func Generate(ctxName, cogPath string, outputType Format, filter LinkFilter) (CfgMap, error)

Generate is a top level command that takes an context name argument and cog file path to return a string map

type Format added in v0.6.0

type Format string

Format represents the final marshalled k/v output type from a resolved Gear TODO reconcile readType and Format patterns

const (
	JSON   Format = "json"
	YAML   Format = "yaml"
	TOML   Format = "toml"
	Dotenv Format = "dotenv"
	Raw    Format = "raw"
)

Formats for respective object notation

func FormatForPath added in v0.6.0

func FormatForPath(path string) Format

FormatForPath returns the correct format given the path to a file

func FormatLinkInput added in v0.7.2

func FormatLinkInput(link *Link) (format Format)

FormatLinkInput returns the correct format given the readType

func (Format) Validate added in v0.6.0

func (t Format) Validate() error

Validate ensures that a string maps to a valid Format

type Gear

type Gear struct {
	Name string
	// contains filtered or unexported fields
}

Gear represents one of the contexts in a cog manifest. The term "gear" is used to refer to the operating state of a machine (similar to how a microservice can operate locally or in a remote environment) rather than a gear object. The term "switching gears" is an apt representation of how one Cog manifest file can have many contexts/environments

func (*Gear) ResolveMap

func (g *Gear) ResolveMap(ctx baseContext) (CfgMap, error)

ResolveMap outputs the flat associative string, resolving potential filepath pointers held by Link objects by calling the .SetValue() method

func (*Gear) SetName

func (g *Gear) SetName(name string)

SetName sets the gear name to the provided string

type Link struct {
	KeyName    string      // the key name defined in the context file
	SearchName string      // same as keyName unless redefined using the `name` key: var.name="other_name"
	Value      interface{} // Holds a complex or simple value for the given Link
	Path       string      // filepath string where Link can be resolved
	SubPath    string      // object traversal string used to resolve Link if not at top level of document (yq syntax)
	// contains filtered or unexported fields
}

Link holds all the data needed to resolve one string key value pair

func (Link) String added in v0.7.0

func (c Link) String() string

String holds the string representation of a Link struct

type LinkFilter added in v0.7.1

type LinkFilter func(LinkMap) (LinkMap, error)

LinkFilter if a function meant to filter a LinkMap

type LinkMap added in v0.7.0

type LinkMap map[string]*Link

LinkMap is used by Resolver to output the final k/v associative array

func Exclude added in v0.7.0

func Exclude(exclusionList []string, linkMap LinkMap) LinkMap

Exclude produces a laundered map with exclusionList values missing

type ReadType added in v0.7.3

type ReadType string

ReadType represents the logic used to derive the deserliazied value for a given Link

func (ReadType) String added in v0.7.3

func (t ReadType) String() string

func (ReadType) Validate added in v0.7.3

func (t ReadType) Validate() error

Validate ensures that a string is a valid readType enum

type Resolver

type Resolver interface {
	ResolveMap(baseContext) (CfgMap, error)
	SetName(string)
}

Resolver is meant to define an object that returns the final string map to be used in a configuration resolving any paths and sub paths defined in the underling config map

type Visitor added in v0.6.0

type Visitor interface {
	SetValue(*Link) error
	Errors() []error
}

Visitor allows a query path to return the underlying value for a given visitor

func NewDotenvVisitor added in v0.6.0

func NewDotenvVisitor(buf []byte) (Visitor, error)

NewDotenvVisitor returns a visitor object that satisfies the Visitor interface attempting to turn a supposed dotenv byte slice into a *yaml.Node object

func NewJSONVisitor added in v0.5.0

func NewJSONVisitor(buf []byte) (Visitor, error)

NewJSONVisitor returns a visitor object that satisfies the Visitor interface attempting to turn a supposed JSON byte slice into a *yaml.Node object

func NewTOMLVisitor added in v0.6.0

func NewTOMLVisitor(buf []byte) (Visitor, error)

NewTOMLVisitor returns a visitor object that satisfies the Visitor interface attempting to turn a supposed TOML byte slice into a *yaml.Node object

func NewYAMLVisitor added in v0.6.0

func NewYAMLVisitor(buf []byte) (Visitor, error)

NewYAMLVisitor returns a visitor object that satisfies the Visitor interface

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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