From c6bcb58fc4de17d39c7b428b34659c12600263f9 Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Fri, 28 Jun 2019 13:39:45 +0100 Subject: [PATCH] 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. --- cmd/install/bash.go | 13 +++++++++---- cmd/install/fish.go | 25 +++++++++++++++++++------ cmd/install/install.go | 1 + cmd/install/zsh.go | 13 +++++++++---- 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/cmd/install/bash.go b/cmd/install/bash.go index a287f99..17c64de 100644 --- a/cmd/install/bash.go +++ b/cmd/install/bash.go @@ -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) } diff --git a/cmd/install/fish.go b/cmd/install/fish.go index c4f2018..2b64bfc 100644 --- a/cmd/install/fish.go +++ b/cmd/install/fish.go @@ -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} diff --git a/cmd/install/install.go b/cmd/install/install.go index 3cb9746..a9a01d4 100644 --- a/cmd/install/install.go +++ b/cmd/install/install.go @@ -11,6 +11,7 @@ import ( ) type installer interface { + IsInstalled(cmd, bin string) bool Install(cmd, bin string) error Uninstall(cmd, bin string) error } diff --git a/cmd/install/zsh.go b/cmd/install/zsh.go index a625f53..29950ab 100644 --- a/cmd/install/zsh.go +++ b/cmd/install/zsh.go @@ -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) }