Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/cli-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,10 @@ Update fetch binary in place. Use with `--dry-run` to check for updates without

### `--complete SHELL`

Output shell completion scripts. Values: `fish`, `zsh`.
Output shell completion scripts. Values: `bash`, `fish`, `zsh`.

```sh
echo 'eval "$(fetch --complete bash)"' >> ~/.bashrc
fetch --complete zsh > ~/.zshrc.d/_fetch
fetch --complete fish > ~/.config/fish/completions/fetch.fish
```
Expand Down
3 changes: 3 additions & 0 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ auto-update = true
Generate shell completion scripts:

```sh
# Bash
echo 'eval "$(fetch --complete bash)"' >> ~/.bashrc

# Zsh
fetch --complete zsh > ~/.zshrc.d/fetch-completion.zsh

Expand Down
7 changes: 7 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ info "fetch successfully installed to '${DIM}${INSTALL_DIR}/fetch${RESET}'"

# Optionally install completions.
case "$SHELL" in
*/bash)
COMPLETION_CMD='eval "$(fetch --complete=bash)"'
if ! grep -qF "$COMPLETION_CMD" "$HOME/.bashrc" 2>/dev/null; then
printf "\n# fetch completions\n${COMPLETION_CMD}\n" >> "$HOME/.bashrc"
info "completions appended to '${DIM}${HOME}/.bashrc${RESET}'"
fi
;;
*/fish)
mkdir -p "$HOME/.config/fish/completions"
echo "fetch --complete=fish | source" > "$HOME/.config/fish/completions/fetch.fish"
Expand Down
55 changes: 28 additions & 27 deletions internal/cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,20 @@ func (a *App) CLI() *CLI {
return a.Cfg.ParseCert(value)
},
},
{
Short: "",
Long: "clobber",
Args: "",
Description: "Overwrite existing output file",
Default: "",
IsSet: func() bool {
return a.Clobber
},
Fn: func(value string) error {
a.Clobber = true
return nil
},
},
{
Short: "",
Long: "color",
Expand Down Expand Up @@ -268,27 +282,14 @@ func (a *App) CLI() *CLI {
return a.Cfg.ParseColor(value)
},
},
{
Short: "",
Long: "clobber",
Args: "",
Description: "Overwrite existing output file",
Default: "",
IsSet: func() bool {
return a.Clobber
},
Fn: func(value string) error {
a.Clobber = true
return nil
},
},
{
Short: "",
Long: "complete",
Args: "SHELL",
Description: "Output shell completion",
Default: "",
Values: []core.KeyVal[string]{
{Key: "bash"},
{Key: "fish"},
{Key: "zsh"},
},
Expand Down Expand Up @@ -847,6 +848,19 @@ func (a *App) CLI() *CLI {
return nil
},
},
{
Short: "S",
Long: "session",
Args: "NAME",
Description: "Use a named session for cookies",
Default: "",
IsSet: func() bool {
return a.Cfg.Session != nil
},
Fn: func(value string) error {
return a.Cfg.ParseSession(value)
},
},
{
Short: "s",
Long: "silent",
Expand All @@ -862,19 +876,6 @@ func (a *App) CLI() *CLI {
return nil
},
},
{
Short: "S",
Long: "session",
Args: "NAME",
Description: "Use a named session for cookies",
Default: "",
IsSet: func() bool {
return a.Cfg.Session != nil
},
Fn: func(value string) error {
return a.Cfg.ParseSession(value)
},
},
{
Short: "t",
Long: "timeout",
Expand Down
15 changes: 15 additions & 0 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ import (
"github.com/ryanfowler/fetch/internal/core"
)

func TestFlagsAlphabeticalOrder(t *testing.T) {
app, err := Parse(nil)
if err != nil {
t.Fatalf("unable to parse cli: %s", err.Error())
}
cli := app.CLI()
for i := 1; i < len(cli.Flags); i++ {
prev := cli.Flags[i-1].Long
curr := cli.Flags[i].Long
if curr < prev {
t.Errorf("flags out of alphabetical order: %q should come before %q", curr, prev)
}
}
}

func TestCLI(t *testing.T) {
app, err := Parse(nil)
if err != nil {
Expand Down
70 changes: 70 additions & 0 deletions internal/complete/complete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,76 @@ import (
"github.com/ryanfowler/fetch/internal/cli"
)

func TestCompleteBash(t *testing.T) {
var app cli.App
flags := app.CLI().Flags
_, long := getFlagMaps(flags)

tests := []struct {
name string
shell Shell
args []string
exp string
}{
{
name: "should return nothing when no args",
shell: Bash{},
args: nil,
exp: "",
},
{
name: "should return nothing when only arg is command",
shell: Bash{},
args: []string{"fetch"},
exp: "",
},
{
name: "should complete color flag",
shell: Bash{},
args: []string{"fetch", "--col"},
exp: "--color \n",
},
{
name: "should complete color value",
shell: Bash{},
args: []string{"fetch", "--color", ""},
exp: func() string {
var sb strings.Builder
for _, kv := range long["color"].Values {
sb.WriteString(kv.Key)
sb.WriteString(" \n")
}
return sb.String()
}(),
},
{
name: "should complete color value with prefix",
shell: Bash{},
args: []string{"fetch", "--color", "o"},
exp: func() string {
var sb strings.Builder
for _, kv := range long["color"].Values {
if !strings.HasPrefix(kv.Key, "o") {
continue
}
sb.WriteString(kv.Key)
sb.WriteString(" \n")
}
return sb.String()
}(),
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
res := Complete(test.shell, test.args)
if res != test.exp {
t.Fatalf("Unexpected result:\n%s", res)
}
})
}
}

func TestCompleteFish(t *testing.T) {
var app cli.App
flags := app.CLI().Flags
Expand Down
32 changes: 32 additions & 0 deletions internal/complete/shells.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type Shell interface {
// nil is returned.
func GetShell(name string) Shell {
switch name {
case "bash":
return Bash{}
case "fish":
return Fish{}
case "zsh":
Expand All @@ -26,6 +28,36 @@ func GetShell(name string) Shell {
}
}

type Bash struct{}

func (b Bash) Name() string {
return "bash"
}

func (b Bash) Register() string {
return `_fetch_complete() {
local cur prev_tokens
cur="${COMP_WORDS[COMP_CWORD]}"
prev_tokens=("${COMP_WORDS[@]:0:COMP_CWORD}")
local IFS=$'\n'
COMPREPLY=($(fetch --complete=bash -- "${prev_tokens[@]}" "$cur"))
IFS=$' \t\n'
}
complete -o nosort -o nospace -F _fetch_complete fetch`
}

func (b Bash) Complete(vals []core.KeyVal[string]) string {
var sb strings.Builder
for _, kv := range vals {
sb.WriteString(kv.Key)
if !strings.HasSuffix(kv.Key, "/") && !strings.HasSuffix(kv.Key, "=") {
sb.WriteByte(' ')
}
sb.WriteByte('\n')
}
return sb.String()
}

type Fish struct{}

func (f Fish) Name() string {
Expand Down