cmdconfig

package
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2024 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CmdBase64       = "base64"
	CmdCompare      = "compare"
	CmdCSV          = "csv"
	CmdGenerate     = "generate"
	CmdGet          = "get"
	CmdSetEnv       = "set-env"
	CmdUpdateConfig = "update-config"
	CmdVersion      = "version"
)
View Source
const (
	FlagAll      = "all"
	FlagBase64   = "base64"
	FlagCompare  = "compare"
	FlagCSV      = "csv"
	FlagDel      = "del"
	FlagDryRun   = "dry-run"
	FlagEnv      = "env"
	FlagExtend   = "extend"
	FlagGenerate = "generate"
	FlagGet      = "get"
	FlagKey      = "key"
	FlagMerge    = "merge"
	FlagPrefix   = "prefix"
	FlagSep      = "sep"
	FlagValue    = "value"
	FlagVersion  = "version"
	FlagOS       = "os"
	FlagFormat   = "format"
)
View Source
const ExportFormat = OtherExportFormat
View Source
const FileNameConfigGo = "config.go"

FileNameConfigGo for config.go

View Source
const FileNameFnGo = "fn.go"

FileNameFnGo for fn.go

View Source
const FileNameTemplateGo = "template.go"

FileNameTemplateGo for template.go

View Source
const FileTypeEnv = ".env" // e.g. .env
View Source
const FileTypeJSON = ".json" // e.g. config.json
View Source
const FileTypeYAML = ".yaml" // e.g. config.yaml
View Source
const LineBreak = OtherLineBreak
View Source
const OtherExportFormat = "export %v=%v"
View Source
const OtherLineBreak = "\n"
View Source
const OtherUnsetFormat = "unset %v"
View Source
const SamplePrefix = "sample."
View Source
const UnsetFormat = OtherUnsetFormat
View Source
const WindowsExportFormat = "set %v=%v"

Note the difference between set and setx https://superuser.com/a/916652/537059

View Source
const WindowsLineBreak = "\r\n"
View Source
const WindowsUnsetFormat = "set %v=\"\""

Variables

View Source
var ErrCmdConfig = errors.NewCause("cmdconfig")
View Source
var ErrDuplicateKey = func(key string) error {
	return errors.NewWithCausef(ErrCmdConfig, "duplicate key %s", key)
}
View Source
var ErrMissingKey = func(key string) error {
	return errors.NewWithCausef(ErrCmdConfig, "missing key %s", key)
}
View Source
var ErrNotImplemented = errors.NewWithCausef(ErrCmdConfig, "not implemented")
View Source
var ErrParentNotFound = errors.NewWithCausef(
	ErrCmdConfig, "parent config not found")

Functions

func Copy added in v0.5.0

func Copy(src, dst string) error

Copy the src file to dst. Return an error if dst exists. Inspired by https://stackoverflow.com/a/21061062/639133

func FormatKey added in v0.4.0

func FormatKey(prefix, keyWithPrefix string) string

FormatKey removes the prefix and converts env var to golang var, e.g. APP_FOO_BAR becomes FooBar

func GetTemplate added in v0.5.0

func GetTemplate(fileName string) (s string, err error)

GetTemplate returns the text template for the given file name.

func GetTemplateParams added in v0.4.0

func GetTemplateParams(value string) (params []string)

GetTemplateParams from template, e.g. passing in "Fizz{{.Buz}}{{.Meh}}" should return ["Buz", "Meh"]

func KeyExtensionsDir added in v0.12.0

func KeyExtensionsDir(prefix string) string

func KeyPrefixExtensions added in v0.12.0

func KeyPrefixExtensions(prefix string) string

func KeyPrefixTemplate added in v0.12.0

func KeyPrefixTemplate(prefix string) string

func Main

func Main(version string)

Main function for cmd/configu. The configu command can be customized by copying the code below. Try not to change the default behaviour, e.g. custom flags must only add functionality

func MarshalENV added in v0.9.0

func MarshalENV(c *conf) (b []byte, err error)

MarshalENV key value map to .env file bytes

func ReadConfigFile added in v0.9.0

func ReadConfigFile(appDir, env string) (configPath string, b []byte, err error)

func ToPrivate

func ToPrivate(str string) string

