config

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2025 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

Overview

Package config handles user inputs/UI elements.

Package config handles user inputs/UI elements.

Package config handles user inputs/UI elements.

Package config handles user inputs/UI elements.

Index

Constants

View Source
const (

	// ModTimeFormat is the expected modtime format
	ModTimeFormat = time.RFC3339
)

Variables

View Source
var (

	// ConfigXDG is the offset to the config for XDG
	ConfigXDG = configDirFile
	// ConfigHome is the offset to the config HOME
	ConfigHome = filepath.Join(".config", configDirFile)
	// ConfigEnv allows overriding the config detection
	ConfigEnv = environmentPrefix + "CONFIG_TOML"
	// YesValue is the string variant of 'Yes' (or true) items
	YesValue = strconv.FormatBool(true)
	// NoValue is the string variant of 'No' (or false) items
	NoValue = strconv.FormatBool(false)
	// TOTPDefaultColorWindow is the default coloring rules for totp
	TOTPDefaultColorWindow = []util.TimeWindow{{Start: 0, End: 5}, {Start: 30, End: 35}}
	// TOTPDefaultBetween is the default color window as a string
	TOTPDefaultBetween = func() []string {
		var results []string
		for _, w := range TOTPDefaultColorWindow {
			results = append(results, fmt.Sprintf("%d%s%d", w.Start, util.TimeWindowSpan, w.End))
		}
		return results
	}()
)
View Source
var (
	// EnvClipTimeout gets the maximum clipboard time
	EnvClipTimeout = environmentRegister(EnvironmentInt{
		environmentDefault: newDefaultedEnvironment(45,
			environmentBase{
				key:         clipCategory + "TIMEOUT",
				description: "Override the amount of time before totp clears the clipboard (seconds).",
			}),
		short: "clipboard max time",
	})
	// EnvJSONHashLength handles the hashing output length
	EnvJSONHashLength = environmentRegister(EnvironmentInt{
		environmentDefault: newDefaultedEnvironment(0,
			environmentBase{
				key:         jsonCategory + "HASH_LENGTH",
				description: fmt.Sprintf("Maximum string length of the JSON value when '%s' mode is set for JSON output.", output.JSONModes.Hash),
			}),
		short:   "hash length",
		canZero: true,
	})
	// EnvClipOSC52 indicates if OSC52 clipboard mode is enabled
	EnvClipOSC52 = environmentRegister(EnvironmentBool{
		environmentDefault: newDefaultedEnvironment(false,
			environmentBase{
				key:         clipCategory + "OSC52",
				description: "Enable OSC52 clipboard mode.",
			}),
	})
	// EnvTOTPEnabled indicates if TOTP is allowed
	EnvTOTPEnabled = environmentRegister(EnvironmentBool{
		environmentDefault: newDefaultedEnvironment(true,
			environmentBase{
				key:         totpCategory + "ENABLED",
				description: "Enable TOTP integrations.",
			}),
	})
	// EnvReadOnly indicates if in read-only mode
	EnvReadOnly = environmentRegister(EnvironmentBool{
		environmentDefault: newDefaultedEnvironment(false,
			environmentBase{
				key:         "READONLY",
				description: "Operate in readonly mode.",
			}),
	})
	// EnvClipEnabled indicates if clipboard is enabled
	EnvClipEnabled = environmentRegister(EnvironmentBool{
		environmentDefault: newDefaultedEnvironment(true,
			environmentBase{
				key:         clipCategory + "ENABLED",
				description: "Enable clipboard operations.",
			}),
	})
	// EnvColorEnabled indicates if colors are enabled
	EnvColorEnabled = environmentRegister(EnvironmentBool{
		environmentDefault: newDefaultedEnvironment(true,
			environmentBase{
				key:         "COLOR_ENABLED",
				description: "Enable terminal colors.",
			}),
	})
	// EnvHooksEnabled indicates if hooks are enabled
	EnvHooksEnabled = environmentRegister(EnvironmentBool{
		environmentDefault: newDefaultedEnvironment(true,
			environmentBase{
				key:         hookCategory + "ENABLED",
				description: "Enable hooks",
			}),
	})
	// EnvInteractive indicates if operating in interactive mode
	EnvInteractive = environmentRegister(EnvironmentBool{
		environmentDefault: newDefaultedEnvironment(true,
			environmentBase{
				key:         "INTERACTIVE",
				description: "Enable interactive mode.",
			}),
	})
	// EnvTOTPTimeout indicates when TOTP display should timeout
	EnvTOTPTimeout = environmentRegister(EnvironmentInt{
		environmentDefault: newDefaultedEnvironment(120,
			environmentBase{
				key:         totpCategory + "TIMEOUT",
				description: "Time, in seconds, to show a TOTP token before automatically exiting.",
			}),
		short: "max totp time",
	})
	// EnvTOTPEntry is the leaf token to use to store TOTP tokens
	EnvTOTPEntry = environmentRegister(EnvironmentString{
		environmentStrings: environmentStrings{
			environmentDefault: newDefaultedEnvironment("totp",
				environmentBase{
					key:         totpCategory + "ENTRY",
					description: "Entry name to store TOTP tokens within the database.",
				}),
			allowed: []string{"<string>"},
			flags:   []stringsFlags{canDefaultFlag},
		},
	})
	// EnvPlatform is the platform that the application is running on
	EnvPlatform = environmentRegister(EnvironmentString{
		environmentStrings: environmentStrings{
			environmentDefault: newDefaultedEnvironment(detectedValue,
				environmentBase{
					key:         "PLATFORM",
					description: "Override the detected platform.",
				}),
			allowed: platform.Systems.List(),
		},
	})
	// EnvStore is the location of the keepass file/store
	EnvStore = environmentRegister(EnvironmentString{
		environmentStrings: environmentStrings{
			environmentDefault: newDefaultedEnvironment("",
				environmentBase{
					key:         "STORE",
					description: "The kdbx file to operate on.",
					requirement: "must be set",
				}),
			allowed: []string{fileExample},
			flags:   []stringsFlags{canExpandFlag},
		},
	})
	// EnvHookDir is the directory of hooks to execute
	EnvHookDir = environmentRegister(EnvironmentString{
		environmentStrings: environmentStrings{
			environmentDefault: newDefaultedEnvironment("",
				environmentBase{
					key:         hookCategory + "DIRECTORY",
					description: "The path to a directory of hooks to execute on actions against the database.",
				}),
			allowed: []string{"<directory>"},
			flags:   []stringsFlags{canDefaultFlag, canExpandFlag},
		},
	})
	// EnvClipCopy allows overriding the clipboard copy command
	EnvClipCopy = environmentRegister(EnvironmentArray{
		environmentStrings: environmentStrings{
			environmentDefault: newDefaultedEnvironment("",
				environmentBase{
					key:         clipCategory + "COPY_COMMAND",
					description: "Override the detected platform copy command.",
				}),
			flags: []stringsFlags{isCommandFlag},
		},
	})
	// EnvClipPaste allows overriding the clipboard paste command
	EnvClipPaste = environmentRegister(EnvironmentArray{
		environmentStrings: environmentStrings{
			environmentDefault: newDefaultedEnvironment("",
				environmentBase{
					key:         clipCategory + "PASTE_COMMAND",
					description: "Override the detected platform paste command.",
				}),
			flags: []stringsFlags{isCommandFlag},
		},
	})
	// EnvTOTPColorBetween handles terminal coloring for TOTP windows (seconds)
	EnvTOTPColorBetween = environmentRegister(EnvironmentArray{
		environmentStrings: environmentStrings{
			environmentDefault: newDefaultedEnvironment(strings.Join(TOTPDefaultBetween, arrayDelimiter),
				environmentBase{
					key: totpCategory + "COLOR_WINDOWS",
					description: fmt.Sprintf(`Override when to set totp generated outputs to different colors,
must be a list of one (or more) rules where a '%s' delimits the start and end second (0-60 for each).`, util.TimeWindowSpan),
				}),
			flags:   []stringsFlags{canDefaultFlag},
			allowed: exampleColorWindows,
		},
	})
	// EnvKeyFile is an keyfile for the database
	EnvKeyFile = environmentRegister(EnvironmentString{
		environmentStrings: environmentStrings{
			environmentDefault: newDefaultedEnvironment("",
				environmentBase{
					key:         credsCategory + "KEY_FILE",
					requirement: requiredKeyOrKeyFile,
					description: "A keyfile to access/protect the database.",
				}),
			allowed: []string{"keyfile"},
			flags:   []stringsFlags{canDefaultFlag, canExpandFlag},
		},
	})
	// EnvDefaultModTime is modtime override ability for entries
	EnvDefaultModTime = environmentRegister(EnvironmentString{
		environmentStrings: environmentStrings{
			environmentDefault: newDefaultedEnvironment("",
				environmentBase{
					key:         defaultCategory + "MODTIME",
					description: fmt.Sprintf("Input modification time to set for the entry\n\nExpected format: %s.", ModTimeFormat),
				}),
			flags:   []stringsFlags{canDefaultFlag},
			allowed: []string{"modtime"},
		},
	})
	// EnvJSONMode controls how JSON is output in the 'data' field
	EnvJSONMode = environmentRegister(EnvironmentString{
		environmentStrings: environmentStrings{
			environmentDefault: newDefaultedEnvironment(string(output.JSONModes.Hash),
				environmentBase{
					key:         jsonCategory + "MODE",
					description: fmt.Sprintf("Changes what the data field in JSON outputs will contain.\n\nUse '%s' with CAUTION.", output.JSONModes.Raw),
				}),
			flags:   []stringsFlags{canDefaultFlag},
			allowed: output.JSONModes.List(),
		},
	})
	// EnvTOTPFormat supports formatting the TOTP tokens for generation of tokens
	EnvTOTPFormat = environmentRegister(EnvironmentFormatter{
		environmentBase: environmentBase{
			key:         totpCategory + "OTP_FORMAT",
			description: "Override the otpauth url used to store totp tokens. It must have ONE format string ('%s') to insert the totp base code.",
		}, fxn: formatterTOTP, allowed: "otpauth//url/%s/args...",
	})
	// EnvPasswordMode indicates how the password is read
	EnvPasswordMode = environmentRegister(EnvironmentString{
		environmentStrings: environmentStrings{
			environmentDefault: newDefaultedEnvironment(string(DefaultKeyMode),
				environmentBase{
					key:         credsCategory + "PASSWORD_MODE",
					requirement: "must be set to a valid mode when using a key",
					description: fmt.Sprintf(`How to retrieve the database store password. Set to '%s' when only using a key file.
Set to '%s' to ignore the set key value`, noKeyMode, IgnoreKeyMode),
				}),
			allowed: []string{string(AskKeyMode), string(commandKeyMode), string(IgnoreKeyMode), string(noKeyMode), string(plainKeyMode)},
			flags:   []stringsFlags{canDefaultFlag},
		},
	})

	// EnvPasswordGenWordCount is the number of words that will be selected for password generation
	EnvPasswordGenWordCount = environmentRegister(EnvironmentInt{
		environmentDefault: newDefaultedEnvironment(8,
			environmentBase{
				key:         genCategory + "WORD_COUNT",
				description: "Number of words to select and include in the generated password.",
			}),
		short: "word count",
	})
	// EnvPasswordGenTitle indicates if titling (e.g. uppercasing) will occur to words
	EnvPasswordGenTitle = environmentRegister(EnvironmentBool{
		environmentDefault: newDefaultedEnvironment(true,
			environmentBase{
				key:         genCategory + "TITLE",
				description: "Title words during password generation.",
			}),
	})
	// EnvPasswordGenTemplate is the output template for controlling how output words are placed together
	EnvPasswordGenTemplate = environmentRegister(EnvironmentString{
		environmentStrings: environmentStrings{
			environmentDefault: newDefaultedEnvironment("{{range $i, $val := .}}{{if $i}}-{{end}}{{$val.Text}}{{end}}",
				environmentBase{
					key:         genCategory + "TEMPLATE",
					description: fmt.Sprintf("The go text template to use to format the chosen words into a password. Available fields: %s.", util.TextPositionFields()),
				}),
			allowed: []string{"<go template>"},
			flags:   []stringsFlags{canDefaultFlag},
		},
	})
	// EnvPasswordGenWordList is the command text to generate the word list
	EnvPasswordGenWordList = environmentRegister(EnvironmentArray{
		environmentStrings: environmentStrings{
			environmentDefault: newDefaultedEnvironment("",
				environmentBase{
					key:         genCategory + "WORDS_COMMAND",
					description: "Command to retrieve the word list to use for password generation (must be split by newline).",
				}),
			flags: []stringsFlags{isCommandFlag},
		},
	})
	// EnvLanguage is the language to use for everything
	EnvLanguage = environmentRegister(EnvironmentString{
		environmentStrings: environmentStrings{
			environmentDefault: newDefaultedEnvironment("en-US",
				environmentBase{
					key:         "LANGUAGE",
					description: "Language to run under.",
				}),
			allowed: []string{"<language code>"},
			flags:   []stringsFlags{canDefaultFlag},
		},
	})
	// EnvPasswordGenEnabled indicates if password generation is enabled
	EnvPasswordGenEnabled = environmentRegister(EnvironmentBool{
		environmentDefault: newDefaultedEnvironment(true,
			environmentBase{
				key:         genCategory + "ENABLED",
				description: "Enable password generation.",
			}),
	})
	// EnvPasswordGenChars allows for restricting which characters can be used
	EnvPasswordGenChars = environmentRegister(EnvironmentString{
		environmentStrings: environmentStrings{
			environmentDefault: newDefaultedEnvironment("",
				environmentBase{
					key:         genCategory + "CHARACTERS",
					description: "The set of allowed characters in output words (empty means any character is allowed).",
				}),
			allowed: []string{"<list of characters>"},
			flags:   []stringsFlags{canDefaultFlag},
		},
	})
)

