diff options
| author | Eyal Posener <[email protected]> | 2017-05-13 11:10:46 +0300 |
|---|---|---|
| committer | GitHub <[email protected]> | 2017-05-13 11:10:46 +0300 |
| commit | 758253551ecf81fc5fb107ef8bdad2745d3525fb (patch) | |
| tree | 49ef787eeb6071d84adb7c8f35051d474351f11f /cmd/install/install.go | |
| parent | cc2d0e6974a8644191666e9e1e466b06c8bd6cde (diff) | |
| parent | 87f385425a98490ad44c01e33d16e6a4324696f0 (diff) | |
Merge pull request #18 from posener/zsh
zsh support
Diffstat (limited to 'cmd/install/install.go')
| -rw-r--r-- | cmd/install/install.go | 66 |
1 files changed, 40 insertions, 26 deletions
diff --git a/cmd/install/install.go b/cmd/install/install.go index bb44ad8..11581c1 100644 --- a/cmd/install/install.go +++ b/cmd/install/install.go @@ -2,9 +2,11 @@ package install import ( "errors" - "fmt" "os" + "os/user" "path/filepath" + + "github.com/hashicorp/go-multierror" ) type installer interface { @@ -15,46 +17,55 @@ type installer interface { // Install complete command given: // cmd: is the command name func Install(cmd string) error { - shell := shellType() - if shell == "" { - return errors.New("must install through a terminatl") - } - i := getInstaller(shell) - if i == nil { - return fmt.Errorf("shell %s not supported", shell) + is := installers() + if len(is) == 0 { + return errors.New("Did not found any shells to install") } bin, err := getBinaryPath() if err != nil { return err } - return i.Install(cmd, bin) + + for _, i := range is { + errI := i.Install(cmd, bin) + if errI != nil { + multierror.Append(err, errI) + } + } + + return err } // Uninstall complete command given: // cmd: is the command name func Uninstall(cmd string) error { - shell := shellType() - if shell == "" { - return errors.New("must uninstall through a terminatl") - } - i := getInstaller(shell) - if i == nil { - return fmt.Errorf("shell %s not supported", shell) + is := installers() + if len(is) == 0 { + return errors.New("Did not found any shells to uninstall") } bin, err := getBinaryPath() if err != nil { return err } - return i.Uninstall(cmd, bin) + + for _, i := range is { + errI := i.Uninstall(cmd, bin) + if errI != nil { + multierror.Append(err, errI) + } + } + + return err } -func getInstaller(shell string) installer { - switch shell { - case "bash": - return bash{} - default: - return nil +func installers() (i []installer) { + if f := rcFile(".bashrc"); f != "" { + i = append(i, bash{f}) } + if f := rcFile(".zshrc"); f != "" { + i = append(i, zsh{f}) + } + return } func getBinaryPath() (string, error) { @@ -65,7 +76,10 @@ func getBinaryPath() (string, error) { return filepath.Abs(bin) } -func shellType() string { - shell := os.Getenv("SHELL") - return filepath.Base(shell) +func rcFile(name string) string { + u, err := user.Current() + if err != nil { + return "" + } + return filepath.Join(u.HomeDir, name) } |
