Install fixups

- remove root installation
- install according to shell type

Closes #7
This commit is contained in:
Eyal Posener 2017-05-08 07:32:13 +03:00
parent b2791b7360
commit 5db452a63f
4 changed files with 42 additions and 54 deletions

View File

@ -27,9 +27,9 @@ func Run(cmd string) {
} }
fmt.Println(c.action() + "ing...") fmt.Println(c.action() + "ing...")
if c.install { if c.install {
err = install.Install(cmd, c.root) err = install.Install(cmd)
} else { } else {
err = install.Uninstall(cmd, c.root) err = install.Uninstall(cmd)
} }
if err != nil { if err != nil {
fmt.Printf("%s failed! %s\n", c.action(), err) fmt.Printf("%s failed! %s\n", c.action(), err)
@ -40,7 +40,7 @@ func Run(cmd string) {
// prompt use for approval // prompt use for approval
func prompt(action, cmd string) bool { func prompt(action, cmd string) bool {
fmt.Printf("%s bash completion for %s? ", action, cmd) fmt.Printf("%s completion for %s? ", action, cmd)
var answer string var answer string
fmt.Scanln(&answer) fmt.Scanln(&answer)
@ -56,7 +56,6 @@ func prompt(action, cmd string) bool {
type config struct { type config struct {
install bool install bool
uninstall bool uninstall bool
root bool
yes bool yes bool
} }
@ -64,13 +63,9 @@ type config struct {
func parseFlags(cmd string) config { func parseFlags(cmd string) config {
var c config var c config
flag.BoolVar(&c.install, "install", false, flag.BoolVar(&c.install, "install", false,
fmt.Sprintf("Install bash completion for %s command", cmd)) fmt.Sprintf("Install completion for %s command", cmd))
flag.BoolVar(&c.uninstall, "uninstall", false, flag.BoolVar(&c.uninstall, "uninstall", false,
fmt.Sprintf("Uninstall bash completion for %s command", cmd)) fmt.Sprintf("Uninstall completion for %s command", cmd))
flag.BoolVar(&c.root, "root", false,
"(Un)Install as root:\n"+
" (Un)Install at /etc/bash_completion.d/ (user should have write permissions to that directory).\n"+
" If not set, a complete command will be added(removed) to ~/.bashrc")
flag.BoolVar(&c.yes, "y", false, "Don't prompt user for typing 'yes'") flag.BoolVar(&c.yes, "y", false, "Don't prompt user for typing 'yes'")
flag.Parse() flag.Parse()
return c return c

View File

@ -11,9 +11,9 @@ import (
"path/filepath" "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() bashRCFileName, err := bashRCFileName()
if err != nil { if err != nil {
return err return err
@ -32,7 +32,7 @@ func (home) Install(cmd, bin string) error {
return err return err
} }
func (home) Uninstall(cmd, bin string) error { func (bash) Uninstall(cmd, bin string) error {
bashRC, err := bashRCFileName() bashRC, err := bashRCFileName()
if err != nil { if err != nil {
return err return err
@ -57,6 +57,7 @@ func (home) Uninstall(cmd, bin string) error {
} }
return os.Remove(backup) return os.Remove(backup)
} }
func completeCmd(cmd, bin string) string { func completeCmd(cmd, bin string) string {

View File

@ -1,6 +1,8 @@
package install package install
import ( import (
"errors"
"fmt"
"os" "os"
"path/filepath" "path/filepath"
) )
@ -12,33 +14,47 @@ type installer interface {
// Install complete command given: // Install complete command given:
// cmd: is the command name // cmd: is the command name
// asRoot: if true the completion will be installed in /etc/bash_complete.d func Install(cmd string) error {
// otherwise the complete command will be added to the ~/.bashrc file. shell := shellType()
func Install(cmd string, asRoot bool) error { 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() bin, err := getBinaryPath()
if err != nil { if err != nil {
return err return err
} }
return getInstaller(asRoot).Install(cmd, bin) return i.Install(cmd, bin)
} }
// Uninstall complete command given: // Uninstall complete command given:
// cmd: is the command name // cmd: is the command name
// asRoot: if true the completion will be removed from /etc/bash_complete.d func Uninstall(cmd string) error {
// otherwise the complete command will be removed from the ~/.bashrc file. shell := shellType()
func Uninstall(cmd string, asRoot bool) error { 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() bin, err := getBinaryPath()
if err != nil { if err != nil {
return err return err
} }
return getInstaller(asRoot).Uninstall(cmd, bin) return i.Uninstall(cmd, bin)
} }
func getInstaller(asRoot bool) installer { func getInstaller(shell string) installer {
if asRoot { switch shell {
return root{} case "bash":
return bash{}
default:
return nil
} }
return home{}
} }
func getBinaryPath() (string, error) { func getBinaryPath() (string, error) {
@ -48,3 +64,8 @@ func getBinaryPath() (string, error) {
} }
return filepath.Abs(bin) return filepath.Abs(bin)
} }
func shellType() string {
shell := os.Getenv("SHELL")
return filepath.Base(shell)
}

View File

@ -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
}