Merge pull request #94 from vadmeste/add-is-installed

Add IsInstalled API to installer interface
This commit is contained in:
Eyal Posener 2019-07-01 07:18:04 +03:00 committed by GitHub
commit 33efd44daa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 14 deletions

View File

@ -10,20 +10,25 @@ type bash struct {
rc string rc string
} }
func (b bash) Install(cmd, bin string) error { func (b bash) IsInstalled(cmd, bin string) bool {
completeCmd := b.cmd(cmd, bin) 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) return fmt.Errorf("already installed in %s", b.rc)
} }
completeCmd := b.cmd(cmd, bin)
return appendToFile(b.rc, completeCmd) return appendToFile(b.rc, completeCmd)
} }
func (b bash) Uninstall(cmd, bin string) error { func (b bash) Uninstall(cmd, bin string) error {
completeCmd := b.cmd(cmd, bin) if !b.IsInstalled(cmd, bin) {
if !lineInFile(b.rc, completeCmd) {
return fmt.Errorf("does not installed in %s", b.rc) return fmt.Errorf("does not installed in %s", b.rc)
} }
completeCmd := b.cmd(cmd, bin)
return removeFromFile(b.rc, completeCmd) return removeFromFile(b.rc, completeCmd)
} }

View File

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

View File

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

View File

@ -11,12 +11,17 @@ type zsh struct {
rc string rc string
} }
func (z zsh) Install(cmd, bin string) error { func (z zsh) IsInstalled(cmd, bin string) bool {
completeCmd := z.cmd(cmd, bin) 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) return fmt.Errorf("already installed in %s", z.rc)
} }
completeCmd := z.cmd(cmd, bin)
bashCompInit := "autoload -U +X bashcompinit && bashcompinit" bashCompInit := "autoload -U +X bashcompinit && bashcompinit"
if !lineInFile(z.rc, bashCompInit) { if !lineInFile(z.rc, bashCompInit) {
completeCmd = bashCompInit + "\n" + completeCmd completeCmd = bashCompInit + "\n" + completeCmd
@ -26,11 +31,11 @@ func (z zsh) Install(cmd, bin string) error {
} }
func (z zsh) Uninstall(cmd, bin string) error { func (z zsh) Uninstall(cmd, bin string) error {
completeCmd := z.cmd(cmd, bin) if !z.IsInstalled(cmd, bin) {
if !lineInFile(z.rc, completeCmd) {
return fmt.Errorf("does not installed in %s", z.rc) return fmt.Errorf("does not installed in %s", z.rc)
} }
completeCmd := z.cmd(cmd, bin)
return removeFromFile(z.rc, completeCmd) return removeFromFile(z.rc, completeCmd)
} }