Functions

func CanColor

func CanColor() (bool, error)

CanColor indicates if colorized output is allowed (or disabled)

func DefaultTOML added in v1.4.0

func DefaultTOML() (string, error)

DefaultTOML will load the internal, default TOML with additional comment markups

func LoadConfig added in v1.4.0

func LoadConfig(r io.Reader, loader Loader) error

LoadConfig will read the input reader and use the loader to source configuration files

func LoadConfigFile added in v1.4.0

func LoadConfigFile(path string) error

LoadConfigFile will load a path as the configuration it will also set the environment

func NewConfigFiles added in v1.4.0

func NewConfigFiles() []string

NewConfigFiles will get the list of candidate config files

Types

type AskPassword

type AskPassword func() (string, error)

AskPassword is a function to prompt for passwords (when required)

type EnvironmentArray added in v1.4.0

type EnvironmentArray struct {
	// contains filtered or unexported fields
}

EnvironmentArray is an array of strings variable

func (EnvironmentArray) Get added in v1.4.0

func (e EnvironmentArray) Get() []string

Get indicates the item should be queried as an array

type EnvironmentBool

type EnvironmentBool struct {
	// contains filtered or unexported fields
}

EnvironmentBool are environment settings that are booleans

func (EnvironmentBool) Get

func (e EnvironmentBool) Get() bool

