diff options
| author | Eyal Posener <[email protected]> | 2019-11-19 05:28:26 +0200 |
|---|---|---|
| committer | Eyal Posener <[email protected]> | 2019-11-19 05:28:26 +0200 |
| commit | 3794f4987cf961143340f197318d540c44ed459a (patch) | |
| tree | b718badcea7aca8b1f09f6edfb67aacec4cefe9f /install/zsh.go | |
| parent | c3bfbddfe6b4d133259ee84a9f0f93a7d1b0971e (diff) | |
Actually adding the install package
Diffstat (limited to 'install/zsh.go')
| -rw-r--r-- | install/zsh.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/install/zsh.go b/install/zsh.go new file mode 100644 index 0000000..29950ab --- /dev/null +++ b/install/zsh.go @@ -0,0 +1,44 @@ +package install + +import "fmt" + +// (un)install in zsh +// basically adds/remove from .zshrc: +// +// autoload -U +X bashcompinit && bashcompinit" +// complete -C </path/to/completion/command> <command> +type zsh struct { + rc string +} + +func (z zsh) IsInstalled(cmd, bin string) bool { + completeCmd := z.cmd(cmd, bin) + return lineInFile(z.rc, completeCmd) +} + +func (z zsh) Install(cmd, bin string) error { + if z.IsInstalled(cmd, bin) { + return fmt.Errorf("already installed in %s", z.rc) + } + + completeCmd := z.cmd(cmd, bin) + bashCompInit := "autoload -U +X bashcompinit && bashcompinit" + if !lineInFile(z.rc, bashCompInit) { + completeCmd = bashCompInit + "\n" + completeCmd + } + + return appendToFile(z.rc, completeCmd) +} + +func (z zsh) Uninstall(cmd, bin string) error { + if !z.IsInstalled(cmd, bin) { + return fmt.Errorf("does not installed in %s", z.rc) + } + + completeCmd := z.cmd(cmd, bin) + return removeFromFile(z.rc, completeCmd) +} + +func (zsh) cmd(cmd, bin string) string { + return fmt.Sprintf("complete -o nospace -C %s %s", bin, cmd) +} |
