diff options
Diffstat (limited to 'cmd/install')
| -rw-r--r-- | cmd/install/bash.go (renamed from cmd/install/home.go) | 7 | ||||
| -rw-r--r-- | cmd/install/install.go | 45 | ||||
| -rw-r--r-- | cmd/install/root.go | 29 |
3 files changed, 37 insertions, 44 deletions
diff --git a/cmd/install/home.go b/cmd/install/bash.go index 2694e96..c8bff49 100644 --- a/cmd/install/home.go +++ b/cmd/install/bash.go @@ -11,9 +11,9 @@ import ( "path/filepath" ) -type home struct{} +type bash struct{} -func (home) Install(cmd, bin string) error { +func (bash) Install(cmd, bin string) error { bashRCFileName, err := bashRCFileName() if err != nil { return err @@ -32,7 +32,7 @@ func (home) Install(cmd, bin string) error { return err } -func (home) Uninstall(cmd, bin string) error { +func (bash) Uninstall(cmd, bin string) error { bashRC, err := bashRCFileName() if err != nil { return err @@ -57,6 +57,7 @@ func (home) Uninstall(cmd, bin string) error { } return os.Remove(backup) + } func completeCmd(cmd, bin string) string { diff --git a/cmd/install/install.go b/cmd/install/install.go index c20e11a..bb44ad8 100644 --- a/cmd/install/install.go +++ b/cmd/install/install.go @@ -1,6 +1,8 @@ package install import ( + "errors" + "fmt" "os" "path/filepath" ) @@ -12,33 +14,47 @@ type installer interface { // Install complete command given: // cmd: is the command name -// asRoot: if true the completion will be installed in /etc/bash_complete.d -// otherwise the complete command will be added to the ~/.bashrc file. -func Install(cmd string, asRoot bool) error { +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) + } bin, err := getBinaryPath() if err != nil { return err } - return getInstaller(asRoot).Install(cmd, bin) + return i.Install(cmd, bin) } // Uninstall complete command given: // cmd: is the command name -// asRoot: if true the completion will be removed from /etc/bash_complete.d -// otherwise the complete command will be removed from the ~/.bashrc file. -func Uninstall(cmd string, asRoot bool) error { +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) + } bin, err := getBinaryPath() if err != nil { return err } - return getInstaller(asRoot).Uninstall(cmd, bin) + return i.Uninstall(cmd, bin) } -func getInstaller(asRoot bool) installer { - if asRoot { - return root{} +func getInstaller(shell string) installer { + switch shell { + case "bash": + return bash{} + default: + return nil } - return home{} } func getBinaryPath() (string, error) { @@ -48,3 +64,8 @@ func getBinaryPath() (string, error) { } return filepath.Abs(bin) } + +func shellType() string { + shell := os.Getenv("SHELL") + return filepath.Base(shell) +} diff --git a/cmd/install/root.go b/cmd/install/root.go deleted file mode 100644 index 66e23b3..0000000 --- a/cmd/install/root.go +++ /dev/null @@ -1,29 +0,0 @@ -package install - -import "os" - -type root struct{} - -func (r root) Install(cmd string, bin string) error { - completeLink := getBashCompletionDLink(cmd) - err := r.Uninstall(cmd, bin) - if err != nil { - return err - } - return os.Symlink(bin, completeLink) -} - -func (root) Uninstall(cmd string, bin string) error { - completeLink := getBashCompletionDLink(cmd) - if _, err := os.Stat(completeLink); err == nil { - err := os.Remove(completeLink) - if err != nil { - return err - } - } - return nil -} - -func getBashCompletionDLink(cmd string) string { - return "/etc/bash_completion.d/" + cmd -} |
