2017-05-06 10:55:54 -05:00
|
|
|
package install
|
|
|
|
|
|
|
|
import (
|
2017-05-07 23:32:13 -05:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-05-06 10:55:54 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
type installer interface {
|
|
|
|
Install(cmd, bin string) error
|
|
|
|
Uninstall(cmd, bin string) error
|
|
|
|
}
|
|
|
|
|
2017-05-06 14:25:44 -05:00
|
|
|
// Install complete command given:
|
|
|
|
// cmd: is the command name
|
2017-05-07 23:32:13 -05:00
|
|
|
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)
|
|
|
|
}
|
2017-05-06 10:55:54 -05:00
|
|
|
bin, err := getBinaryPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-07 23:32:13 -05:00
|
|
|
return i.Install(cmd, bin)
|
2017-05-06 10:55:54 -05:00
|
|
|
}
|
|
|
|
|
2017-05-06 14:25:44 -05:00
|
|
|
// Uninstall complete command given:
|
|
|
|
// cmd: is the command name
|
2017-05-07 23:32:13 -05:00
|
|
|
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)
|
|
|
|
}
|
2017-05-06 10:55:54 -05:00
|
|
|
bin, err := getBinaryPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-07 23:32:13 -05:00
|
|
|
return i.Uninstall(cmd, bin)
|
2017-05-06 10:55:54 -05:00
|
|
|
}
|
|
|
|
|
2017-05-07 23:32:13 -05:00
|
|
|
func getInstaller(shell string) installer {
|
|
|
|
switch shell {
|
|
|
|
case "bash":
|
|
|
|
return bash{}
|
|
|
|
default:
|
|
|
|
return nil
|
2017-05-06 10:55:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBinaryPath() (string, error) {
|
|
|
|
bin, err := os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.Abs(bin)
|
|
|
|
}
|
2017-05-07 23:32:13 -05:00
|
|
|
|
|
|
|
func shellType() string {
|
|
|
|
shell := os.Getenv("SHELL")
|
|
|
|
return filepath.Base(shell)
|
|
|
|
}
|