2017-05-06 10:55:54 -05:00
|
|
|
package install
|
|
|
|
|
|
|
|
import (
|
2017-05-07 23:32:13 -05:00
|
|
|
"errors"
|
2017-05-06 10:55:54 -05:00
|
|
|
"os"
|
2017-05-13 03:03:22 -05:00
|
|
|
"os/user"
|
2017-05-06 10:55:54 -05:00
|
|
|
"path/filepath"
|
2017-05-13 03:03:22 -05:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-multierror"
|
2017-05-06 10:55:54 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2017-05-13 03:03:22 -05:00
|
|
|
is := installers()
|
|
|
|
if len(is) == 0 {
|
2017-09-08 07:40:26 -05:00
|
|
|
return errors.New("Did not find any shells to install")
|
2017-05-07 23:32:13 -05:00
|
|
|
}
|
2017-05-06 10:55:54 -05:00
|
|
|
bin, err := getBinaryPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-13 03:03:22 -05:00
|
|
|
|
|
|
|
for _, i := range is {
|
|
|
|
errI := i.Install(cmd, bin)
|
|
|
|
if errI != nil {
|
2017-05-23 09:43:32 -05:00
|
|
|
err = multierror.Append(err, errI)
|
2017-05-13 03:03:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
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 {
|
2017-05-13 03:03:22 -05:00
|
|
|
is := installers()
|
|
|
|
if len(is) == 0 {
|
2017-09-08 07:40:26 -05:00
|
|
|
return errors.New("Did not find any shells to uninstall")
|
2017-05-07 23:32:13 -05:00
|
|
|
}
|
2017-05-06 10:55:54 -05:00
|
|
|
bin, err := getBinaryPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-13 03:03:22 -05:00
|
|
|
|
|
|
|
for _, i := range is {
|
|
|
|
errI := i.Uninstall(cmd, bin)
|
|
|
|
if errI != nil {
|
|
|
|
multierror.Append(err, errI)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2017-05-06 10:55:54 -05:00
|
|
|
}
|
|
|
|
|
2017-05-13 03:03:22 -05:00
|
|
|
func installers() (i []installer) {
|
Fixes the issue where if .profile is used and not any of the other files the -install-autocomplete fails with :
Error executing CLI: Did not find any shells to install
excerpt from bash man page
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of /etc/bash.bashrc and ~/.bashrc.
2017-12-19 04:05:40 -06:00
|
|
|
for _, rc := range [...]string{".bashrc", ".bash_profile", ".bash_login", ".profile"} {
|
2017-05-23 10:04:13 -05:00
|
|
|
if f := rcFile(rc); f != "" {
|
|
|
|
i = append(i, bash{f})
|
|
|
|
break
|
|
|
|
}
|
2017-05-06 10:55:54 -05:00
|
|
|
}
|
2017-05-13 03:03:22 -05:00
|
|
|
if f := rcFile(".zshrc"); f != "" {
|
|
|
|
i = append(i, zsh{f})
|
|
|
|
}
|
2018-01-08 02:51:45 -06:00
|
|
|
if d := fishConfigDir(); d != "" {
|
|
|
|
i = append(i, fish{d})
|
|
|
|
}
|
2017-05-13 03:03:22 -05:00
|
|
|
return
|
2017-05-06 10:55:54 -05:00
|
|
|
}
|
|
|
|
|
2018-01-08 02:51:45 -06: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-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
|
|
|
|
2017-05-13 03:03:22 -05:00
|
|
|
func rcFile(name string) string {
|
|
|
|
u, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
2017-05-23 09:45:43 -05:00
|
|
|
path := filepath.Join(u.HomeDir, name)
|
|
|
|
if _, err := os.Stat(path); err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return path
|
2017-05-07 23:32:13 -05:00
|
|
|
}
|