complete/install/bash.go

38 lines
823 B
Go
Raw Normal View History

2019-11-18 21:28:26 -06:00
package install
import "fmt"
// (un)install in bash
// basically adds/remove from .bashrc:
//
// complete -C </path/to/completion/command> <command>
type bash struct {
rc string
}
func (b bash) IsInstalled(cmd, bin string) bool {
completeCmd := b.cmd(cmd, bin)
return lineInFile(b.rc, completeCmd)
}
func (b bash) Install(cmd, bin string) error {
if b.IsInstalled(cmd, bin) {
return fmt.Errorf("already installed in %s", b.rc)
}
completeCmd := b.cmd(cmd, bin)
2019-11-23 04:14:56 -06:00
return appendFile(b.rc, completeCmd)
2019-11-18 21:28:26 -06:00
}
func (b bash) Uninstall(cmd, bin string) error {
if !b.IsInstalled(cmd, bin) {
return fmt.Errorf("does not installed in %s", b.rc)
}
completeCmd := b.cmd(cmd, bin)
return removeFromFile(b.rc, completeCmd)
}
func (bash) cmd(cmd, bin string) string {
return fmt.Sprintf("complete -C %s %s", bin, cmd)
}