Skip to content

Commit

Permalink
Merge pull request #94 from vadmeste/add-is-installed
Browse files Browse the repository at this point in the history
Add IsInstalled API to installer interface
  • Loading branch information
posener authored Jul 1, 2019
2 parents 60e9d0a + c6bcb58 commit 33efd44
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
13 changes: 9 additions & 4 deletions cmd/install/bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
25 changes: 19 additions & 6 deletions cmd/install/fish.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
1 change: 1 addition & 0 deletions cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

type installer interface {
IsInstalled(cmd, bin string) bool
Install(cmd, bin string) error
Uninstall(cmd, bin string) error
}
Expand Down
13 changes: 9 additions & 4 deletions cmd/install/zsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand Down

0 comments on commit 33efd44

Please sign in to comment.