Skip to content

Commit 43405c3

Browse files
zeripatha1012112796techknowlogick
authored
Add Bash and Zsh completion scripts (#22646)
This PR adds contrib scripts for bash and zsh completion. Simply call: ```bash source contrib/autocompletion/bash_autocomplete ``` or for Zsh: ```bash source contrib/autocompletion/zsh_autocomplete ``` Signed-off-by: Andrew Thornton <[email protected]> --------- Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: a1012112796 <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent 4de5cd9 commit 43405c3

File tree

8 files changed

+103
-0
lines changed

8 files changed

+103
-0
lines changed

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,7 @@ CMD ["/bin/s6-svscan", "/etc/s6"]
6464
COPY docker/root /
6565
COPY --from=build-env /go/src/code.gitea.io/gitea/gitea /app/gitea/gitea
6666
COPY --from=build-env /go/src/code.gitea.io/gitea/environment-to-ini /usr/local/bin/environment-to-ini
67+
COPY --from=build-env /go/src/code.gitea.io/gitea/contrib/autocompletion/bash_autocomplete /etc/profile.d/gitea_bash_autocomplete.sh
6768
RUN chmod 755 /usr/bin/entrypoint /app/gitea/gitea /usr/local/bin/gitea /usr/local/bin/environment-to-ini
6869
RUN chmod 755 /etc/s6/gitea/* /etc/s6/openssh/* /etc/s6/.s6-svscan/*
70+
RUN chmod 644 /etc/profile.d/gitea_bash_autocomplete.sh

Dockerfile.rootless

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ RUN chown git:git /var/lib/gitea /etc/gitea
5454
COPY docker/rootless /
5555
COPY --from=build-env --chown=root:root /go/src/code.gitea.io/gitea/gitea /app/gitea/gitea
5656
COPY --from=build-env --chown=root:root /go/src/code.gitea.io/gitea/environment-to-ini /usr/local/bin/environment-to-ini
57+
COPY --from=build-env /go/src/code.gitea.io/gitea/contrib/autocompletion/bash_autocomplete /etc/profile.d/gitea_bash_autocomplete.sh
5758
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
59+
RUN chmod 644 /etc/profile.d/gitea_bash_autocomplete.sh
5860

5961
#git:git
6062
USER 1000:1000

contrib/autocompletion/README

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Bash and Zsh completion
2+
=======================
3+
4+
From within the gitea root run:
5+
6+
```bash
7+
source contrib/autocompletion/bash_autocomplete
8+
```
9+
10+
or for zsh run:
11+
12+
```bash
13+
source contrib/autocompletion/zsh_autocomplete
14+
```
15+
16+
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`.
17+
If gitea has been installed as a different program pass in the `PROG` environment variable to set the correct program name.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#! /bin/bash
2+
# Heavily inspired by https://github.com/urfave/cli
3+
4+
_cli_bash_autocomplete() {
5+
if [[ "${COMP_WORDS[0]}" != "source" ]]; then
6+
local cur opts base
7+
COMPREPLY=()
8+
cur="${COMP_WORDS[COMP_CWORD]}"
9+
if [[ "$cur" == "-"* ]]; then
10+
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} ${cur} --generate-bash-completion )
11+
else
12+
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion )
13+
fi
14+
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
15+
return 0
16+
fi
17+
}
18+
19+
if [ -z "$PROG" ] && [ ! "$(command -v gitea &> /dev/null)" ] ; then
20+
complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete gitea
21+
elif [ -z "$PROG" ]; then
22+
complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete ./gitea
23+
complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete "$PWD/gitea"
24+
else
25+
complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete "$PROG"
26+
unset PROG
27+
fi
28+
29+
30+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#compdef ${PROG:=gitea}
2+
3+
4+
# Heavily inspired by https://github.com/urfave/cli
5+
6+
_cli_zsh_autocomplete() {
7+
8+
local -a opts
9+
local cur
10+
cur=${words[-1]}
11+
if [[ "$cur" == "-"* ]]; then
12+
opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
13+
else
14+
opts=("${(@f)$(_CLI_ZSH_AUTOCOMPLETE_HACK=1 ${words[@]:0:#words[@]-1} --generate-bash-completion)}")
15+
fi
16+
17+
if [[ "${opts[1]}" != "" ]]; then
18+
_describe 'values' opts
19+
else
20+
_files
21+
fi
22+
23+
return
24+
}
25+
26+
if [ -z $PROG ] ; then
27+
compdef _cli_zsh_autocomplete gitea
28+
else
29+
compdef _cli_zsh_autocomplete $(basename $PROG)
30+
fi

docs/content/doc/installation/from-binary.en-us.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ export GITEA_WORK_DIR=/var/lib/gitea/
129129
cp gitea /usr/local/bin/gitea
130130
```
131131

132+
### Adding bash/zsh autocompletion (from 1.19)
133+
134+
A script to enable bash-completion can be found at [`contrib/autocompletion/bash_autocomplete`](https://raw.githubusercontent.com/go-gitea/gitea/main/contrib/autocompletion/bash_autocomplete). This can be copied to `/usr/share/bash-completion/completions/gitea`
135+
or sourced within your `.bashrc`.
136+
137+
Similarly a script for zsh-completion can be found at [`contrib/autocompletion/zsh_autocomplete`](https://raw.githubusercontent.com/go-gitea/gitea/main/contrib/autocompletion/zsh_autocomplete). This can be copied to `/usr/share/zsh/_gitea` or sourced within your
138+
`.zshrc`.
139+
140+
YMMV and these scripts may need further improvement.
141+
132142
## Running Gitea
133143

134144
After you complete the above steps, you can run Gitea two ways:

docs/content/doc/installation/from-source.en-us.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,13 @@ LDFLAGS="-linkmode external -extldflags '-static' $LDFLAGS" TAGS="netgo osusergo
193193
```
194194

195195
This can be combined with `CC`, `GOOS`, and `GOARCH` as above.
196+
197+
### Adding bash/zsh autocompletion (from 1.19)
198+
199+
A script to enable bash-completion can be found at [`contrib/autocompletion/bash_autocomplete`](https://raw.githubusercontent.com/go-gitea/gitea/main/contrib/autocompletion/bash_autocomplete). This should be altered as appropriate and can be `source` in your `.bashrc`
200+
or copied as `/usr/share/bash-completion/completions/gitea`.
201+
202+
Similary a script for zsh-completion can be found at [`contrib/autocompletion/zsh_autocomplete`](https://raw.githubusercontent.com/go-gitea/gitea/main/contrib/autocompletion/zsh_autocomplete). This can be copied to `/usr/share/zsh/_gitea` or sourced within your
203+
`.zshrc`.
204+
205+
YMMV and these scripts may need further improvement.

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ arguments - which can alternatively be run by running the subcommand web.`
113113
setFlagsAndBeforeOnSubcommands(&app.Commands[i], defaultFlags, establishCustomPath)
114114
}
115115

116+
app.EnableBashCompletion = true
117+
116118
err := app.Run(os.Args)
117119
if err != nil {
118120
log.Fatal("Failed to run app with %s: %v", os.Args, err)

0 commit comments

Comments
 (0)