Add bash and zsh completions
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Elara 2022-12-02 12:29:55 -08:00
parent 8f9bdf69ad
commit 23fd711be2
3 changed files with 74 additions and 4 deletions

37
main.go
View File

@ -20,15 +20,19 @@ package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/genjidb/genji/document"
"github.com/genjidb/genji/types"
"github.com/urfave/cli/v2"
"go.arsenm.dev/logger"
"go.arsenm.dev/logger/log"
"go.arsenm.dev/lure/internal/config"
"go.arsenm.dev/lure/internal/db"
)
//go:generate scripts/gen-version.sh
@ -54,10 +58,11 @@ func main() {
Usage: "Linux User REpository",
Commands: []*cli.Command{
{
Name: "install",
Usage: "Install a new package",
Aliases: []string{"in"},
Action: installCmd,
Name: "install",
Usage: "Install a new package",
Aliases: []string{"in"},
Action: installCmd,
BashComplete: completionInstall,
},
{
Name: "remove",
@ -150,6 +155,7 @@ func main() {
After: func(ctx *cli.Context) error {
return gdb.Close()
},
EnableBashCompletion: true,
}
err := app.RunContext(ctx, os.Args)
@ -162,3 +168,26 @@ func displayVersion(c *cli.Context) error {
print(config.Version)
return nil
}
func completionInstall(c *cli.Context) {
result, err := db.GetPkgs(gdb, "true")
if err != nil {
log.Fatal("Error getting packages").Err(err).Send()
}
defer result.Close()
err = result.Iterate(func(d types.Document) error {
var pkg db.Package
err = document.StructScan(d, &pkg)
if err != nil {
return err
}
fmt.Println(pkg.Name)
return nil
})
if err != nil {
log.Fatal("Error iterating over packages").Err(err).Send()
}
}

21
scripts/completion/bash Normal file
View File

@ -0,0 +1,21 @@
#! /bin/bash
: ${PROG:=$(basename ${BASH_SOURCE})}
_cli_bash_autocomplete() {
if [[ "${COMP_WORDS[0]}" != "source" ]]; then
local cur opts base
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
if [[ "$cur" == "-"* ]]; then
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} ${cur} --generate-bash-completion )
else
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion )
fi
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi
}
complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete $PROG
unset PROG

20
scripts/completion/zsh Normal file
View File

@ -0,0 +1,20 @@
#compdef lure
_cli_zsh_autocomplete() {
local -a opts
local cur
cur=${words[-1]}
if [[ "$cur" == "-"* ]]; then
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
else
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-bash-completion)}")
fi
if [[ "${opts[1]}" != "" ]]; then
_describe 'values' opts
else
_files
fi
}
compdef _cli_zsh_autocomplete lure