diff options
| author | Anis Elleuch <[email protected]> | 2019-06-28 13:39:45 +0100 |
|---|---|---|
| committer | Anis Elleuch <[email protected]> | 2019-06-28 13:45:36 +0100 |
| commit | c6bcb58fc4de17d39c7b428b34659c12600263f9 (patch) | |
| tree | 028139ccfc5b79af1bc360c6743c79162ec474a5 /cmd/install/fish.go | |
| parent | 60e9d0a237999e714fb4d04727fe7ffa1b377c6e (diff) | |
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.
Diffstat (limited to 'cmd/install/fish.go')
| -rw-r--r-- | cmd/install/fish.go | 25 |
1 files changed, 19 insertions, 6 deletions
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} |
