cogs

package module
v0.7.3 Latest Latest
Warning

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

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

README

installation:

With go:

  • clone this repo, cd into it
  • go build -o $GOPATH/bin ./cmd/cogs

Without go, PLatform can be Linux/Windows/Darwin:

PL="Darwin" VR="0.7.3" \
  curl -SLk \ 
  "github.com/Bestowinc/cogs/releases/download/v${VR}/cogs_${VR}_${PL}_x86_64.tar.gz" | \
  tar xvz -C /usr/local/bin cogs
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.
  --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:

name = "basic_example"

# key value pairs for a context are defined under <ctx>.vars
[docker.vars]
var = "var_value"
other_var = "other_var_value"

[sops]
# a default path to be inherited can be defined under <ctx>.path
path = ["./test_files/manifest.yaml", ".subpath"]
[sops.vars]
# a <var>.path key can map to four valid types:
# 1. path value is "string_value" - indicating a single file to look through
# 2. path value is [] - thus <ctx>.path will be inherited
# 3. path value is a ["two_index, "array"] - either index being [] or "string_value":
# -  [[], "subpath"] - path will be inherited from <ctx>.path if present
# -  ["path", []] - subpath will be inherited from <ctx>.path if present
# 4. ["path", "subpath"] - nothing will be inherited
var1.path = ["./test_files/manifest.yaml", ".subpath"]
var2.path = []
var3.path = [[], ".other_subpath"]
# dangling variable should return {"empty_var": ""} since only name override was defined
empty_var.name = "some_name"
# key value pairs for an encrypted context are defined under <ctx>.enc.vars
[sops.enc.vars]
yaml_enc.path = "./test_files/test.enc.yaml"
dotenv_enc = {path = "./test_files/test.enc.env", name = "DOTENV_ENC"}
json_enc.path = "./test_files/test.enc.json"

[kustomize]
path = ["./test_files/kustomization.yaml", ".configMapGenerator.[0].literals"]
# a default deserialization path to be inherited can be defined under <ctx>.path
# once <var>.path has been traversed, attempt to deserialize the returned object
# as if it was in dotenv format
type = "dotenv"
[kustomize.vars]
# var1.name = "VAR_1" means that the key alias "VAR_1" will
# be searched for to retrieve the var1 value
var1 = {path = [], name = "VAR_1"}
var2 = {path = [], name = "VAR_2"}
var3 = {path = [[], ".jsonMap"], type = "json"}

running example data locally:

  • gpg --import ./test_files/sops_functional_tests_key.asc should be run to import the test private key used for encrypted dummy data
  • Building binary locally : go build -o $GOPATH/bin ./cmd/cogs
  • Kustomize style var retrieval: cogs gen kustomize ./basic.cog.toml
  • Encrypted var retrieval: cogs gen sops ./basic.cog.toml
  • some-service.cog.toml shows how a toml definition correlates to the JSON counterpart

further references

TOML spec envsubst cheatsheet: yq expressions

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:

  • envsubst warning, make sure that any --envsubst tags retain a file's membership as valid TOML:
# yes
[env.vars]
thing = "${THING_VAR}"

# NO
[sloppy.vars]${NO}
thing = "${THING_VAR}"

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EnvSubst bool = false

EnvSubst decides whether to use environmental substitution or not

View Source
var NoEnc bool = false

NoEnc decides whether to output encrypted variables or now

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 undelying 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 cogfilepath to return a string map

type Format added in v0.6.0

type Format string

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

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 dereive the deserliazied value for a given Link

func (ReadType) GetUnmarshal added in v0.7.3

func (t ReadType) GetUnmarshal() (unmarshalFn, error)

GetUnmarshal returns the corresponding function to unmarshal a given read type

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