2017-08-14 17:14:00 +02:00
|
|
|
package install
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
|
|
)
|
|
|
|
|
|
|
|
type installer interface {
|
|
|
|
Install(cmd, bin string) error
|
|
|
|
Uninstall(cmd, bin string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install complete command given:
|
|
|
|
// cmd: is the command name
|
|
|
|
func Install(cmd string) error {
|
|
|
|
is := installers()
|
|
|
|
if len(is) == 0 {
|
2017-12-19 13:02:16 +01:00
|
|
|
return errors.New("Did not find any shells to install")
|
2017-08-14 17:14:00 +02:00
|
|
|
}
|
|
|
|
bin, err := getBinaryPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, i := range is {
|
|
|
|
errI := i.Install(cmd, bin)
|
|
|
|
if errI != nil {
|
|
|
|
err = multierror.Append(err, errI)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uninstall complete command given:
|
|
|
|
// cmd: is the command name
|
|
|
|
func Uninstall(cmd string) error {
|
|
|
|
is := installers()
|
|
|
|
if len(is) == 0 {
|
2017-12-19 13:02:16 +01:00
|
|
|
return errors.New("Did not find any shells to uninstall")
|
2017-08-14 17:14:00 +02:00
|
|
|
}
|
|
|
|
bin, err := getBinaryPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, i := range is {
|
|
|
|
errI := i.Uninstall(cmd, bin)
|
|
|
|
if errI != nil {
|
|
|
|
multierror.Append(err, errI)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func installers() (i []installer) {
|
2017-12-19 13:02:16 +01:00
|
|
|
for _, rc := range [...]string{".bashrc", ".bash_profile", ".bash_login", ".profile"} {
|
2017-08-14 17:14:00 +02:00
|
|
|
if f := rcFile(rc); f != "" {
|
|
|
|
i = append(i, bash{f})
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if f := rcFile(".zshrc"); f != "" {
|
|
|
|
i = append(i, zsh{f})
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBinaryPath() (string, error) {
|
|
|
|
bin, err := os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.Abs(bin)
|
|
|
|
}
|
|
|
|
|
|
|
|
func rcFile(name string) string {
|
|
|
|
u, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
path := filepath.Join(u.HomeDir, name)
|
|
|
|
if _, err := os.Stat(path); err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|