aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/posener/complete/cmd/install/install.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/posener/complete/cmd/install/install.go')
-rw-r--r--vendor/github.com/posener/complete/cmd/install/install.go92
1 files changed, 92 insertions, 0 deletions
diff --git a/vendor/github.com/posener/complete/cmd/install/install.go b/vendor/github.com/posener/complete/cmd/install/install.go
new file mode 100644
index 0000000..082a226
--- /dev/null
+++ b/vendor/github.com/posener/complete/cmd/install/install.go
@@ -0,0 +1,92 @@
1package install
2
3import (
4 "errors"
5 "os"
6 "os/user"
7 "path/filepath"
8
9 "github.com/hashicorp/go-multierror"
10)
11
12type installer interface {
13 Install(cmd, bin string) error
14 Uninstall(cmd, bin string) error
15}
16
17// Install complete command given:
18// cmd: is the command name
19func Install(cmd string) error {
20 is := installers()
21 if len(is) == 0 {
22 return errors.New("Did not find any shells to install")
23 }
24 bin, err := getBinaryPath()
25 if err != nil {
26 return err
27 }
28
29 for _, i := range is {
30 errI := i.Install(cmd, bin)
31 if errI != nil {
32 err = multierror.Append(err, errI)
33 }
34 }
35
36 return err
37}
38
39// Uninstall complete command given:
40// cmd: is the command name
41func Uninstall(cmd string) error {
42 is := installers()
43 if len(is) == 0 {
44 return errors.New("Did not find any shells to uninstall")
45 }
46 bin, err := getBinaryPath()
47 if err != nil {
48 return err
49 }
50
51 for _, i := range is {
52 errI := i.Uninstall(cmd, bin)
53 if errI != nil {
54 multierror.Append(err, errI)
55 }
56 }
57
58 return err
59}
60
61func installers() (i []installer) {
62 for _, rc := range [...]string{".bashrc", ".bash_profile", ".bash_login", ".profile"} {
63 if f := rcFile(rc); f != "" {
64 i = append(i, bash{f})
65 break
66 }
67 }
68 if f := rcFile(".zshrc"); f != "" {
69 i = append(i, zsh{f})
70 }
71 return
72}
73
74func getBinaryPath() (string, error) {
75 bin, err := os.Executable()
76 if err != nil {
77 return "", err
78 }
79 return filepath.Abs(bin)
80}
81
82func rcFile(name string) string {
83 u, err := user.Current()
84 if err != nil {
85 return ""
86 }
87 path := filepath.Join(u.HomeDir, name)
88 if _, err := os.Stat(path); err != nil {
89 return ""
90 }
91 return path
92}