Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Bash and Zsh completion scripts #22646

Merged
merged 18 commits into from Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Expand Up @@ -64,5 +64,6 @@ CMD ["/bin/s6-svscan", "/etc/s6"]
COPY docker/root /
COPY --from=build-env /go/src/code.gitea.io/gitea/gitea /app/gitea/gitea
COPY --from=build-env /go/src/code.gitea.io/gitea/environment-to-ini /usr/local/bin/environment-to-ini
COPY contrib/autocompletion/bash_autocompletion /etc/profile.d/gitea_bash_autocompletion.sh
RUN chmod 755 /usr/bin/entrypoint /app/gitea/gitea /usr/local/bin/gitea /usr/local/bin/environment-to-ini
RUN chmod 755 /etc/s6/gitea/* /etc/s6/openssh/* /etc/s6/.s6-svscan/*
1 change: 1 addition & 0 deletions Dockerfile.rootless
Expand Up @@ -54,6 +54,7 @@ RUN chown git:git /var/lib/gitea /etc/gitea
COPY docker/rootless /
COPY --from=build-env --chown=root:root /go/src/code.gitea.io/gitea/gitea /app/gitea/gitea
COPY --from=build-env --chown=root:root /go/src/code.gitea.io/gitea/environment-to-ini /usr/local/bin/environment-to-ini
COPY contrib/autocompletion/bash_autocompletion /etc/profile.d/gitea_bash_autocompletion.sh
RUN chmod 755 /usr/local/bin/docker-entrypoint.sh /usr/local/bin/docker-setup.sh /app/gitea/gitea /usr/local/bin/gitea /usr/local/bin/environment-to-ini

#git:git
Expand Down
17 changes: 17 additions & 0 deletions contrib/autocompletion/README
@@ -0,0 +1,17 @@
Bash and Zsh completion
zeripath marked this conversation as resolved.
Show resolved Hide resolved
=======================

From within the gitea root run:

```bash
source contrib/autocompletion/bash_autocomplete
```

or for zsh run:

```bash
source contrib/autocompletion/zsh_autocomplete
```

These scripts will check if gitea is on the path and if so add autocompletion for `gitea`. Or if not autocompletion will work for `./gitea`.
If gitea has been installed as a different program pass in the `PROG` environment variable to set the correct program name.
30 changes: 30 additions & 0 deletions contrib/autocompletion/bash_autocomplete
@@ -0,0 +1,30 @@
#! /bin/bash
# Heavily inspired by https://github.com/urfave/cli

_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
}

if [ -z "$PROG" ] && [ ! "$(command -v gitea &> /dev/null)" ] ; then
complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete gitea
elif [ -z "$PROG" ]; then
complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete ./gitea
complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete "$PWD/gitea"
else
complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete "$PROG"
unset PROG
fi



30 changes: 30 additions & 0 deletions contrib/autocompletion/zsh_autocomplete
@@ -0,0 +1,30 @@
#compdef ${PROG:=gitea}


# Heavily inspired by https://github.com/urfave/cli

_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
}

if [ -z $PROG ] ; then
compdef _cli_zsh_autocomplete gitea
else
compdef _cli_zsh_autocomplete $(basename $PROG)
fi
10 changes: 10 additions & 0 deletions docs/content/doc/installation/from-binary.en-us.md
Expand Up @@ -127,6 +127,16 @@ export GITEA_WORK_DIR=/var/lib/gitea/
cp gitea /usr/local/bin/gitea
```

### Adding bash/zsh autocompletion (from 1.19)

A script to enable bash-completion can be found at `contrib/autocompletion/bash_autocomplete`. This can be copied to `/usr/share/bash-completion/completions/gitea`
zeripath marked this conversation as resolved.
Show resolved Hide resolved
or sourced within your `.bashrc`.

Similary a script for zsh-completion can be found at `contrib/autocompletion/zsh_autocomplete`. This can be copied to `/usr/share/zsh/_gitea` or sourced within your
zeripath marked this conversation as resolved.
Show resolved Hide resolved
`.zshrc`.

YMMV and these scripts may need further improvement.

## Running Gitea

After you complete the above steps, you can run Gitea two ways:
Expand Down
10 changes: 10 additions & 0 deletions docs/content/doc/installation/from-source.en-us.md
Expand Up @@ -193,3 +193,13 @@ LDFLAGS="-linkmode external -extldflags '-static' $LDFLAGS" TAGS="netgo osusergo
```

This can be combined with `CC`, `GOOS`, and `GOARCH` as above.

### Adding bash/zsh autocompletion (from 1.19)

A script to enable bash-completion can be found at `contrib/autocompletion/bash_autocomplete`. This should be altered as appropriate and can be `source` in your `.bashrc`
or copied as `/usr/share/bash-completion/completions/gitea`.

Similary a script for zsh-completion can be found at `contrib/autocompletion/zsh_autocomplete`. This can be copied to `/usr/share/zsh/_gitea` or sourced within your
zeripath marked this conversation as resolved.
Show resolved Hide resolved
`.zshrc`.

YMMV and these scripts may need further improvement.
2 changes: 2 additions & 0 deletions main.go
Expand Up @@ -113,6 +113,8 @@ arguments - which can alternatively be run by running the subcommand web.`
setFlagsAndBeforeOnSubcommands(&app.Commands[i], defaultFlags, establishCustomPath)
}

app.EnableBashCompletion = true

err := app.Run(os.Args)
if err != nil {
log.Fatal("Failed to run app with %s: %v", os.Args, err)
Expand Down