2019-11-18 21:28:26 -06:00
|
|
|
package install
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-11-23 04:14:56 -06:00
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"github.com/posener/script"
|
2019-11-18 21:28:26 -06:00
|
|
|
)
|
|
|
|
|
2019-11-23 04:14:56 -06:00
|
|
|
func lineInFile(path string, line string) bool {
|
|
|
|
return script.Cat(path).Grep(regexp.MustCompile("^"+line+"$")).Wc().Lines > 0
|
2019-11-18 21:28:26 -06:00
|
|
|
}
|
|
|
|
|
2019-11-23 04:14:56 -06:00
|
|
|
func createFile(path string, content string) error {
|
|
|
|
return script.Echo(content).ToFile(path)
|
2019-11-18 21:28:26 -06:00
|
|
|
}
|
|
|
|
|
2019-11-23 04:14:56 -06:00
|
|
|
func appendFile(path string, content string) error {
|
|
|
|
return script.Echo(content).AppendFile(path)
|
2019-11-18 21:28:26 -06:00
|
|
|
}
|
|
|
|
|
2019-11-23 04:14:56 -06:00
|
|
|
func removeFromFile(path string, line string) error {
|
|
|
|
backupPath := path + ".bck"
|
|
|
|
err := script.Cat(path).ToFile(backupPath)
|
2019-11-18 21:28:26 -06:00
|
|
|
if err != nil {
|
2019-11-23 04:14:56 -06:00
|
|
|
return fmt.Errorf("creating backup file: %s", err)
|
2019-11-18 21:28:26 -06:00
|
|
|
}
|
|
|
|
|
2019-11-23 16:42:07 -06:00
|
|
|
tmp, err := script.Cat(path).Modify(script.Grep{Re: regexp.MustCompile("^" + line + "$"), Inverse: true}).ToTempFile()
|
2019-11-18 21:28:26 -06:00
|
|
|
if err != nil {
|
2019-11-23 04:14:56 -06:00
|
|
|
return fmt.Errorf("failed remove: %s", err)
|
2019-11-18 21:28:26 -06:00
|
|
|
}
|
2019-11-23 04:14:56 -06:00
|
|
|
defer os.Remove(tmp)
|
2019-11-18 21:28:26 -06:00
|
|
|
|
2019-11-23 04:14:56 -06:00
|
|
|
err = script.Cat(tmp).ToFile(path)
|
2019-11-18 21:28:26 -06:00
|
|
|
if err != nil {
|
2019-11-23 04:14:56 -06:00
|
|
|
restoreErr := script.Cat(backupPath).ToFile(path)
|
|
|
|
if restoreErr != nil {
|
|
|
|
return fmt.Errorf("failed write: %s, and failed restore: %s", err, restoreErr)
|
2019-11-18 21:28:26 -06:00
|
|
|
}
|
|
|
|
}
|
2019-11-23 04:14:56 -06:00
|
|
|
return nil
|
2019-11-18 21:28:26 -06:00
|
|
|
}
|