Get will get the boolean value for the setting

type EnvironmentFormatter

type EnvironmentFormatter struct {
	// contains filtered or unexported fields
}

EnvironmentFormatter allows for sending a string into a get request

func (EnvironmentFormatter) Get

func (e EnvironmentFormatter) Get(value string) string

Get will retrieve the value with the formatted input included

func (EnvironmentFormatter) Key added in v1.3.1

func (e EnvironmentFormatter) Key() string

type EnvironmentInt

type EnvironmentInt struct {
	// contains filtered or unexported fields
}

EnvironmentInt are environment settings that are integers

func (EnvironmentInt) Get

func (e EnvironmentInt) Get() (int64, error)

Get will get the integer value for the setting

type EnvironmentString

type EnvironmentString struct {
	// contains filtered or unexported fields
}

EnvironmentString represents a string variable

func (EnvironmentString) Get

func (e EnvironmentString) Get() string

Get will read the string from the environment

type Key

type Key struct {
	// contains filtered or unexported fields
}

Key is a wrapper to help manage the returned key

func NewKey

func NewKey(defaultKeyModeType KeyModeType) (Key, error)

NewKey will create a new key

func (Key) Ask

func (k Key) Ask() bool

Ask will indicate if prompting is required to get the key

func (Key) Read

func (k Key) Read(ask AskPassword) (string, error)

Read will read the key as configured by the mode

type KeyModeType

type KeyModeType string

KeyModeType are valid ways to get the key

const (

	// AskKeyMode is the mode in which the user is prompted for key input (each time)
	AskKeyMode KeyModeType = "ask"

	// IgnoreKeyMode will ignore the value set in the key (acts like no key)
	IgnoreKeyMode KeyModeType = "ignore"

	// DefaultKeyMode is the default operating keymode if NOT set
	DefaultKeyMode = commandKeyMode
)

type Loader added in v1.4.0

type Loader func(string) (io.Reader, error)

Loader indicates how included files should be sourced

Directories

Path Synopsis
Package store is the internal memory store for loaded configuration settings
Package store is the internal memory store for loaded configuration settings

Jump to

Keyboard shortcuts

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