From 9a90449019493f707e1d2ef63bc979d212bf3e6c Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Sun, 25 Jul 2021 22:14:45 -0700 Subject: [PATCH] Improve usage and set password arg --- Makefile | 2 +- main.go | 13 +++++++++++-- scripts/yk.fish | 11 +++++------ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index dbc891e..3c9d8d4 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ TARGET_ALIAS = $(NAME)-linux-amd64 $(NAME)-linux-arm $(NAME)-linux-arm64 $(NAME) TARGETS = $(addprefix dist/,$(TARGET_ALIAS)) .PHONY: default -default: yk +default: build/yk .PHONY: all all: $(TARGETS) diff --git a/main.go b/main.go index 528e481..2641bbd 100644 --- a/main.go +++ b/main.go @@ -64,9 +64,18 @@ func getPasskey(s *ykoath.Select) ([]byte, error) { return s.DeriveKey(password), nil } +func usage() { + fmt.Fprintf(flag.CommandLine.Output(), "Usage:\n %s [target site]\n\n", os.Args[0]) + fmt.Fprintf(flag.CommandLine.Output(), "Prints TOTP code for provided target site. If no site is provided, %s will list all sites.\n\n", os.Args[0]) + fmt.Fprint(flag.CommandLine.Output(), "If a touch is required for your code, the command will not return until the your key is touched.\n\n") + flag.PrintDefaults() +} + func main() { + flag.Usage = usage flag.BoolVar(&slog.DebugLevel, "debug", false, "enable debug logging") showVersion := flag.Bool("version", false, "print version and exit") + shouldSetPassword := flag.Bool("set-password", false, "prompt for key password and store in system keychain") flag.Parse() if *showVersion { @@ -90,7 +99,7 @@ func main() { slog.FatalOnErr(err, "failed to select oath") // Check to see if we are trying to set a password - if flag.Arg(0) == "set-password" { + if *shouldSetPassword { err = setPassword(s) slog.FatalOnErr(err, "failed to save password") @@ -112,7 +121,7 @@ func main() { slog.Debug("no challenge required") } - if flag.Arg(0) == "list" { + if flag.NArg() == 0 || flag.Arg(0) == "list" { // List names only names, err := oath.List() slog.FatalOnErr(err, "failed to list names") diff --git a/scripts/yk.fish b/scripts/yk.fish index 1bcb610..1b58923 100644 --- a/scripts/yk.fish +++ b/scripts/yk.fish @@ -1,9 +1,8 @@ -# Disable file completions -complete -c yk -f -# Add completion for setting password -complete -c yk -a set-password -d "Set authentication password" -# Add completion of credentials -complete -c yk -a "(yk list)" -d "Credential to get TOTP for" +complete --command yk --no-files +complete --command yk --arguments "(yk list)" --description "Credential to get TOTP for" +complete --command yk --old-option "debug" --description "enable debug logging" +complete --command yk --old-option "version" --description "print version and exit" +complete --command yk --old-option "set-password" --description "prompt for key password and store in system keychain" function yk-copy --description "Select a credential using fzf and copy it to clipboard" set result (yk (yk list | fzf))