Add IsInstalled API to installer interface

`IsInstalled(cmd, bin string) bool` will help tools using this library
to show a prompt to users asking if they would like to have completion
enabled.
This commit is contained in:
Anis Elleuch 2019-06-28 13:39:45 +01:00
parent 60e9d0a237
commit c6bcb58fc4
4 changed files with 38 additions and 14 deletions

View File

@ -10,20 +10,25 @@ type bash struct {
rc string
}
func (b bash) Install(cmd, bin string) error {
func (b bash) IsInstalled(cmd, bin string) bool {
completeCmd := b.cmd(cmd, bin)
if lineInFile(b.rc, completeCmd) {
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)
return appendToFile(b.rc, completeCmd)
}
func (b bash) Uninstall(cmd, bin string) error {
completeCmd := b.cmd(cmd, bin)
if !lineInFile(b.rc, completeCmd) {
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)
}

View File

@ -14,28 +14,41 @@ type fish struct {
configDir string
}
func (f fish) IsInstalled(cmd, bin string) bool {
completionFile := f.getCompletionFilePath(cmd)
if _, err := os.Stat(completionFile); err == nil {
return true
}
return false
}
func (f fish) Install(cmd, bin string) error {
completionFile := filepath.Join(f.configDir, "completions", fmt.Sprintf("%s.fish", cmd))
if f.IsInstalled(cmd, bin) {
return fmt.Errorf("already installed at %s", f.getCompletionFilePath(cmd))
}
completionFile := f.getCompletionFilePath(cmd)
completeCmd, err := f.cmd(cmd, bin)
if err != nil {
return err
}
if _, err := os.Stat(completionFile); err == nil {
return fmt.Errorf("already installed at %s", completionFile)
}
return createFile(completionFile, completeCmd)
}
func (f fish) Uninstall(cmd, bin string) error {
completionFile := filepath.Join(f.configDir, "completions", fmt.Sprintf("%s.fish", cmd))
if _, err := os.Stat(completionFile); err != nil {
if !f.IsInstalled(cmd, bin) {
return fmt.Errorf("does not installed in %s", f.configDir)
}
completionFile := f.getCompletionFilePath(cmd)
return os.Remove(completionFile)
}
func (f fish) getCompletionFilePath(cmd string) string {
return filepath.Join(f.configDir, "completions", fmt.Sprintf("%s.fish", cmd))
}
func (f fish) cmd(cmd, bin string) (string, error) {
var buf bytes.Buffer
params := struct{ Cmd, Bin string }{cmd, bin}

View File

@ -11,6 +11,7 @@ import (
)
type installer interface {
IsInstalled(cmd, bin string) bool
Install(cmd, bin string) error
Uninstall(cmd, bin string) error
}

View File

@ -11,12 +11,17 @@ type zsh struct {
rc string
}
func (z zsh) Install(cmd, bin string) error {
func (z zsh) IsInstalled(cmd, bin string) bool {
completeCmd := z.cmd(cmd, bin)
if lineInFile(z.rc, completeCmd) {
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
@ -26,11 +31,11 @@ func (z zsh) Install(cmd, bin string) error {
}
func (z zsh) Uninstall(cmd, bin string) error {
completeCmd := z.cmd(cmd, bin)
if !lineInFile(z.rc, completeCmd) {
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)
}