commit ef19656b1c9deb54c9e6e048a3937c2b52c0a69e
parent 26ed57668177d4cee0c04a1f3a9cd39a035cacec
Author: Sean Enck <sean@ttypty.com>
Date: Mon, 27 Mar 2023 19:10:02 -0400
common pathways for sub commands
Diffstat:
2 files changed, 27 insertions(+), 30 deletions(-)
diff --git a/internal/cli/completions.bash b/internal/cli/completions.bash
@@ -1,7 +1,7 @@
# {{ $.Executable }} completion
_{{ $.Executable }}() {
- local cur opts needs
+ local cur opts
cur=${COMP_WORDS[COMP_CWORD]}
if [ "$COMP_CWORD" -eq 1 ]; then
{{range $idx, $value := $.Options }}
@@ -13,7 +13,10 @@ _{{ $.Executable }}() {
case ${COMP_WORDS[1]} in
{{ if not $.ReadOnly }}
"{{ $.InsertCommand }}")
- opts="{{ $.InsertMultiCommand }}{{ if $.CanTOTP }} {{ $.InsertTOTPCommand }}{{end}} $({{ $.DoList }})"
+{{ range $key, $value := .InsertSubCommands }}
+ opts="$opts {{ $value }}"
+{{end}}
+ opts="$opts $({{ $.DoList }})"
;;
"{{ $.HelpCommand }}")
opts="{{ $.HelpAdvancedCommand }}"
@@ -24,10 +27,11 @@ _{{ $.Executable }}() {
{{end}}
{{ if $.CanTOTP }}
"{{ $.TOTPCommand }}")
- opts="{{ $.TOTPShortCommand }} {{ $.TOTPOnceCommand }} {{ $.TOTPListCommand }} "$({{ $.DoTOTPList }})
-{{ if $.CanClip }}
- opts="$opts {{ $.TOTPClipCommand }}"
+ opts="{{ $.TOTPListCommand }} "
+{{ range $key, $value := .TOTPSubCommands }}
+ opts="$opts {{ $value }}"
{{end}}
+ opts="$opts "$({{ $.DoTOTPList }})
;;
{{end}}
"{{ $.ShowCommand }}" | "{{ $.StatsCommand }}" {{ if not $.ReadOnly }}| "{{ $.RemoveCommand }}" {{end}} {{ if $.CanClip }} | "{{ $.ClipCommand }}" {{end}})
@@ -39,9 +43,13 @@ _{{ $.Executable }}() {
case "${COMP_WORDS[1]}" in
{{ if not $.ReadOnly }}
"{{ $.InsertCommand }}")
- if [ "${COMP_WORDS[2]}" == "{{ $.InsertMultiCommand }}" ] {{ if $.CanTOTP }}|| [ "${COMP_WORDS[2]}" == "{{ $.InsertTOTPCommand }}" ] {{end}}; then
- opts=$({{ $.DoList }})
- fi
+ case "${COMP_WORDS[2]}" in
+{{ range $key, $value := .InsertSubCommands }}
+ "{{ $value }}")
+ opts=$({{ $.DoList }})
+ ;;
+{{end}}
+ esac
;;
"{{ $.MoveCommand }}")
opts=$({{ $.DoList }})
@@ -49,19 +57,13 @@ _{{ $.Executable }}() {
{{end}}
{{ if $.CanTOTP }}
"{{ $.TOTPCommand }}")
- needs=0
- if [ "${COMP_WORDS[2]}" == "{{ $.TOTPOnceCommand }}" ] || [ "${COMP_WORDS[2]}" == "{{ $.TOTPShortCommand }}" ]; then
- needs=1
-{{ if $.CanClip }}
- else
- if [ "${COMP_WORDS[2]}" == "{{ $.TOTPClipCommand }}" ]; then
- needs=1
- fi
-{{end}}
- fi
- if [ $needs -eq 1 ]; then
+ case "${COMP_WORDS[2]}" in
+{{ range $key, $value := .TOTPSubCommands }}
+ "{{ $value }}")
opts=$({{ $.DoTOTPList }})
- fi
+ ;;
+{{end}}
+ esac
;;
{{end}}
esac
diff --git a/internal/cli/core.go b/internal/cli/core.go
@@ -83,12 +83,9 @@ type (
CanTOTP bool
ReadOnly bool
InsertCommand string
- TOTPShortCommand string
- TOTPOnceCommand string
- TOTPClipCommand string
+ InsertSubCommands []string
+ TOTPSubCommands []string
TOTPListCommand string
- InsertMultiCommand string
- InsertTOTPCommand string
RemoveCommand string
ClipCommand string
ShowCommand string
@@ -137,17 +134,14 @@ func BashCompletions(defaults bool) ([]string, error) {
Executable: name,
InsertCommand: InsertCommand,
RemoveCommand: RemoveCommand,
- TOTPShortCommand: TOTPShortCommand,
- TOTPClipCommand: TOTPClipCommand,
- TOTPOnceCommand: TOTPOnceCommand,
+ TOTPSubCommands: []string{TOTPShortCommand, TOTPOnceCommand},
TOTPListCommand: TOTPListCommand,
ClipCommand: ClipCommand,
ShowCommand: ShowCommand,
StatsCommand: StatsCommand,
- InsertMultiCommand: InsertMultiCommand,
+ InsertSubCommands: []string{InsertMultiCommand, InsertTOTPCommand},
HelpCommand: HelpCommand,
HelpAdvancedCommand: HelpAdvancedCommand,
- InsertTOTPCommand: InsertTOTPCommand,
TOTPCommand: TOTPCommand,
MoveCommand: MoveCommand,
DoList: fmt.Sprintf("%s %s", name, ListCommand),
@@ -183,6 +177,7 @@ func BashCompletions(defaults bool) ([]string, error) {
options := []string{EnvCommand, FindCommand, HelpCommand, ListCommand, ShowCommand, VersionCommand, StatsCommand}
if c.CanClip {
options = append(options, ClipCommand)
+ c.TOTPSubCommands = append(c.TOTPSubCommands, TOTPClipCommand)
}
if !c.ReadOnly {
options = append(options, MoveCommand, RemoveCommand, InsertCommand)