Improve usage and set password arg

This commit is contained in:
IamTheFij 2021-07-25 22:14:45 -07:00
parent c688f8ce00
commit 9a90449019
3 changed files with 17 additions and 9 deletions

View File

@ -6,7 +6,7 @@ TARGET_ALIAS = $(NAME)-linux-amd64 $(NAME)-linux-arm $(NAME)-linux-arm64 $(NAME)
TARGETS = $(addprefix dist/,$(TARGET_ALIAS)) TARGETS = $(addprefix dist/,$(TARGET_ALIAS))
.PHONY: default .PHONY: default
default: yk default: build/yk
.PHONY: all .PHONY: all
all: $(TARGETS) all: $(TARGETS)

13
main.go
View File

@ -64,9 +64,18 @@ func getPasskey(s *ykoath.Select) ([]byte, error) {
return s.DeriveKey(password), nil 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() { func main() {
flag.Usage = usage
flag.BoolVar(&slog.DebugLevel, "debug", false, "enable debug logging") flag.BoolVar(&slog.DebugLevel, "debug", false, "enable debug logging")
showVersion := flag.Bool("version", false, "print version and exit") 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() flag.Parse()
if *showVersion { if *showVersion {
@ -90,7 +99,7 @@ func main() {
slog.FatalOnErr(err, "failed to select oath") slog.FatalOnErr(err, "failed to select oath")
// Check to see if we are trying to set a password // Check to see if we are trying to set a password
if flag.Arg(0) == "set-password" { if *shouldSetPassword {
err = setPassword(s) err = setPassword(s)
slog.FatalOnErr(err, "failed to save password") slog.FatalOnErr(err, "failed to save password")
@ -112,7 +121,7 @@ func main() {
slog.Debug("no challenge required") slog.Debug("no challenge required")
} }
if flag.Arg(0) == "list" { if flag.NArg() == 0 || flag.Arg(0) == "list" {
// List names only // List names only
names, err := oath.List() names, err := oath.List()
slog.FatalOnErr(err, "failed to list names") slog.FatalOnErr(err, "failed to list names")

View File

@ -1,9 +1,8 @@
# Disable file completions complete --command yk --no-files
complete -c yk -f complete --command yk --arguments "(yk list)" --description "Credential to get TOTP for"
# Add completion for setting password complete --command yk --old-option "debug" --description "enable debug logging"
complete -c yk -a set-password -d "Set authentication password" complete --command yk --old-option "version" --description "print version and exit"
# Add completion of credentials complete --command yk --old-option "set-password" --description "prompt for key password and store in system keychain"
complete -c yk -a "(yk list)" -d "Credential to get TOTP for"
function yk-copy --description "Select a credential using fzf and copy it to clipboard" function yk-copy --description "Select a credential using fzf and copy it to clipboard"
set result (yk (yk list | fzf)) set result (yk (yk list | fzf))