Skip to content

Commit 5fb91e6

Browse files
committed
docs: document bash completion in README
Also, change command `golangci-lint completion` to `golangci-lint completion bash`.
1 parent 91e90eb commit 5fb91e6

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Sponsored by [GolangCI.com](https://golangci.com): SaaS service for running lint
1919
* [Trusted By](#trusted-by)
2020
* [Quick Start](#quick-start)
2121
* [Editor Integration](#editor-integration)
22+
* [Shell Completion](#shell-completion)
2223
* [Comparison](#comparison)
2324
* [Performance](#performance)
2425
* [Internals](#internals)
@@ -250,6 +251,35 @@ golangci-lint run --disable-all -E errcheck
250251
* ale [merged pull request](https://github.com/w0rp/ale/pull/1890) with golangci-lint support
251252
6. Atom - [go-plus](https://atom.io/packages/go-plus) supports golangci-lint.
252253
254+
## Shell Completion
255+
256+
`golangci-lint` can generate bash completion file.
257+
258+
### Mac OS X
259+
260+
Yhere are two versions of `bash-completion`, v1 and v2. V1 is for Bash 3.2 (which is the default on macOS), and v2 is for Bash 4.1+. The `golangci-lint` completion script doesn’t work correctly with bash-completion v1 and Bash 3.2. It requires bash-completion v2 and Bash 4.1+. Thus, to be able to correctly use `golangci-lint` completion on macOS, you have to install and use Bash 4.1+ ([instructions](https://itnext.io/upgrading-bash-on-macos-7138bd1066ba)). The following instructions assume that you use Bash 4.1+ (that is, any Bash version of 4.1 or newer).
261+
262+
Install `bash-completion v2`:
263+
264+
```bash
265+
brew install bash-completion@2
266+
echo 'export BASH_COMPLETION_COMPAT_DIR="/usr/local/etc/bash_completion.d"' >>~/.bashrc
267+
echo '[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"' >>~/.bashrc
268+
exec bash # reload and replace (if it was updated) shell
269+
type _init_completion && echo "completion is OK" # verify that bash-completion v2 is correctly installed
270+
```
271+
272+
Add `golangci-lint` bash completion:
273+
274+
```bash
275+
echo 'source <(golangci-lint completion bash)' >>~/.bashrc
276+
source ~/.bashrc
277+
```
278+
279+
### Linux
280+
281+
See [kubectl instructions](https://kubernetes.io/docs/tasks/tools/install-kubectl/#enabling-shell-autocompletion) and don't forget to replace `kubectl` with `golangci-lint`.
282+
253283
## Comparison
254284
255285
### `golangci-lint` vs `gometalinter`

README.tmpl.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Sponsored by [GolangCI.com](https://golangci.com): SaaS service for running lint
1919
* [Trusted By](#trusted-by)
2020
* [Quick Start](#quick-start)
2121
* [Editor Integration](#editor-integration)
22+
* [Shell Completion](#shell-completion)
2223
* [Comparison](#comparison)
2324
* [Performance](#performance)
2425
* [Internals](#internals)
@@ -217,6 +218,35 @@ golangci-lint run --disable-all -E errcheck
217218
* ale [merged pull request](https://github.com/w0rp/ale/pull/1890) with golangci-lint support
218219
6. Atom - [go-plus](https://atom.io/packages/go-plus) supports golangci-lint.
219220

221+
## Shell Completion
222+
223+
`golangci-lint` can generate bash completion file.
224+
225+
### Mac OS X
226+
227+
Yhere are two versions of `bash-completion`, v1 and v2. V1 is for Bash 3.2 (which is the default on macOS), and v2 is for Bash 4.1+. The `golangci-lint` completion script doesn’t work correctly with bash-completion v1 and Bash 3.2. It requires bash-completion v2 and Bash 4.1+. Thus, to be able to correctly use `golangci-lint` completion on macOS, you have to install and use Bash 4.1+ ([instructions](https://itnext.io/upgrading-bash-on-macos-7138bd1066ba)). The following instructions assume that you use Bash 4.1+ (that is, any Bash version of 4.1 or newer).
228+
229+
Install `bash-completion v2`:
230+
231+
```bash
232+
brew install bash-completion@2
233+
echo 'export BASH_COMPLETION_COMPAT_DIR="/usr/local/etc/bash_completion.d"' >>~/.bashrc
234+
echo '[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"' >>~/.bashrc
235+
exec bash # reload and replace (if it was updated) shell
236+
type _init_completion && echo "completion is OK" # verify that bash-completion v2 is correctly installed
237+
```
238+
239+
Add `golangci-lint` bash completion:
240+
241+
```bash
242+
echo 'source <(golangci-lint completion bash)' >>~/.bashrc
243+
source ~/.bashrc
244+
```
245+
246+
### Linux
247+
248+
See [kubectl instructions](https://kubernetes.io/docs/tasks/tools/install-kubectl/#enabling-shell-autocompletion) and don't forget to replace `kubectl` with `golangci-lint`.
249+
220250
## Comparison
221251

222252
### `golangci-lint` vs `gometalinter`

pkg/commands/completion.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
package commands
22

33
import (
4-
"fmt"
54
"os"
65

6+
"github.com/pkg/errors"
77
"github.com/spf13/cobra"
88
)
99

1010
func (e *Executor) initCompletion() {
1111
completionCmd := &cobra.Command{
1212
Use: "completion",
13-
Short: "Generates bash completion scripts",
14-
RunE: e.executeCompletion,
13+
Short: "Output completion script",
1514
}
1615
e.rootCmd.AddCommand(completionCmd)
16+
17+
bashCmd := &cobra.Command{
18+
Use: "bash",
19+
Short: "Output bash completion script",
20+
RunE: e.executeCompletion,
21+
}
22+
completionCmd.AddCommand(bashCmd)
1723
}
1824

1925
func (e *Executor) executeCompletion(cmd *cobra.Command, args []string) error {
2026
err := cmd.Root().GenBashCompletion(os.Stdout)
2127
if err != nil {
22-
return fmt.Errorf("unable to generate bash completions: %v", err)
28+
return errors.Wrap(err, "unable to generate bash completions: %v")
2329
}
2430

2531
return nil

0 commit comments

Comments
 (0)