ToPrivate lowercases the first character of str

func UnmarshalENV added in v0.9.0

func UnmarshalENV(b []byte) (m map[string]string, err error)

UnmarshalENV .env file bytes to key value map. Syntax rules as per this comment https://github.com/mozey/config/issues/24#issue-1091975787

Types

type ArgMap

type ArgMap []string

ArgMap for parsing flags with multiple keys

func (*ArgMap) Set

func (a *ArgMap) Set(value string) error

func (*ArgMap) String

func (a *ArgMap) String() string

type CmdIn

type CmdIn struct {

	// AppDir is the application root
	AppDir string
	// Prefix for env vars
	Prefix string
	// PrintVersion for printing the build version
	PrintVersion bool
	// Env selects the config file
	Env string
	// All makes the cmd apply to all config files in APP_DIR, including samples
	// https://github.com/mozey/config/issues/2
	All bool
	// Del deletes the specified keys
	Del bool
	// Compare config file keys
	Compare string
	// Keys to update
	Keys ArgMap
	// Value to update
	Values ArgMap
	// PrintValue for the given key
	PrintValue string
	// Generate config helper
	Generate string
	CSV      bool
	Sep      string
	DryRun   bool
	// Base64 encode config file
	Base64 bool
	// OS overrides the compiled x-platform config
	OS string
	// Override config file format
	Format string
	// Extend config
	Extend ArgMap
	// Merge with parent config
	Merge bool
	// contains filtered or unexported fields
}

CmdIn for use with command functions

func NewCmdIn added in v0.10.0

func NewCmdIn(params CmdInParams) *CmdIn

NewCmdIn constructor for CmdIn

func ParseFlags

func ParseFlags(version string) *CmdIn

ParseFlags before calling Cmd

func (*CmdIn) Process

func (in *CmdIn) Process(out *CmdOut) (exitCode int, err error)

Process the output of the Cmd func. For example, this is where results are printed to stdout or disk IO happens, depending on the whether the in.DryRun flag was set

func (*CmdIn) Valid added in v0.10.0

func (in *CmdIn) Valid() error

Valid returns true if the command input is valid. It may also set default values

type CmdInParams added in v0.10.0

type CmdInParams struct {
	// Version to print with the version flag
	Version string
}

type CmdOut

type CmdOut struct {
	// Cmd is the unique command that was executed
	Cmd string
	// ExitCode can be non-zero if the err returned is nil,
	// that means the program did not have any internal error,
	// but the command "failed", i.e. non-zero exit code
	ExitCode int
	// Buf of cmd output
	Buf *bytes.Buffer
	// Files to write if in.DryRun is not set
	Files Files
}

CmdOut for use with Cmd function

func Cmd

func Cmd(in *CmdIn) (out *CmdOut, err error)

Cmd runs a command given flags and input from the user

type File added in v0.5.0

type File struct {
	// Path to file
	Path string
	// Buf for new file content
	Buf *bytes.Buffer
}

type Files added in v0.5.0

type Files []File

func (Files) Print added in v0.5.0

func (files Files) Print(buf *bytes.Buffer)

Print file paths and contents to buf

func (Files) Save added in v0.5.0

func (files Files) Save(buf *bytes.Buffer) (err error)

Save file contents to disk, and print paths to buf

type GenerateData added in v0.4.0

type GenerateData struct {
	Prefix       string
	AppDir       string
	Keys         []GenerateKey
	TemplateKeys []TemplateKey
	// KeyMap can be used to lookup an index in Keys given a key
	KeyMap map[string]int
}

func NewGenerateData added in v0.4.0

func NewGenerateData(in *CmdIn) (data *GenerateData, err error)

type GenerateKey added in v0.4.0

type GenerateKey struct {
	KeyPrefix  string
	KeyPrivate string
	Key        string
}

type TemplateKey

type TemplateKey struct {
	GenerateKey
	ExplicitParams string
	Params         []TemplateParam
}

TemplateKey, e.g. APP_TEMPLATE_*

type TemplateParam added in v0.4.0

type TemplateParam struct {
	KeyPrivate string
	Key        string
	// Implicit is set if the param is also a config key
	Implicit bool
}

Jump to

Keyboard shortcuts

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