Install: use script library

This commit is contained in:
Eyal Posener 2019-11-23 12:14:56 +02:00
parent 027a352502
commit ddaf561db3
5 changed files with 33 additions and 122 deletions

1
go.mod
View File

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

6
go.sum
View File

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

View File

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

View File

@ -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
func createFile(path string, content string) error {
return script.Echo(content).ToFile(path)
}
// 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 appendFile(path string, content string) error {
return script.Echo(content).AppendFile(path)
}
func appendToFile(name string, content string) error {
f, err := os.OpenFile(name, os.O_RDWR|os.O_APPEND, 0)
func removeFromFile(path string, line string) error {
backupPath := path + ".bck"
err := script.Cat(path).ToFile(backupPath)
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(fmt.Sprintf("\n%s\n", content))
return err
return fmt.Errorf("creating backup file: %s", err)
}
func removeFromFile(name string, content string) error {
backup := name + ".bck"
err := copyFile(name, backup)
tmp, err := script.Cat(path).Modify(script.Grep{Re: regexp.MustCompile("^" + line + "$"), Invert: true}).ToTempFile()
if err != nil {
return err
}
temp, err := removeContentToTempFile(name, content)
if err != nil {
return err
return fmt.Errorf("failed remove: %s", err)
}
defer os.Remove(tmp)
err = copyFile(temp, name)
err = script.Cat(tmp).ToFile(path)
if err != nil {
return err
restoreErr := script.Cat(backupPath).ToFile(path)
if restoreErr != nil {
return fmt.Errorf("failed write: %s, and failed restore: %s", err, restoreErr)
}
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
return nil
}

View File

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