commit d0fc87bdd745a33a434989e940a0b0019c8ff56e
parent 2720f679a1a94377247c511b55ae6b79a803ba73
Author: Sean Enck <sean@ttypty.com>
Date: Fri, 19 Apr 2024 21:29:02 -0400
all help command is the same for shells, make that a help command
Diffstat:
9 files changed, 39 insertions(+), 57 deletions(-)
diff --git a/internal/app/completions.go b/internal/app/completions.go
@@ -28,6 +28,7 @@ type (
JSONCommand string
HelpCommand string
HelpAdvancedCommand string
+ HelpShellCommand string
Profiles []Profile
DefaultProfile Profile
Shell string
@@ -154,6 +155,7 @@ when %s=<unknown>
JSONCommand: JSONCommand,
HelpCommand: HelpCommand,
HelpAdvancedCommand: HelpAdvancedCommand,
+ HelpShellCommand: HelpShellCommand,
TOTPCommand: TOTPCommand,
MoveCommand: MoveCommand,
DoList: fmt.Sprintf("%s %s", exe, ListCommand),
diff --git a/internal/app/core.go b/internal/app/core.go
@@ -19,7 +19,6 @@ import (
)
const (
- completionHelpDesc = "display shell completion help"
// TOTPCommand is the parent of totp and by defaults generates a rotating token
TOTPCommand = "totp"
// ConvCommand handles text conversion of the data store
@@ -58,8 +57,8 @@ const (
TOTPOnceCommand = "once"
// BashCommand is the command to generate bash completions
BashCommand = "bash"
- // CompletionHelpCommand outputs completion help information
- CompletionHelpCommand = "help"
+ // HelpShellCommand is the help output about shell variables
+ HelpShellCommand = "shell"
// ReKeyCommand will rekey the underlying database
ReKeyCommand = "rekey"
// MultiLineCommand handles multi-line inserts (when not piped)
@@ -101,7 +100,8 @@ type (
MoveCommand string
RemoveCommand string
ReKeyCommand string
- ShellHelpCommand string
+ HelpCommand string
+ HelpShellCommand string
ReKey struct {
Store string
KeyFile string
@@ -185,11 +185,11 @@ func commandText(args, name, desc string) string {
func Usage(verbose bool, exe string) ([]string, error) {
var results []string
results = append(results, command(BashCommand, "", "generate user environment bash completion"))
- results = append(results, subCommand(BashCommand, CompletionHelpCommand, "", completionHelpDesc))
results = append(results, command(ClipCommand, "entry", "copy the entry's value into the clipboard"))
results = append(results, command(EnvCommand, "", "display environment variable information"))
results = append(results, command(HelpCommand, "", "show this usage information"))
results = append(results, subCommand(HelpCommand, HelpAdvancedCommand, "", "display verbose help information"))
+ results = append(results, subCommand(HelpCommand, HelpShellCommand, "", "display shell variable help information"))
results = append(results, command(InsertCommand, "entry", "insert a new entry into the store"))
results = append(results, command(JSONCommand, "filter", "display detailed information"))
results = append(results, command(ListCommand, "", "list entries"))
@@ -207,9 +207,7 @@ func Usage(verbose bool, exe string) ([]string, error) {
results = append(results, subCommand(TOTPCommand, TOTPShowCommand, "entry", "show the totp entry"))
results = append(results, command(VersionCommand, "", "display version information"))
results = append(results, command(ZshCommand, "", "generate user environment zsh completion"))
- results = append(results, subCommand(ZshCommand, CompletionHelpCommand, "", completionHelpDesc))
results = append(results, command(FishCommand, "", "generate user environment fish completion"))
- results = append(results, subCommand(FishCommand, CompletionHelpCommand, "", completionHelpDesc))
sort.Strings(results)
usage := []string{fmt.Sprintf("%s usage:", exe)}
if verbose {
@@ -219,7 +217,8 @@ func Usage(verbose bool, exe string) ([]string, error) {
MoveCommand: MoveCommand,
RemoveCommand: RemoveCommand,
ReKeyCommand: ReKeyCommand,
- ShellHelpCommand: CompletionHelpCommand,
+ HelpShellCommand: HelpShellCommand,
+ HelpCommand: HelpCommand,
}
document.ReKey.Store = setDocFlag(config.ReKeyStoreFlag)
document.ReKey.Key = setDocFlag(config.ReKeyKeyFlag)
diff --git a/internal/app/core_test.go b/internal/app/core_test.go
@@ -9,11 +9,11 @@ import (
func TestUsage(t *testing.T) {
u, _ := app.Usage(false, "lb")
- if len(u) != 27 {
+ if len(u) != 25 {
t.Errorf("invalid usage, out of date? %d", len(u))
}
u, _ = app.Usage(true, "lb")
- if len(u) != 110 {
+ if len(u) != 108 {
t.Errorf("invalid verbose usage, out of date? %d", len(u))
}
for _, usage := range u {
diff --git a/internal/app/doc/bash.sh b/internal/app/doc/bash.sh
@@ -17,7 +17,7 @@
if [ "$COMP_CWORD" -eq 2 ]; then
case ${COMP_WORDS[1]} in
"{{ $.HelpCommand }}")
- opts="{{ $.HelpAdvancedCommand }}"
+ opts="{{ $.HelpAdvancedCommand }} {{ $.HelpShellCommand }}"
;;
{{- if not $profile.ReadOnly }}
{{- if $profile.CanList }}
diff --git a/internal/app/doc/completions.txt b/internal/app/doc/completions.txt
@@ -1,5 +1,5 @@
Completions are available for certain shells and, by default, assume all
features of `{{ $.Executable }}` are enabled and available. When changing certain environment
flags it may be useful to change the completion profile to more closely match
-the restricted command options, run `{{ $.Executable }} <shell> {{ $.ShellHelpCommand }}` for information
+the restricted command options, run `{{ $.Executable }} {{ $.HelpCommand }} {{ $.HelpShellCommand }}` for information
on how best to alter completion outputs.
diff --git a/internal/app/doc/fish.sh b/internal/app/doc/fish.sh
@@ -5,7 +5,7 @@ complete -c {{ $.Executable }} -f
function {{ $profile.Name }}
set -l commands {{ range $idx, $value := $profile.Options }}{{ if gt $idx 0}} {{ end }}{{ $value }}{{ end }}
complete -c {{ $.Executable }} -n "not __fish_seen_subcommand_from $commands" -a "$commands"
- complete -c {{ $.Executable }} -n "__fish_seen_subcommand_from {{ $.HelpCommand }}; and test (count (commandline -opc)) -lt 3" -a "{{ $.HelpAdvancedCommand }}"
+ complete -c {{ $.Executable }} -n "__fish_seen_subcommand_from {{ $.HelpCommand }}; and test (count (commandline -opc)) -lt 3" -a "{{ $.HelpAdvancedCommand }} {{ $.HelpShellCommand }}"
{{- if not $profile.ReadOnly }}
{{- if $profile.CanList }}
complete -c {{ $.Executable }} -n "__fish_seen_subcommand_from {{ $.InsertCommand }} {{ $.MultiLineCommand }} {{ $.RemoveCommand }}; and test (count (commandline -opc)) -lt 3" -a "({{ $.DoList }})"
diff --git a/internal/app/doc/zsh.sh b/internal/app/doc/zsh.sh
@@ -21,6 +21,7 @@
case $words[2] in
"{{ $.HelpCommand }}")
if [ "$len" -eq 3 ]; then
+ compadd "$@" "{{ $.HelpShellCommand }}"
compadd "$@" "{{ $.HelpAdvancedCommand }}"
fi
;;
diff --git a/internal/app/info.go b/internal/app/info.go
@@ -40,17 +40,20 @@ func info(command string, args []string) ([]string, error) {
return nil, errors.New("invalid help command")
}
isAdvanced := false
+ exe, err := exeName()
+ if err != nil {
+ return nil, err
+ }
if len(args) == 1 {
- if args[0] == HelpAdvancedCommand {
+ switch args[0] {
+ case HelpAdvancedCommand:
isAdvanced = true
- } else {
+ case HelpShellCommand:
+ return GenerateCompletions("", true, exe)
+ default:
return nil, errors.New("invalid help option")
}
}
- exe, err := exeName()
- if err != nil {
- return nil, err
- }
results, err := Usage(isAdvanced, exe)
if err != nil {
return nil, err
@@ -62,22 +65,14 @@ func info(command string, args []string) ([]string, error) {
}
return config.Environ(), nil
case BashCommand, ZshCommand, FishCommand:
- if len(args) > 1 {
+ if len(args) != 0 {
return nil, fmt.Errorf("invalid %s command", command)
}
- isHelp := false
- if len(args) == 1 {
- if args[0] == CompletionHelpCommand {
- isHelp = true
- } else {
- return nil, fmt.Errorf("invalid %s subcommand", command)
- }
- }
exe, err := exeName()
if err != nil {
return nil, err
}
- return GenerateCompletions(command, isHelp, exe)
+ return GenerateCompletions(command, false, exe)
}
return nil, nil
}
diff --git a/internal/app/info_test.go b/internal/app/info_test.go
@@ -41,6 +41,15 @@ func TestHelpInfo(t *testing.T) {
if _, err = app.Info(&buf, "help", []string{"verbose", "A"}); err.Error() != "invalid help command" {
t.Errorf("invalid error: %v", err)
}
+ old = buf.String()
+ buf = bytes.Buffer{}
+ ok, err = app.Info(&buf, "help", []string{"shell"})
+ if !ok || err != nil {
+ t.Errorf("invalid error: %v", err)
+ }
+ if buf.String() == "" || old == buf.String() {
+ t.Error("nothing written")
+ }
}
func TestBashInfo(t *testing.T) {
@@ -53,21 +62,13 @@ func TestBashInfo(t *testing.T) {
if buf.String() == "" {
t.Error("nothing written")
}
- buf = bytes.Buffer{}
- ok, err = app.Info(&buf, "bash", []string{"help"})
- if !ok || err != nil {
- t.Errorf("invalid error: %v", err)
- }
- if buf.String() == "" {
- t.Error("nothing written")
- }
- if _, err = app.Info(&buf, "bash", []string{"defaults"}); err.Error() != "invalid bash subcommand" {
+ if _, err = app.Info(&buf, "bash", []string{"defaults"}); err.Error() != "invalid bash command" {
t.Errorf("invalid error: %v", err)
}
if _, err = app.Info(&buf, "bash", []string{"test", "default"}); err.Error() != "invalid bash command" {
t.Errorf("invalid error: %v", err)
}
- if _, err = app.Info(&buf, "bash", []string{"short"}); err.Error() != "invalid bash subcommand" {
+ if _, err = app.Info(&buf, "bash", []string{"short"}); err.Error() != "invalid bash command" {
t.Errorf("invalid error: %v", err)
}
}
@@ -108,15 +109,7 @@ func TestZshInfo(t *testing.T) {
if buf.String() == "" {
t.Error("nothing written")
}
- buf = bytes.Buffer{}
- ok, err = app.Info(&buf, "zsh", []string{"help"})
- if !ok || err != nil {
- t.Errorf("invalid error: %v", err)
- }
- if buf.String() == "" {
- t.Error("nothing written")
- }
- if _, err = app.Info(&buf, "zsh", []string{"defaults"}); err.Error() != "invalid zsh subcommand" {
+ if _, err = app.Info(&buf, "zsh", []string{"defaults"}); err.Error() != "invalid zsh command" {
t.Errorf("invalid error: %v", err)
}
if _, err = app.Info(&buf, "zsh", []string{"test", "default"}); err.Error() != "invalid zsh command" {
@@ -134,15 +127,7 @@ func TestFishInfo(t *testing.T) {
if buf.String() == "" {
t.Error("nothing written")
}
- buf = bytes.Buffer{}
- ok, err = app.Info(&buf, "fish", []string{"help"})
- if !ok || err != nil {
- t.Errorf("invalid error: %v", err)
- }
- if buf.String() == "" {
- t.Error("nothing written")
- }
- if _, err = app.Info(&buf, "fish", []string{"defaults"}); err.Error() != "invalid fish subcommand" {
+ if _, err = app.Info(&buf, "fish", []string{"help"}); err.Error() != "invalid fish command" {
t.Errorf("invalid error: %v", err)
}
if _, err = app.Info(&buf, "fish", []string{"test", "default"}); err.Error() != "invalid fish command" {