From ddaf561db38590cb729e3ee296fa18bbd7f798e6 Mon Sep 17 00:00:00 2001 From: Eyal Posener Date: Sat, 23 Nov 2019 12:14:56 +0200 Subject: [PATCH] Install: use script library --- go.mod | 1 + go.sum | 6 ++ install/bash.go | 2 +- install/utils.go | 144 ++++++++--------------------------------------- install/zsh.go | 2 +- 5 files changed, 33 insertions(+), 122 deletions(-) diff --git a/go.mod b/go.mod index 914444c..d356498 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,7 @@ module github.com/posener/complete/v2 require ( github.com/hashicorp/go-multierror v1.0.0 + github.com/posener/script v1.0.1 github.com/stretchr/testify v1.4.0 ) diff --git a/go.sum b/go.sum index accaa27..c089edc 100644 --- a/go.sum +++ b/go.sum @@ -4,8 +4,14 @@ github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/U github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o= github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/posener/script v1.0.0 h1:/mJqplLNbKgJBeOElS00vDU9CG5QaN+qwp/o+lQAoDc= +github.com/posener/script v1.0.0/go.mod h1:Rg3ijooqulo05aGLyGsHoLmIOUzHUVK19WVgrYBPU/E= +github.com/posener/script v1.0.1 h1:MIYijfAGNnzPcH9zTFi+s8iiCgSfv90uTM5QnaSbajA= +github.com/posener/script v1.0.1/go.mod h1:Rg3ijooqulo05aGLyGsHoLmIOUzHUVK19WVgrYBPU/E= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= diff --git a/install/bash.go b/install/bash.go index 17c64de..3e85d28 100644 --- a/install/bash.go +++ b/install/bash.go @@ -20,7 +20,7 @@ func (b bash) Install(cmd, bin string) error { return fmt.Errorf("already installed in %s", b.rc) } completeCmd := b.cmd(cmd, bin) - return appendToFile(b.rc, completeCmd) + return appendFile(b.rc, completeCmd) } func (b bash) Uninstall(cmd, bin string) error { diff --git a/install/utils.go b/install/utils.go index d34ac8c..6748613 100644 --- a/install/utils.go +++ b/install/utils.go @@ -1,140 +1,44 @@ package install import ( - "bufio" "fmt" - "io" - "io/ioutil" "os" - "path/filepath" + "regexp" + + "github.com/posener/script" ) -func lineInFile(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] - } +func lineInFile(path string, line string) bool { + return script.Cat(path).Grep(regexp.MustCompile("^"+line+"$")).Wc().Lines > 0 } -func createFile(name string, content string) error { - // make sure file directory exists - if err := os.MkdirAll(filepath.Dir(name), 0775); err != nil { - return err - } - - // create the file - f, err := os.Create(name) - if err != nil { - return err - } - defer f.Close() - - // write file content - _, err = f.WriteString(fmt.Sprintf("%s\n", content)) - return err +func createFile(path string, content string) error { + return script.Echo(content).ToFile(path) } -func appendToFile(name string, content string) error { - f, err := os.OpenFile(name, os.O_RDWR|os.O_APPEND, 0) - if err != nil { - return err - } - defer f.Close() - _, err = f.WriteString(fmt.Sprintf("\n%s\n", content)) - return err +func appendFile(path string, content string) error { + return script.Echo(content).AppendFile(path) } -func removeFromFile(name string, content string) error { - backup := name + ".bck" - err := copyFile(name, backup) +func removeFromFile(path string, line string) error { + backupPath := path + ".bck" + err := script.Cat(path).ToFile(backupPath) if err != nil { - return err - } - temp, err := removeContentToTempFile(name, content) - if err != nil { - return err + return fmt.Errorf("creating backup file: %s", err) } - err = copyFile(temp, name) + tmp, err := script.Cat(path).Modify(script.Grep{Re: regexp.MustCompile("^" + line + "$"), Invert: true}).ToTempFile() if err != nil { - return err + return fmt.Errorf("failed remove: %s", err) } + defer os.Remove(tmp) - return os.Remove(backup) -} - -func removeContentToTempFile(name, content string) (string, error) { - rf, err := os.Open(name) - if err != nil { - return "", err - } - defer rf.Close() - wf, err := ioutil.TempFile("/tmp", "complete-") - 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 == content { - continue - } - _, err = wf.WriteString(str + "\n") - if err != nil { - return "", err - } - 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 + err = script.Cat(tmp).ToFile(path) + if err != nil { + restoreErr := script.Cat(backupPath).ToFile(path) + if restoreErr != nil { + return fmt.Errorf("failed write: %s, and failed restore: %s", err, restoreErr) + } + } + return nil } diff --git a/install/zsh.go b/install/zsh.go index 29950ab..cc00c88 100644 --- a/install/zsh.go +++ b/install/zsh.go @@ -27,7 +27,7 @@ func (z zsh) Install(cmd, bin string) error { completeCmd = bashCompInit + "\n" + completeCmd } - return appendToFile(z.rc, completeCmd) + return appendFile(z.rc, completeCmd) } func (z zsh) Uninstall(cmd, bin string) error {