Install: use script library
This commit is contained in:
parent
027a352502
commit
ddaf561db3
1
go.mod
1
go.mod
|
@ -2,6 +2,7 @@ module github.com/posener/complete/v2
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/hashicorp/go-multierror v1.0.0
|
github.com/hashicorp/go-multierror v1.0.0
|
||||||
|
github.com/posener/script v1.0.1
|
||||||
github.com/stretchr/testify v1.4.0
|
github.com/stretchr/testify v1.4.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
6
go.sum
6
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/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 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o=
|
||||||
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
|
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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
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/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 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
||||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||||
|
|
|
@ -20,7 +20,7 @@ func (b bash) Install(cmd, bin string) error {
|
||||||
return fmt.Errorf("already installed in %s", b.rc)
|
return fmt.Errorf("already installed in %s", b.rc)
|
||||||
}
|
}
|
||||||
completeCmd := b.cmd(cmd, bin)
|
completeCmd := b.cmd(cmd, bin)
|
||||||
return appendToFile(b.rc, completeCmd)
|
return appendFile(b.rc, completeCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b bash) Uninstall(cmd, bin string) error {
|
func (b bash) Uninstall(cmd, bin string) error {
|
||||||
|
|
138
install/utils.go
138
install/utils.go
|
@ -1,140 +1,44 @@
|
||||||
package install
|
package install
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"regexp"
|
||||||
|
|
||||||
|
"github.com/posener/script"
|
||||||
)
|
)
|
||||||
|
|
||||||
func lineInFile(name string, lookFor string) bool {
|
func lineInFile(path string, line string) bool {
|
||||||
f, err := os.Open(name)
|
return script.Cat(path).Grep(regexp.MustCompile("^"+line+"$")).Wc().Lines > 0
|
||||||
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 createFile(name string, content string) error {
|
func createFile(path string, content string) error {
|
||||||
// make sure file directory exists
|
return script.Echo(content).ToFile(path)
|
||||||
if err := os.MkdirAll(filepath.Dir(name), 0775); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// create the file
|
func appendFile(path string, content string) error {
|
||||||
f, err := os.Create(name)
|
return script.Echo(content).AppendFile(path)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
// write file content
|
|
||||||
_, err = f.WriteString(fmt.Sprintf("%s\n", content))
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func appendToFile(name string, content string) error {
|
func removeFromFile(path string, line string) error {
|
||||||
f, err := os.OpenFile(name, os.O_RDWR|os.O_APPEND, 0)
|
backupPath := path + ".bck"
|
||||||
|
err := script.Cat(path).ToFile(backupPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("creating backup file: %s", err)
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
_, err = f.WriteString(fmt.Sprintf("\n%s\n", content))
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeFromFile(name string, content string) error {
|
tmp, err := script.Cat(path).Modify(script.Grep{Re: regexp.MustCompile("^" + line + "$"), Invert: true}).ToTempFile()
|
||||||
backup := name + ".bck"
|
|
||||||
err := copyFile(name, backup)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed remove: %s", err)
|
||||||
}
|
|
||||||
temp, err := removeContentToTempFile(name, content)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
defer os.Remove(tmp)
|
||||||
|
|
||||||
err = copyFile(temp, name)
|
err = script.Cat(tmp).ToFile(path)
|
||||||
if err != nil {
|
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)
|
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ func (z zsh) Install(cmd, bin string) error {
|
||||||
completeCmd = bashCompInit + "\n" + completeCmd
|
completeCmd = bashCompInit + "\n" + completeCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
return appendToFile(z.rc, completeCmd)
|
return appendFile(z.rc, completeCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (z zsh) Uninstall(cmd, bin string) error {
|
func (z zsh) Uninstall(cmd, bin string) error {
|
||||||
|
|
Loading…
Reference in New Issue