2017-09-06 14:20:06 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-04 16:39:08 +00:00
|
|
|
func (z zsh) IsInstalled(cmd, bin string) bool {
|
2017-09-06 14:20:06 +00:00
|
|
|
completeCmd := z.cmd(cmd, bin)
|
2020-05-04 16:39:08 +00:00
|
|
|
return lineInFile(z.rc, completeCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z zsh) Install(cmd, bin string) error {
|
|
|
|
if z.IsInstalled(cmd, bin) {
|
2017-09-06 14:20:06 +00:00
|
|
|
return fmt.Errorf("already installed in %s", z.rc)
|
|
|
|
}
|
|
|
|
|
2020-05-04 16:39:08 +00:00
|
|
|
completeCmd := z.cmd(cmd, bin)
|
2017-09-06 14:20:06 +00:00
|
|
|
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 {
|
2020-05-04 16:39:08 +00:00
|
|
|
if !z.IsInstalled(cmd, bin) {
|
2017-09-06 14:20:06 +00:00
|
|
|
return fmt.Errorf("does not installed in %s", z.rc)
|
|
|
|
}
|
|
|
|
|
2020-05-04 16:39:08 +00:00
|
|
|
completeCmd := z.cmd(cmd, bin)
|
2017-09-06 14:20:06 +00:00
|
|
|
return removeFromFile(z.rc, completeCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (zsh) cmd(cmd, bin string) string {
|
|
|
|
return fmt.Sprintf("complete -o nospace -C %s %s", bin, cmd)
|
|
|
|
}
|