forked from buildpacks/pack
/
completion.go
65 lines (50 loc) · 1.66 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
package commands
import (
"path/filepath"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/YousefHaggyHeroku/pack/internal/config"
"github.com/YousefHaggyHeroku/pack/logging"
)
type CompletionFlags struct {
Shell string
}
func CompletionCommand(logger logging.Logger) *cobra.Command {
var flags CompletionFlags
var completionCmd = &cobra.Command{
Use: "completion",
Short: "Outputs completion script location",
Long: `Generates bash completion script and outputs its location.
To configure your bash shell to load completions for each session, add the following to your '.bashrc' or '.bash_profile':
. $(pack completion)
To configure your zsh shell to load completions for each session, add the following to your '.zshrc':
. $(pack completion --shell zsh)
`,
RunE: logError(logger, func(cmd *cobra.Command, args []string) error {
packHome, err := config.PackHome()
if err != nil {
return errors.Wrap(err, "getting pack home")
}
if err = config.MkdirAll(packHome); err != nil {
return errors.Wrapf(err, "creating pack home: %s", packHome)
}
completionPath := filepath.Join(packHome, "completion")
var flagErr error
switch flags.Shell {
case "bash":
flagErr = cmd.Parent().GenBashCompletionFile(completionPath)
case "zsh":
flagErr = cmd.Parent().GenZshCompletionFile(completionPath)
default:
return errors.Errorf("%s is unsupported shell", flags.Shell)
}
if flagErr != nil {
return flagErr
}
logger.Info(completionPath)
return nil
}),
}
completionCmd.Flags().StringVarP(&flags.Shell, "shell", "s", "bash", "Generates completion file for [bash|zsh]")
return completionCmd
}