diff options
Diffstat (limited to 'vendor/github.com/posener/complete/cmd/install')
4 files changed, 281 insertions, 0 deletions
diff --git a/vendor/github.com/posener/complete/cmd/install/bash.go b/vendor/github.com/posener/complete/cmd/install/bash.go new file mode 100644 index 0000000..a287f99 --- /dev/null +++ b/vendor/github.com/posener/complete/cmd/install/bash.go | |||
@@ -0,0 +1,32 @@ | |||
1 | package install | ||
2 | |||
3 | import "fmt" | ||
4 | |||
5 | // (un)install in bash | ||
6 | // basically adds/remove from .bashrc: | ||
7 | // | ||
8 | // complete -C </path/to/completion/command> <command> | ||
9 | type bash struct { | ||
10 | rc string | ||
11 | } | ||
12 | |||
13 | func (b bash) Install(cmd, bin string) error { | ||
14 | completeCmd := b.cmd(cmd, bin) | ||
15 | if lineInFile(b.rc, completeCmd) { | ||
16 | return fmt.Errorf("already installed in %s", b.rc) | ||
17 | } | ||
18 | return appendToFile(b.rc, completeCmd) | ||
19 | } | ||
20 | |||
21 | func (b bash) Uninstall(cmd, bin string) error { | ||
22 | completeCmd := b.cmd(cmd, bin) | ||
23 | if !lineInFile(b.rc, completeCmd) { | ||
24 | return fmt.Errorf("does not installed in %s", b.rc) | ||
25 | } | ||
26 | |||
27 | return removeFromFile(b.rc, completeCmd) | ||
28 | } | ||
29 | |||
30 | func (bash) cmd(cmd, bin string) string { | ||
31 | return fmt.Sprintf("complete -C %s %s", bin, cmd) | ||
32 | } | ||
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 @@ | |||
1 | package install | ||
2 | |||
3 | import ( | ||
4 | "errors" | ||
5 | "os" | ||
6 | "os/user" | ||
7 | "path/filepath" | ||
8 | |||
9 | "github.com/hashicorp/go-multierror" | ||
10 | ) | ||
11 | |||
12 | type 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 | ||
19 | func 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 | ||
41 | func 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 | |||
61 | func 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 | |||
74 | func getBinaryPath() (string, error) { | ||
75 | bin, err := os.Executable() | ||
76 | if err != nil { | ||
77 | return "", err | ||
78 | } | ||
79 | return filepath.Abs(bin) | ||
80 | } | ||
81 | |||
82 | func 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 | } | ||
diff --git a/vendor/github.com/posener/complete/cmd/install/utils.go b/vendor/github.com/posener/complete/cmd/install/utils.go new file mode 100644 index 0000000..2c8b44c --- /dev/null +++ b/vendor/github.com/posener/complete/cmd/install/utils.go | |||
@@ -0,0 +1,118 @@ | |||
1 | package install | ||
2 | |||
3 | import ( | ||
4 | "bufio" | ||
5 | "fmt" | ||
6 | "io" | ||
7 | "io/ioutil" | ||
8 | "os" | ||
9 | ) | ||
10 | |||
11 | func lineInFile(name string, lookFor string) bool { | ||
12 | f, err := os.Open(name) | ||
13 | if err != nil { | ||
14 | return false | ||
15 | } | ||
16 | defer f.Close() | ||
17 | r := bufio.NewReader(f) | ||
18 | prefix := []byte{} | ||
19 | for { | ||
20 | line, isPrefix, err := r.ReadLine() | ||
21 | if err == io.EOF { | ||
22 | return false | ||
23 | } | ||
24 | if err != nil { | ||
25 | return false | ||
26 | } | ||
27 | if isPrefix { | ||
28 | prefix = append(prefix, line...) | ||
29 | continue | ||
30 | } | ||
31 | line = append(prefix, line...) | ||
32 | if string(line) == lookFor { | ||
33 | return true | ||
34 | } | ||
35 | prefix = prefix[:0] | ||
36 | } | ||
37 | } | ||
38 | |||
39 | func appendToFile(name string, content string) error { | ||
40 | f, err := os.OpenFile(name, os.O_RDWR|os.O_APPEND, 0) | ||
41 | if err != nil { | ||
42 | return err | ||
43 | } | ||
44 | defer f.Close() | ||
45 | _, err = f.WriteString(fmt.Sprintf("\n%s\n", content)) | ||
46 | return err | ||
47 | } | ||
48 | |||
49 | func removeFromFile(name string, content string) error { | ||
50 | backup := name + ".bck" | ||
51 | err := copyFile(name, backup) | ||
52 | if err != nil { | ||
53 | return err | ||
54 | } | ||
55 | temp, err := removeContentToTempFile(name, content) | ||
56 | if err != nil { | ||
57 | return err | ||
58 | } | ||
59 | |||
60 | err = copyFile(temp, name) | ||
61 | if err != nil { | ||
62 | return err | ||
63 | } | ||
64 | |||
65 | return os.Remove(backup) | ||
66 | } | ||
67 | |||
68 | func removeContentToTempFile(name, content string) (string, error) { | ||
69 | rf, err := os.Open(name) | ||
70 | if err != nil { | ||
71 | return "", err | ||
72 | } | ||
73 | defer rf.Close() | ||
74 | wf, err := ioutil.TempFile("/tmp", "complete-") | ||
75 | if err != nil { | ||
76 | return "", err | ||
77 | } | ||
78 | defer wf.Close() | ||
79 | |||
80 | r := bufio.NewReader(rf) | ||
81 | prefix := []byte{} | ||
82 | for { | ||
83 | line, isPrefix, err := r.ReadLine() | ||
84 | if err == io.EOF { | ||
85 | break | ||
86 | } | ||
87 | if err != nil { | ||
88 | return "", err | ||
89 | } | ||
90 | if isPrefix { | ||
91 | prefix = append(prefix, line...) | ||
92 | continue | ||
93 | } | ||
94 | line = append(prefix, line...) | ||
95 | str := string(line) | ||
96 | if str == content { | ||
97 | continue | ||
98 | } | ||
99 | wf.WriteString(str + "\n") | ||
100 | prefix = prefix[:0] | ||
101 | } | ||
102 | return wf.Name(), nil | ||
103 | } | ||
104 | |||
105 | func copyFile(src string, dst string) error { | ||
106 | in, err := os.Open(src) | ||
107 | if err != nil { | ||
108 | return err | ||
109 | } | ||
110 | defer in.Close() | ||
111 | out, err := os.Create(dst) | ||
112 | if err != nil { | ||
113 | return err | ||
114 | } | ||
115 | defer out.Close() | ||
116 | _, err = io.Copy(out, in) | ||
117 | return err | ||
118 | } | ||
diff --git a/vendor/github.com/posener/complete/cmd/install/zsh.go b/vendor/github.com/posener/complete/cmd/install/zsh.go new file mode 100644 index 0000000..a625f53 --- /dev/null +++ b/vendor/github.com/posener/complete/cmd/install/zsh.go | |||
@@ -0,0 +1,39 @@ | |||
1 | package install | ||
2 | |||
3 | import "fmt" | ||
4 | |||
5 | // (un)install in zsh | ||
6 | // basically adds/remove from .zshrc: | ||
7 | // | ||
8 | // autoload -U +X bashcompinit && bashcompinit" | ||
9 | // complete -C </path/to/completion/command> <command> | ||
10 | type zsh struct { | ||
11 | rc string | ||
12 | } | ||
13 | |||
14 | func (z zsh) Install(cmd, bin string) error { | ||
15 | completeCmd := z.cmd(cmd, bin) | ||
16 | if lineInFile(z.rc, completeCmd) { | ||
17 | return fmt.Errorf("already installed in %s", z.rc) | ||
18 | } | ||
19 | |||
20 | bashCompInit := "autoload -U +X bashcompinit && bashcompinit" | ||
21 | if !lineInFile(z.rc, bashCompInit) { | ||
22 | completeCmd = bashCompInit + "\n" + completeCmd | ||
23 | } | ||
24 | |||
25 | return appendToFile(z.rc, completeCmd) | ||
26 | } | ||
27 | |||
28 | func (z zsh) Uninstall(cmd, bin string) error { | ||
29 | completeCmd := z.cmd(cmd, bin) | ||
30 | if !lineInFile(z.rc, completeCmd) { | ||
31 | return fmt.Errorf("does not installed in %s", z.rc) | ||
32 | } | ||
33 | |||
34 | return removeFromFile(z.rc, completeCmd) | ||
35 | } | ||
36 | |||
37 | func (zsh) cmd(cmd, bin string) string { | ||
38 | return fmt.Sprintf("complete -o nospace -C %s %s", bin, cmd) | ||
39 | } | ||