Use the safe writer on existing commands

This commit is contained in:
sam boyer 2016-12-30 00:53:50 -05:00 коммит произвёл Jess Frazelle
Родитель ea0f0f03d7
Коммит 7bec4bd58f
3 изменённых файлов: 28 добавлений и 63 удалений

Просмотреть файл

@ -201,56 +201,17 @@ func (cmd *ensureCommand) Run(args []string) error {
return errors.Wrap(err, "ensure Solve()")
}
p.l.P = solution.Projects()
p.l.Memo = solution.InputHash()
tv, err := ioutil.TempDir("", "vendor")
if err != nil {
return errors.Wrap(err, "ensure making temporary vendor")
}
defer os.RemoveAll(tv)
tm, err := ioutil.TempFile("", "manifest")
if err != nil {
return errors.Wrap(err, "ensure making temporary manifest")
}
tm.Close()
defer os.Remove(tm.Name())
tl, err := ioutil.TempFile("", "lock")
if err != nil {
return errors.Wrap(err, "ensure making temporary lock file")
}
tl.Close()
defer os.Remove(tl.Name())
if err := gps.WriteDepTree(tv, p.l, sm, true); err != nil {
return errors.Wrap(err, "ensure gps.WriteDepTree")
sw := safeWriter{
root: p.absroot,
m: p.m,
l: p.l,
nl: solution,
sm: sm,
}
if err := writeFile(tm.Name(), p.m); err != nil {
return errors.Wrap(err, "ensure writeFile for manifest")
if err := sw.writeAllSafe(false); err != nil {
return errors.Wrap(err, "grouped write of manifest, lock and vendor")
}
if err := writeFile(tl.Name(), p.l); err != nil {
return errors.Wrap(err, "ensure writeFile for lock")
}
if err := copyFile(tm.Name(), filepath.Join(p.absroot, manifestName)); err != nil {
return errors.Wrap(err, "ensure moving temp manifest into place!")
}
os.Remove(tm.Name())
if err := copyFile(tl.Name(), filepath.Join(p.absroot, lockName)); err != nil {
return errors.Wrap(err, "ensure moving temp manifest into place!")
}
os.Remove(tl.Name())
os.RemoveAll(filepath.Join(p.absroot, "vendor"))
if err := copyFolder(tv, filepath.Join(p.absroot, "vendor")); err != nil {
return errors.Wrap(err, "ensure moving temp vendor")
}
return nil
}

20
init.go
Просмотреть файл

@ -124,7 +124,12 @@ func (cmd *initCommand) Run(args []string) error {
)
}
var l2 *lock
sw := safeWriter{
root: root,
sm: sm,
m: &m,
}
if len(pd.notondisk) > 0 {
vlogf("Solving...")
params := gps.SolveParameters{
@ -148,19 +153,16 @@ func (cmd *initCommand) Run(args []string) error {
handleAllTheFailuresOfTheWorld(err)
return err
}
l2 = lockFromInterface(soln)
sw.l = lockFromInterface(soln)
} else {
l2 = &l
sw.l = &l
}
vlogf("Writing manifest and lock files.")
if err := writeFile(mf, &m); err != nil {
return errors.Wrap(err, "writeFile for manifest")
}
if err := writeFile(lf, l2); err != nil {
return errors.Wrap(err, "writeFile for lock")
}
if err := sw.writeAllSafe(false); err != nil {
return errors.Wrap(err, "safe write of manifest and lock")
}
return nil
}

Просмотреть файл

@ -9,7 +9,6 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/pkg/errors"
"github.com/sdboyer/gps"
@ -107,13 +106,16 @@ func (cmd *removeCommand) Run(args []string) error {
return err
}
p.l = lockFromInterface(soln)
if err := writeFile(filepath.Join(p.absroot, manifestName), p.m); err != nil {
return errors.Wrap(err, "writeFile for manifest")
sw := safeWriter{
root: p.absroot,
m: p.m,
l: p.l,
nl: soln,
sm: sm,
}
if err := writeFile(filepath.Join(p.absroot, lockName), p.l); err != nil {
return errors.Wrap(err, "writeFile for lock")
if err := sw.writeAllSafe(false); err != nil {
return errors.Wrap(err, "grouped write of manifest, lock and vendor")
}
return nil
}