2017-09-06 14:20:06 +00:00
|
|
|
package install
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
2020-05-04 16:39:08 +00:00
|
|
|
"runtime"
|
2017-09-06 14:20:06 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
|
|
)
|
|
|
|
|
|
|
|
type installer interface {
|
2020-05-04 16:39:08 +00:00
|
|
|
IsInstalled(cmd, bin string) bool
|
2017-09-06 14:20:06 +00:00
|
|
|
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 {
|
2019-03-26 21:50:42 +00:00
|
|
|
return errors.New("Did not find any shells to install")
|
2017-09-06 14:20:06 +00: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
|
|
|
|
}
|
|
|
|
|
2020-05-04 16:39:08 +00:00
|
|
|
// IsInstalled returns true if the completion
|
|
|
|
// for the given cmd is installed.
|
|
|
|
func IsInstalled(cmd string) bool {
|
|
|
|
bin, err := getBinaryPath()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, i := range installers() {
|
|
|
|
installed := i.IsInstalled(cmd, bin)
|
|
|
|
if installed {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-09-06 14:20:06 +00:00
|
|
|
// Uninstall complete command given:
|
|
|
|
// cmd: is the command name
|
|
|
|
func Uninstall(cmd string) error {
|
|
|
|
is := installers()
|
|
|
|
if len(is) == 0 {
|
2019-03-26 21:50:42 +00:00
|
|
|
return errors.New("Did not find any shells to uninstall")
|
2017-09-06 14:20:06 +00:00
|
|
|
}
|
|
|
|
bin, err := getBinaryPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, i := range is {
|
|
|
|
errI := i.Uninstall(cmd, bin)
|
|
|
|
if errI != nil {
|
2020-05-04 16:39:08 +00:00
|
|
|
err = multierror.Append(err, errI)
|
2017-09-06 14:20:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func installers() (i []installer) {
|
2020-05-04 16:39:08 +00:00
|
|
|
// The list of bash config files candidates where it is
|
|
|
|
// possible to install the completion command.
|
|
|
|
var bashConfFiles []string
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "darwin":
|
|
|
|
bashConfFiles = []string{".bash_profile"}
|
|
|
|
default:
|
|
|
|
bashConfFiles = []string{".bashrc", ".bash_profile", ".bash_login", ".profile"}
|
|
|
|
}
|
|
|
|
for _, rc := range bashConfFiles {
|
2017-09-06 14:20:06 +00:00
|
|
|
if f := rcFile(rc); f != "" {
|
|
|
|
i = append(i, bash{f})
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if f := rcFile(".zshrc"); f != "" {
|
|
|
|
i = append(i, zsh{f})
|
|
|
|
}
|
2019-03-26 21:50:42 +00:00
|
|
|
if d := fishConfigDir(); d != "" {
|
|
|
|
i = append(i, fish{d})
|
|
|
|
}
|
2017-09-06 14:20:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-26 21:50:42 +00:00
|
|
|
func fishConfigDir() string {
|
|
|
|
configDir := filepath.Join(getConfigHomePath(), "fish")
|
|
|
|
if configDir == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if info, err := os.Stat(configDir); err != nil || !info.IsDir() {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return configDir
|
|
|
|
}
|
|
|
|
|
|
|
|
func getConfigHomePath() string {
|
|
|
|
u, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
configHome := os.Getenv("XDG_CONFIG_HOME")
|
|
|
|
if configHome == "" {
|
|
|
|
return filepath.Join(u.HomeDir, ".config")
|
|
|
|
}
|
|
|
|
return configHome
|
|
|
|
}
|
|
|
|
|
2017-09-06 14:20:06 +00:00
|
|
|
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
|
|
|
|
}
|