Add command to install shell completion #309

Merged
6543 merged 10 commits from noerw/tea:shell-completion into master 2020-12-16 16:02:00 +00:00
4 changed files with 118 additions and 0 deletions
Showing only changes of commit a958f1238c - Show all commits

@ -29,6 +29,9 @@ brew install tea
Distribution packages exist for: **alpinelinux ([tea](https://pkgs.alpinelinux.org/packages?name=tea&branch=edge))** and **archlinux ([gitea-tea](https://aur.archlinux.org/packages/gitea-tea))**
Shell completion can be added via `tea meta autocomplete --install`.
## Usage
First of all, you have to create a token on your `personal settings -> application` page of your gitea instance.
@ -54,6 +57,7 @@ open Open something of the repository on web browser
notifications Show notifications
milestones List and create milestones
organizations List, create, delete organizations
meta Operations on tea itself
help, h Shows a list of commands or help for one command
```

20
cmd/meta.go Normal file

@ -0,0 +1,20 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package cmd
import (
"code.gitea.io/tea/cmd/meta"
"github.com/urfave/cli/v2"
)
// CmdMeta represents a sub command
var CmdMeta = cli.Command{
Name: "meta",
Usage: "Operations on tea itself",
Description: `Operations on tea itself`,
Subcommands: []*cli.Command{
&meta.CmdAutocomplete,
},
}

93
cmd/meta/autocomplete.go Normal file

@ -0,0 +1,93 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package meta
import (
"fmt"
"io"
"net/http"
"os"
"os/exec"
"github.com/adrg/xdg"
"github.com/urfave/cli/v2"
)
// CmdAutocomplete manages autocompletion
var CmdAutocomplete = cli.Command{
Name: "autocomplete",
Usage: "Install shell completetion for tea",
Description: "Install shell completetion for tea",
ArgsUsage: "<shell type> (bash, zsh)",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "install",
Usage: "Persist in shell config instead of printing commands",
},
},
Action: runAutocompleteAdd,
}
func runAutocompleteAdd(ctx *cli.Context) error {
var file, cmds string
shell := ctx.Args().First()
switch shell {
case "zsh":
file = "contrib/autocomplete.zsh"
cmds = "echo 'PROG=tea _CLI_ZSH_AUTOCOMPLETE_HACK=1 source %s' >> ~/.zshrc && source ~/.zshrc"
case "bash":
file = "contrib/autocomplete.sh"
cmds = "echo 'PROG=tea source %s' >> ~/.bashrc && source ~/.bashrc"
default:
return fmt.Errorf("Must specify valid shell type")
}
destPath, err := xdg.ConfigFile("tea/" + file)
if err != nil {
return err
}
cmds = fmt.Sprintf(cmds, destPath)
if err := saveAutoCompleteFile(file, destPath); err != nil {
return err
}
if ctx.Bool("install") {
fmt.Println("Installing in your shellrc")
installer := exec.Command(shell, "-c", cmds)
out, err := installer.CombinedOutput()
if err != nil {
return fmt.Errorf("Couldn't run the commands: %s %s", err, out)
}
} else {
fmt.Println("\n# Run the following commands to install autocompletion (or use --install)")
fmt.Println(cmds)
}
return nil
}
func saveAutoCompleteFile(file, destPath string) error {
url := fmt.Sprintf("https://gitea.com/gitea/tea/raw/branch/master/%s", file)
fmt.Println("Fetching " + url)
res, err := http.Get(url)
if err != nil {
return err
}
defer res.Body.Close()
writer, err := os.Create(destPath)
if err != nil {
return err
}
defer writer.Close()
_, err = io.Copy(writer, res.Body)
return err
}

@ -40,6 +40,7 @@ func main() {
&cmd.CmdNotifications,
&cmd.CmdMilestones,
&cmd.CmdOrgs,
&cmd.CmdMeta,
}
app.EnableBashCompletion = true
err := app.Run(os.Args)