commit 3afcc98cefdb6eccd6d9e11f59f2d628ba66c29b
parent 21005e7023ef4f3e06d423019b46da77f249b923
Author: Sean Enck <sean@ttypty.com>
Date: Tue, 11 Oct 2022 19:01:08 -0400
moving env vars to internal command
Diffstat:
5 files changed, 81 insertions(+), 44 deletions(-)
diff --git a/Makefile b/Makefile
@@ -2,8 +2,9 @@ DESTDIR :=
BUILD := bin/
TARGET := $(BUILD)lb
TESTDIR := $(sort $(dir $(wildcard internal/**/*_test.go)))
-DOC := contrib/doc.sections
+DOC := $(BUILD)doc.text
MAN := $(BUILD)lb.man
+DOCTEXT := contrib/doc.sections
.PHONY: $(TESTDIR)
@@ -22,8 +23,14 @@ check: $(TARGET) $(TESTDIR)
clean:
rm -rf $(BUILD)
+$(DOC): $(TARGET) $(DOCTEXT)
+ @cat $(DOCTEXT) > $(DOC)
+ @echo "[environment variables]" >> $(DOC)
+ @echo >> $(DOC)
+ $(TARGET) env -defaults >> $(DOC)
+
$(MAN): $(TARGET) $(DOC)
- help2man --include $(DOC) -h help -v version ./$(TARGET) > $(MAN)
+ help2man --include $(DOC) -h help -v version -o $(MAN) ./$(TARGET)
install:
install -Dm755 $(TARGET) $(DESTDIR)bin/lb
diff --git a/cmd/main.go b/cmd/main.go
@@ -29,6 +29,7 @@ const (
versionCommand = "version"
helpCommand = "help"
removeCommand = "rm"
+ envCommand = "env"
)
var (
@@ -45,24 +46,26 @@ type (
)
func printSubCommand(name, args, desc string) {
- printCommandText(" ", args, name, desc)
+ printCommandText(args, " "+name, desc)
}
func printCommand(name, args, desc string) {
- printCommandText(" ", args, name, desc)
+ printCommandText(args, name, desc)
}
-func printCommandText(offset, args, name, desc string) {
+func printCommandText(args, name, desc string) {
arguments := ""
if len(args) > 0 {
arguments = fmt.Sprintf("[%s]", args)
}
- fmt.Printf("%s%10s %-10s %s\n", offset, name, arguments, desc)
+ fmt.Printf(" %10s %-15s %s\n", name, arguments, desc)
}
func printUsage() {
fmt.Println("lb usage:")
printCommand(clipCommand, "entry", "copy the entry's value into the clipboard")
+ printCommand(envCommand, "", "display environment variable information")
+ printSubCommand(inputs.DefaultsCommand, "", "display the default environment values, exclude current settings")
printCommand(findCommand, "criteria", "perform a simplistic text search over the entry keys")
printCommand(helpCommand, "", "show this usage information")
printCommand(insertCommand, "entry", "insert a new entry into the store")
@@ -87,6 +90,8 @@ func internalCallback(name string) callbackFunction {
return hashText
case clearCommand:
return clearClipboard
+ case envCommand:
+ return inputs.ListEnvironmentVariables
}
return nil
}
diff --git a/contrib/completions.bash b/contrib/completions.bash
@@ -24,7 +24,7 @@ _lb() {
fi
cur=${COMP_WORDS[COMP_CWORD]}
if [ "$COMP_CWORD" -eq 1 ]; then
- opts="version help ls show totp$readwrite find$clip_enabled"
+ opts="version help ls show env totp$readwrite find$clip_enabled"
# shellcheck disable=SC2207
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
else
diff --git a/contrib/doc.sections b/contrib/doc.sections
@@ -15,37 +15,3 @@ via environment variables
[totp]
By default lb tries to use some reasonable defaults to setup/manage oauth token inputs and
displaying of code outputs. Many of these settings can be changed via environment variables
-
-[environment variables]
-Most of lb is managed by using environment variables, the following environment
-variables are REQUIRED for lb to operate:
-
-[required environment variables]
-- LOCKBOX_STORE (unset) directory to the database
-
-- LOCKBOX_KEYMODE (command) how to retrieve the database store password, can be 'plaintext' or 'command'
-
-- LOCKBOX_KEY (unset) the database key (plaintext) or shell command to run (command) to retrieve the database password
-
-[additional environment variables]
-- LOCKBOX_NOCLIP (no) disable clipboard operations, 'yes'/'no'
-
-- LOCKBOX_NOCOLOR (no) disable terminal colors, 'yes/no'
-
-- LOCKBOX_INTERACTIVE (yes) enable interactive mode, 'yes/no'
-
-- LOCKBOX_READONLY (no) operate in readonly mode, 'yes/no'
-
-- LOCKBOX_TOTP (totp) attribute name to store TOTP tokens within the database
-
-- LOCKBOX_TOTP_FORMAT (unset) override the otpauth url used to store totp tokens (e.g. otpauth://totp/%s/rest/of/string), must have ONE format '%s' to insert the totp base code
-
-- LOCKBOX_TOTP_BETWEEN (unset) override when to set totp generated outputs to different colors (e.g. 0:5,30:35), must be a list of one (or more) rules where a semicolon delimits the start and end second (0-60 for each).
-
-- LOCKBOX_CLIP_PASTE (unset) override the detected platform paste command
-
-- LOCKBOX_CLIP_COPY (unset) override the detected platform clip command
-
-- LOCKBOX_CLIP_MAX (unset) override the amount of time before totp clears the clipboard (e.g. 10), must be an integer
-
-- LOCKBOX_PLATFORM (unset) override the detected platform
diff --git a/internal/inputs/env.go b/internal/inputs/env.go
@@ -39,6 +39,21 @@ const (
ClipPasteEnv = clipBaseEnv + "PASTE"
// ClipCopyEnv allows overriding the clipboard copy command
ClipCopyEnv = clipBaseEnv + "COPY"
+ // DefaultsCommand will get the environment values WITHOUT current environment settings
+ DefaultsCommand = "-defaults"
+ isYes = "yes"
+ isNo = "no"
+ defaultTOTPField = "totp"
+)
+
+var (
+ isYesNoArgs = []string{isYes, isNo}
+)
+
+type (
+ environmentOutput struct {
+ showValues bool
+ }
)
// EnvOrDefault will get the environment value OR default if env is not set.
@@ -98,9 +113,9 @@ func isYesNoEnv(defaultValue bool, env string) (bool, error) {
return defaultValue, nil
}
switch value {
- case "no":
+ case isNo:
return false, nil
- case "yes":
+ case isYes:
return true, nil
}
return false, fmt.Errorf("invalid yes/no env value for %s", env)
@@ -128,7 +143,7 @@ func IsInteractive() (bool, error) {
// TOTPToken gets the name of the totp special case tokens
func TOTPToken() string {
- return EnvOrDefault(fieldTOTPEnv, "totp")
+ return EnvOrDefault(fieldTOTPEnv, defaultTOTPField)
}
// FormatTOTP will format a totp otpauth url
@@ -154,3 +169,47 @@ func FormatTOTP(value string) string {
}
return u.String()
}
+
+func (o environmentOutput) printEnvironmentVariable(required bool, name, val, desc string, allowed []string) {
+ value := val
+ if o.showValues {
+ value = os.Getenv(name)
+ }
+ if len(value) == 0 {
+ value = "(unset)"
+ }
+ fmt.Printf("\n%s\n %s\n\n required: %t\n value: %s\n", name, desc, required, value)
+}
+
+// ListEnvironmentVariables will print information about env variables and potential/set values
+func ListEnvironmentVariables(args []string) error {
+ showValues := true
+ switch len(args) {
+ case 0:
+ break
+ case 1:
+ if args[0] == DefaultsCommand {
+ showValues = false
+ } else {
+ return errors.New("unknown argument")
+ }
+ default:
+ return errors.New("too many arguments")
+ }
+ e := environmentOutput{showValues: showValues}
+ e.printEnvironmentVariable(true, StoreEnv, "", "directory to the database file", nil)
+ e.printEnvironmentVariable(true, keyModeEnv, commandKeyMode, "how to retrieve the database store password", []string{commandKeyMode, plainKeyMode})
+ e.printEnvironmentVariable(true, keyEnv, "unset", fmt.Sprintf("the database key (%s) or shell command to run (%s) to retrieve the database password", plainKeyMode, commandKeyMode), nil)
+ e.printEnvironmentVariable(false, noClipEnv, isNo, "disable clipboard operations", isYesNoArgs)
+ e.printEnvironmentVariable(false, noColorEnv, isNo, "disable terminal colors", isYesNoArgs)
+ e.printEnvironmentVariable(false, interactiveEnv, isYes, "enable interactive mode", isYesNoArgs)
+ e.printEnvironmentVariable(false, readOnlyEnv, isNo, "operate in readonly mode", isYesNoArgs)
+ e.printEnvironmentVariable(false, fieldTOTPEnv, defaultTOTPField, "attribute name to store TOTP tokens within the database", nil)
+ e.printEnvironmentVariable(false, formatTOTPEnv, "", "override the otpauth url used to store totp tokens (e.g. otpauth://totp/%s/rest/of/string), must have ONE format '%s' to insert the totp base code", nil)
+ e.printEnvironmentVariable(false, ColorBetweenEnv, "", "override when to set totp generated outputs to different colors (e.g. 0:5,30:35), must be a list of one (or more) rules where a semicolon delimits the start and end second (0-60 for each)", nil)
+ e.printEnvironmentVariable(false, ClipPasteEnv, "", "override the detected platform paste command", nil)
+ e.printEnvironmentVariable(false, ClipPasteEnv, "", "override the detected platform copy command", nil)
+ e.printEnvironmentVariable(false, ClipMaxEnv, "", "override the amount of time before totp clears the clipboard (e.g. 10), must be an integer", nil)
+ e.printEnvironmentVariable(false, PlatformEnv, "", "override the detected platform", nil)
+ return nil
+}