mirror of
https://gitea.com/gitea/tea.git
synced 2025-09-01 09:28:30 +02:00
remove autocomplete command (#782)
Add a note in readme about adding shell completions Closes: https://gitea.com/gitea/tea/issues/781 Co-authored-by: techknowlogick <techknowlogick@noreply.gitea.com> Reviewed-on: https://gitea.com/gitea/tea/pulls/782 Reviewed-by: techknowlogick <techknowlogick@noreply.gitea.com> Co-authored-by: TheFox0x7 <thefox0x7@gmail.com> Co-committed-by: TheFox0x7 <thefox0x7@gmail.com>
This commit is contained in:

committed by
techknowlogick

parent
a010c9bc7f
commit
5f35cebcf1
19
README.md
19
README.md
@ -46,7 +46,6 @@
|
||||
SETUP:
|
||||
logins, login Log in to a Gitea server
|
||||
logout Log out from a Gitea server
|
||||
shellcompletion, autocomplete Install shell completion for tea
|
||||
whoami Show current logged in user
|
||||
|
||||
OPTIONS
|
||||
@ -101,6 +100,24 @@ There are different ways to get `tea`:
|
||||
|
||||
5. asdf (thirdparty): [mvaldes14/asdf-tea](https://github.com/mvaldes14/asdf-tea)
|
||||
|
||||
### Shell completion
|
||||
|
||||
If you installed from source or the package does not provide the completions with it you can add them yourself with `tea completion <shell>` command which is not visible in help. To generate the completions run one of the following commands depending on your shell.
|
||||
|
||||
```shell
|
||||
# .bashrc
|
||||
source <(tea completion bash)
|
||||
|
||||
# .zshrc
|
||||
source <(tea completion zsh)
|
||||
|
||||
# fish
|
||||
tea completion fish > ~/.config/fish/completions/tea.fish
|
||||
|
||||
# Powershell
|
||||
Output the script to path/to/autocomplete/tea.ps1 an run it.
|
||||
```
|
||||
|
||||
## Compilation
|
||||
|
||||
Make sure you have a current go version installed (1.13 or newer).
|
||||
|
@ -1,138 +0,0 @@
|
||||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
"github.com/adrg/xdg"
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
// CmdAutocomplete manages autocompletion
|
||||
var CmdAutocomplete = cli.Command{
|
||||
Name: "shellcompletion",
|
||||
Aliases: []string{"autocomplete"},
|
||||
Category: catSetup,
|
||||
Usage: "Install shell completion for tea",
|
||||
Description: "Install shell completion for tea",
|
||||
ArgsUsage: "<shell type> (bash, zsh, powershell, fish)",
|
||||
Flags: []cli.Flag{
|
||||
&cli.BoolFlag{
|
||||
Name: "install",
|
||||
Usage: "Persist in shell config instead of printing commands",
|
||||
},
|
||||
},
|
||||
Action: runAutocompleteAdd,
|
||||
}
|
||||
|
||||
func runAutocompleteAdd(_ context.Context, cmd *cli.Command) error {
|
||||
var remoteFile, localFile, cmds string
|
||||
shell := cmd.Args().First()
|
||||
|
||||
switch shell {
|
||||
case "zsh":
|
||||
remoteFile = "contrib/autocomplete.zsh"
|
||||
localFile = "autocomplete.zsh"
|
||||
cmds = "echo 'PROG=tea _CLI_ZSH_AUTOCOMPLETE_HACK=1 source \"%s\"' >> ~/.zshrc && source ~/.zshrc"
|
||||
|
||||
case "bash":
|
||||
remoteFile = "contrib/autocomplete.sh"
|
||||
localFile = "autocomplete.sh"
|
||||
cmds = "echo 'PROG=tea source \"%s\"' >> ~/.bashrc && source ~/.bashrc"
|
||||
|
||||
case "powershell":
|
||||
remoteFile = "contrib/autocomplete.ps1"
|
||||
localFile = "tea.ps1"
|
||||
cmds = "\"& %s\" >> $profile"
|
||||
|
||||
case "fish":
|
||||
// fish is different, in that urfave/cli provides a generator for the shell script needed.
|
||||
// this also means that the fish completion can become out of sync with the tea binary!
|
||||
// writing to this directory suffices, as fish reads files there on startup, no cmds needed.
|
||||
return writeFishAutoCompleteFile(cmd)
|
||||
|
||||
default:
|
||||
return fmt.Errorf("Must specify valid %s", cmd.ArgsUsage)
|
||||
}
|
||||
|
||||
localPath, err := xdg.ConfigFile("tea/" + localFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cmds = fmt.Sprintf(cmds, localPath)
|
||||
if err = writeRemoteAutoCompleteFile(remoteFile, localPath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if cmd.Bool("install") {
|
||||
fmt.Println("Installing in your shellrc")
|
||||
installer := exec.Command(shell, "-c", cmds)
|
||||
if shell == "powershell" {
|
||||
installer = exec.Command("powershell.exe", "-Command", 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 writeRemoteAutoCompleteFile(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
|
||||
}
|
||||
|
||||
func writeFishAutoCompleteFile(cmd *cli.Command) error {
|
||||
// NOTE: to make sure this file is in sync with tea commands, we'd need to
|
||||
// - check if the file exists
|
||||
// - if it does, check if the tea version that wrote it is the currently running version
|
||||
// - if not, rewrite the file
|
||||
// on each application run
|
||||
// NOTE: this generates a completion that also suggests file names, which looks kinda messy..
|
||||
script, err := cmd.ToFishCompletion()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
localPath, err := xdg.ConfigFile("fish/conf.d/tea_completion.fish")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
writer, err := os.Create(localPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = io.WriteString(writer, script); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("Installed tab completion to %s\n", localPath)
|
||||
return nil
|
||||
}
|
@ -36,7 +36,6 @@ func App() *cli.Command {
|
||||
Commands: []*cli.Command{
|
||||
&CmdLogin,
|
||||
&CmdLogout,
|
||||
&CmdAutocomplete,
|
||||
&CmdWhoami,
|
||||
|
||||
&CmdIssues,
|
||||
|
@ -1,9 +0,0 @@
|
||||
$fn = $($MyInvocation.MyCommand.Name)
|
||||
$name = $fn -replace "(.*)\.ps1$", '$1'
|
||||
Register-ArgumentCompleter -Native -CommandName $name -ScriptBlock {
|
||||
param($commandName, $wordToComplete, $cursorPosition)
|
||||
$other = "$wordToComplete --generate-bash-completion"
|
||||
Invoke-Expression $other | ForEach-Object {
|
||||
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
#! /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
|
@ -1,23 +0,0 @@
|
||||
#compdef $PROG
|
||||
|
||||
_cli_zsh_autocomplete() {
|
||||
|
||||
local -a opts
|
||||
local cur
|
||||
cur=${words[-1]}
|
||||
if [[ "$cur" == "-"* ]]; then
|
||||
opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
|
||||
else
|
||||
opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} --generate-bash-completion)}")
|
||||
fi
|
||||
|
||||
if [[ "${opts[1]}" != "" ]]; then
|
||||
_describe 'values' opts
|
||||
else
|
||||
_files
|
||||
fi
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
compdef _cli_zsh_autocomplete $PROG
|
@ -95,12 +95,6 @@ Refresh an OAuth token
|
||||
|
||||
Log out from a Gitea server
|
||||
|
||||
## shellcompletion, autocomplete
|
||||
|
||||
Install shell completion for tea
|
||||
|
||||
**--install**: Persist in shell config instead of printing commands
|
||||
|
||||
## whoami
|
||||
|
||||
Show current logged in user
|
||||
|
Reference in New Issue
Block a user