-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcompletion.go
161 lines (134 loc) · 5.11 KB
/
completion.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (c) 2020, Ben Morgan. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/cassava/repoctl/pacman"
"github.com/cassava/repoctl/pacman/alpm"
"github.com/cassava/repoctl/pacman/aur"
"github.com/cassava/repoctl/pacman/pkgutil"
"github.com/cassava/repoctl/repo"
"github.com/spf13/cobra"
)
func init() {
MainCmd.AddCommand(completionCmd)
}
var completionCmd = &cobra.Command{
Use: "completion [bash|zsh|fish]",
Short: "Generate shell completion",
Long: `Generate shell completion scripts for repoctl.
If you installed repoctl via your package manager (probably Pacman), then
shell completions should already be installed. If this is not the case, and
you only have the repoctl binary, then this command can help you add
completion to your shell.
Completions are supported for three shells: Bash, Zsh, and Fish.
If you don't provide the shell for which the completion script should be
generated, it will generated one based on the SHELL environment variable.
Bash:
To load the completion for the current session:
source <(repoctl completion bash)
To install the completion for all sessions, execute once:
repoctl completion bash > /etc/bash_completion.d/repoctl
Zsh:
To install completions for all sessions, execute once:
repoctl completion zsh > "${fpath[1]}/_yourprogram"
If shell completion is not already enabled in your environment you will need
to enable it. You can execute the following once:
echo "autoload -U compinit; compinit" >> ~/.zshrc
You will need to start a new shell for this setup to take effect.
Fish:
To load the completion for the current session:
repoctl completion fish | source
To install completions for all your sessions, execute once:
repoctl completion fish > ~/.config/fish/completions/repoctl.fish
`,
DisableFlagsInUseLine: true,
Hidden: true,
ValidArgs: []string{"bash", "zsh", "fish"},
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var shell string
if len(args) == 0 {
val, ok := os.LookupEnv("SHELL")
if !ok {
return fmt.Errorf("cannot derive shell from SHELL environment variable")
}
shell = filepath.Base(val)
} else if len(args) == 1 {
shell = args[0]
} else {
return fmt.Errorf("the completion command expects a single argument")
}
switch shell {
case "bash":
cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
cmd.Root().GenZshCompletion(os.Stdout)
case "fish":
cmd.Root().GenFishCompletion(os.Stdout, true)
default:
return fmt.Errorf("unknown shell %q", shell)
}
return nil
},
}
func completeDirectory(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveFilterDirs
}
func completeNoFiles(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return nil, cobra.ShellCompDirectiveNoFileComp
}
func completeProfiles(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
result := make([]string, 0, len(Conf.Profiles))
for k := range Conf.Profiles {
result = append(result, k)
}
return result, cobra.ShellCompDirectiveNoFileComp
}
func completeLocalPackageFiles(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return alpm.PackageExtensions, cobra.ShellCompDirectiveFilterFileExt
}
func completeRepoPackageNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// FIXME: Take profiles into account.
r, err := repo.NewFromConf(Conf)
if err != nil || r == nil {
return nil, cobra.ShellCompDirectiveError
}
// Get the names of the packages in the repository
var names []string
names, err = pacman.ReadDirApproxOnlyNames(nil, r.Directory)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
return filterCompletionResults(names, args, toComplete)
}
func completeAURPackageNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// We don't complete when the argument is too small, because otherwise the
// AUR will probably be overloaded.
if len(toComplete) < 4 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
pkgs, err := aur.SearchByName(toComplete)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
return filterCompletionResults(pkgutil.Map(pkgs, pkgutil.PkgName), args, toComplete)
}
func filterCompletionResults(results []string, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// Filter out all names that we already have or that don't match.
alreadyHas := make(map[string]bool)
for _, x := range args {
alreadyHas[x] = true
}
filteredResults := make([]string, 0, len(results))
for _, x := range results {
if strings.HasPrefix(x, toComplete) && !alreadyHas[x] {
filteredResults = append(filteredResults, x)
}
}
return filteredResults, cobra.ShellCompDirectiveNoFileComp
}