1package install
2
3import "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>
10type zsh struct {
11	rc string
12}
13
14func (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
28func (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
37func (zsh) cmd(cmd, bin string) string {
38	return fmt.Sprintf("complete -o nospace -C %s %s", bin, cmd)
39}
40