diff options
| author | Eyal Posener <[email protected]> | 2017-05-06 22:06:49 +0300 |
|---|---|---|
| committer | Eyal Posener <[email protected]> | 2017-05-06 22:15:15 +0300 |
| commit | 703dd6ebc30f7c6f5a5c02e07a307e0e34d9c2c2 (patch) | |
| tree | 6efd5f6d1205eb6188193c86f5dd1e256b8996c5 /install | |
| parent | 9963a854946be0603f9e79ccba0a8b2688b20053 (diff) | |
improve docs
Diffstat (limited to 'install')
| -rw-r--r-- | install/home.go | 153 | ||||
| -rw-r--r-- | install/install.go | 43 | ||||
| -rw-r--r-- | install/root.go | 29 |
3 files changed, 0 insertions, 225 deletions
diff --git a/install/home.go b/install/home.go deleted file mode 100644 index 825bdb7..0000000 --- a/install/home.go +++ /dev/null @@ -1,153 +0,0 @@ -package install - -import ( - "bufio" - "errors" - "fmt" - "io" - "io/ioutil" - "os" - "os/user" - "path/filepath" -) - -type home struct{} - -func (home) Install(cmd, bin string) error { - bashRCFileName, err := bashRCFileName() - if err != nil { - return err - } - completeCmd := completeCmd(cmd, bin) - if isInFile(bashRCFileName, completeCmd) { - return errors.New("Already installed in ~/.bashrc") - } - - bashRC, err := os.OpenFile(bashRCFileName, os.O_RDWR|os.O_APPEND, 0) - if err != nil { - return err - } - defer bashRC.Close() - _, err = bashRC.WriteString(fmt.Sprintf("\n%s\n", completeCmd)) - return err -} - -func (home) Uninstall(cmd, bin string) error { - bashRC, err := bashRCFileName() - if err != nil { - return err - } - backup := bashRC + ".bck" - err = copyFile(bashRC, backup) - if err != nil { - return err - } - completeCmd := completeCmd(cmd, bin) - if !isInFile(bashRC, completeCmd) { - return errors.New("Does not installed in ~/.bashrc") - } - temp, err := uninstallToTemp(bashRC, completeCmd) - if err != nil { - return err - } - - err = copyFile(temp, bashRC) - if err != nil { - return err - } - - return os.Remove(backup) -} - -func completeCmd(cmd, bin string) string { - return fmt.Sprintf("complete -C %s %s", bin, cmd) -} - -func bashRCFileName() (string, error) { - u, err := user.Current() - if err != nil { - return "", err - } - return filepath.Join(u.HomeDir, ".bashrc"), nil -} - -func isInFile(name string, lookFor string) bool { - f, err := os.Open(name) - if err != nil { - return false - } - defer f.Close() - r := bufio.NewReader(f) - prefix := []byte{} - for { - line, isPrefix, err := r.ReadLine() - if err == io.EOF { - return false - } - if err != nil { - return false - } - if isPrefix { - prefix = append(prefix, line...) - continue - } - line = append(prefix, line...) - if string(line) == lookFor { - return true - } - prefix = prefix[:0] - } - return false -} - -func uninstallToTemp(bashRCFileName, completeCmd string) (string, error) { - rf, err := os.Open(bashRCFileName) - if err != nil { - return "", err - } - defer rf.Close() - wf, err := ioutil.TempFile("/tmp", "bashrc-") - if err != nil { - return "", err - } - defer wf.Close() - - r := bufio.NewReader(rf) - prefix := []byte{} - for { - line, isPrefix, err := r.ReadLine() - if err == io.EOF { - break - } - if err != nil { - return "", err - } - if isPrefix { - prefix = append(prefix, line...) - continue - } - line = append(prefix, line...) - str := string(line) - if str == completeCmd { - continue - } - wf.WriteString(str + "\n") - prefix = prefix[:0] - } - return wf.Name(), nil -} - -func copyFile(src string, dst string) error { - in, err := os.Open(src) - if err != nil { - return err - } - defer in.Close() - out, err := os.Create(dst) - if err != nil { - return err - } - defer out.Close() - _, err = io.Copy(out, in) - return err -} diff --git a/install/install.go b/install/install.go deleted file mode 100644 index cef11f0..0000000 --- a/install/install.go +++ /dev/null @@ -1,43 +0,0 @@ -package install - -import ( - "os" - "path/filepath" -) - -type installer interface { - Install(cmd, bin string) error - Uninstall(cmd, bin string) error -} - -func Install(cmd string, asRoot bool) error { - bin, err := getBinaryPath() - if err != nil { - return err - } - return getInstaller(asRoot).Install(cmd, bin) -} - -func Uninstall(cmd string, asRoot bool) error { - bin, err := getBinaryPath() - if err != nil { - return err - } - return getInstaller(asRoot).Uninstall(cmd, bin) -} - -func getInstaller(asRoot bool) installer { - if asRoot { - return root{} - } else { - return home{} - } -} - -func getBinaryPath() (string, error) { - bin, err := os.Executable() - if err != nil { - return "", err - } - return filepath.Abs(bin) -} diff --git a/install/root.go b/install/root.go deleted file mode 100644 index 66e23b3..0000000 --- a/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 -} |
