2016-12-23 17:49:24 +03:00
|
|
|
// Copyright 2016 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
package dep
|
2016-12-23 17:49:24 +03:00
|
|
|
|
|
|
|
import (
|
2017-03-01 23:52:09 +03:00
|
|
|
"bytes"
|
2016-12-23 17:49:24 +03:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2017-05-06 04:32:39 +03:00
|
|
|
"log"
|
2016-12-23 17:49:24 +03:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2017-11-03 16:05:41 +03:00
|
|
|
"github.com/golang/dep/gps"
|
2017-05-10 19:21:00 +03:00
|
|
|
"github.com/golang/dep/internal/fs"
|
2017-03-23 01:11:43 +03:00
|
|
|
"github.com/pelletier/go-toml"
|
2016-12-23 18:34:20 +03:00
|
|
|
"github.com/pkg/errors"
|
2016-12-23 17:49:24 +03:00
|
|
|
)
|
|
|
|
|
2017-04-13 15:30:07 +03:00
|
|
|
// Example string to be written to the manifest file
|
|
|
|
// if no dependencies are found in the project
|
|
|
|
// during `dep init`
|
2017-11-21 19:35:19 +03:00
|
|
|
var exampleTOML = []byte(`# Gopkg.toml example
|
2017-04-28 06:51:57 +03:00
|
|
|
#
|
2017-06-15 04:12:07 +03:00
|
|
|
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
|
|
|
|
# for detailed Gopkg.toml documentation.
|
2017-04-28 05:04:39 +03:00
|
|
|
#
|
2017-06-15 04:12:07 +03:00
|
|
|
# required = ["github.com/user/thing/cmd/thing"]
|
|
|
|
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
|
2017-05-25 11:07:40 +03:00
|
|
|
#
|
2017-06-15 04:12:07 +03:00
|
|
|
# [[constraint]]
|
|
|
|
# name = "github.com/user/project"
|
|
|
|
# version = "1.0.0"
|
2017-04-28 05:04:39 +03:00
|
|
|
#
|
2017-06-15 04:12:07 +03:00
|
|
|
# [[constraint]]
|
|
|
|
# name = "github.com/user/project2"
|
|
|
|
# branch = "dev"
|
|
|
|
# source = "github.com/myfork/project2"
|
2017-04-28 06:51:57 +03:00
|
|
|
#
|
2017-05-10 07:07:50 +03:00
|
|
|
# [[override]]
|
2017-12-18 00:58:32 +03:00
|
|
|
# name = "github.com/x/y"
|
|
|
|
# version = "2.4.0"
|
|
|
|
#
|
|
|
|
# [prune]
|
|
|
|
# non-go = false
|
|
|
|
# go-tests = true
|
|
|
|
# unused-packages = true
|
2017-04-28 05:04:39 +03:00
|
|
|
|
2017-05-21 13:39:23 +03:00
|
|
|
`)
|
|
|
|
|
|
|
|
// String added on top of lock file
|
|
|
|
var lockFileComment = []byte(`# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
|
|
|
|
|
|
|
|
`)
|
2017-04-13 15:30:07 +03:00
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
// SafeWriter transactionalizes writes of manifest, lock, and vendor dir, both
|
2016-12-23 17:49:24 +03:00
|
|
|
// individually and in any combination, into a pseudo-atomic action with
|
|
|
|
// transactional rollback.
|
|
|
|
//
|
|
|
|
// It is not impervious to errors (writing to disk is hard), but it should
|
|
|
|
// guard against non-arcane failure conditions.
|
2017-01-27 23:39:07 +03:00
|
|
|
type SafeWriter struct {
|
2017-12-05 17:52:43 +03:00
|
|
|
Manifest *Manifest
|
|
|
|
lock *Lock
|
|
|
|
lockDiff *gps.LockDiff
|
|
|
|
writeVendor bool
|
|
|
|
writeLock bool
|
2018-01-24 23:44:08 +03:00
|
|
|
pruneOptions gps.CascadingPruneOptions
|
2017-03-09 22:28:42 +03:00
|
|
|
}
|
|
|
|
|
2017-07-20 06:28:48 +03:00
|
|
|
// NewSafeWriter sets up a SafeWriter to write a set of manifest, lock, and
|
|
|
|
// vendor tree.
|
2017-05-01 00:35:43 +03:00
|
|
|
//
|
|
|
|
// - If manifest is provided, it will be written to the standard manifest file
|
2017-06-02 16:51:17 +03:00
|
|
|
// name beneath root.
|
|
|
|
//
|
2017-05-01 00:35:43 +03:00
|
|
|
// - If newLock is provided, it will be written to the standard lock file
|
2017-06-02 16:51:17 +03:00
|
|
|
// name beneath root.
|
|
|
|
//
|
2017-05-01 00:35:43 +03:00
|
|
|
// - If vendor is VendorAlways, or is VendorOnChanged and the locks are different,
|
2017-06-02 16:51:17 +03:00
|
|
|
// the vendor directory will be written beneath root based on newLock.
|
|
|
|
//
|
2017-05-01 00:35:43 +03:00
|
|
|
// - If oldLock is provided without newLock, error.
|
2017-06-02 16:51:17 +03:00
|
|
|
//
|
2017-05-01 00:35:43 +03:00
|
|
|
// - If vendor is VendorAlways without a newLock, error.
|
2018-01-24 23:44:08 +03:00
|
|
|
func NewSafeWriter(manifest *Manifest, oldLock, newLock *Lock, vendor VendorBehavior, prune gps.CascadingPruneOptions) (*SafeWriter, error) {
|
2017-05-01 14:34:38 +03:00
|
|
|
sw := &SafeWriter{
|
2017-12-05 17:52:43 +03:00
|
|
|
Manifest: manifest,
|
|
|
|
lock: newLock,
|
|
|
|
pruneOptions: prune,
|
2017-05-01 00:35:43 +03:00
|
|
|
}
|
2017-06-17 06:27:02 +03:00
|
|
|
|
2017-05-01 00:35:43 +03:00
|
|
|
if oldLock != nil {
|
|
|
|
if newLock == nil {
|
|
|
|
return nil, errors.New("must provide newLock when oldLock is specified")
|
|
|
|
}
|
2017-06-17 06:27:02 +03:00
|
|
|
|
2017-05-17 03:41:19 +03:00
|
|
|
sw.lockDiff = gps.DiffLocks(oldLock, newLock)
|
2017-06-17 06:27:02 +03:00
|
|
|
if sw.lockDiff != nil {
|
|
|
|
sw.writeLock = true
|
|
|
|
}
|
|
|
|
} else if newLock != nil {
|
|
|
|
sw.writeLock = true
|
2017-05-01 00:35:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch vendor {
|
|
|
|
case VendorAlways:
|
2017-05-17 02:06:54 +03:00
|
|
|
sw.writeVendor = true
|
2017-05-01 00:35:43 +03:00
|
|
|
case VendorOnChanged:
|
2017-06-17 06:27:02 +03:00
|
|
|
sw.writeVendor = sw.lockDiff != nil || (newLock != nil && oldLock == nil)
|
2017-05-01 00:35:43 +03:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:06:54 +03:00
|
|
|
if sw.writeVendor && newLock == nil {
|
2017-05-01 00:35:43 +03:00
|
|
|
return nil, errors.New("must provide newLock in order to write out vendor")
|
|
|
|
}
|
|
|
|
|
|
|
|
return sw, nil
|
|
|
|
}
|
|
|
|
|
2017-05-01 14:34:38 +03:00
|
|
|
// HasLock checks if a Lock is present in the SafeWriter
|
|
|
|
func (sw *SafeWriter) HasLock() bool {
|
2017-05-17 01:53:20 +03:00
|
|
|
return sw.lock != nil
|
2017-03-09 22:28:42 +03:00
|
|
|
}
|
|
|
|
|
2017-05-01 14:34:38 +03:00
|
|
|
// HasManifest checks if a Manifest is present in the SafeWriter
|
|
|
|
func (sw *SafeWriter) HasManifest() bool {
|
|
|
|
return sw.Manifest != nil
|
2017-03-09 22:28:42 +03:00
|
|
|
}
|
|
|
|
|
2017-04-11 18:38:50 +03:00
|
|
|
type rawStringDiff struct {
|
|
|
|
*gps.StringDiff
|
|
|
|
}
|
|
|
|
|
2017-06-01 03:21:52 +03:00
|
|
|
// MarshalTOML serializes the diff as a string.
|
2017-04-11 18:38:50 +03:00
|
|
|
func (diff rawStringDiff) MarshalTOML() ([]byte, error) {
|
|
|
|
return []byte(diff.String()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type rawLockedProjectDiff struct {
|
|
|
|
Name gps.ProjectRoot `toml:"name"`
|
|
|
|
Source *rawStringDiff `toml:"source,omitempty"`
|
|
|
|
Version *rawStringDiff `toml:"version,omitempty"`
|
|
|
|
Branch *rawStringDiff `toml:"branch,omitempty"`
|
|
|
|
Revision *rawStringDiff `toml:"revision,omitempty"`
|
|
|
|
Packages []rawStringDiff `toml:"packages,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func toRawLockedProjectDiff(diff gps.LockedProjectDiff) rawLockedProjectDiff {
|
|
|
|
// this is a shallow copy since we aren't modifying the raw diff
|
|
|
|
raw := rawLockedProjectDiff{Name: diff.Name}
|
|
|
|
if diff.Source != nil {
|
|
|
|
raw.Source = &rawStringDiff{diff.Source}
|
|
|
|
}
|
|
|
|
if diff.Version != nil {
|
|
|
|
raw.Version = &rawStringDiff{diff.Version}
|
|
|
|
}
|
|
|
|
if diff.Branch != nil {
|
|
|
|
raw.Branch = &rawStringDiff{diff.Branch}
|
|
|
|
}
|
|
|
|
if diff.Revision != nil {
|
|
|
|
raw.Revision = &rawStringDiff{diff.Revision}
|
|
|
|
}
|
|
|
|
raw.Packages = make([]rawStringDiff, len(diff.Packages))
|
|
|
|
for i := 0; i < len(diff.Packages); i++ {
|
|
|
|
raw.Packages[i] = rawStringDiff{&diff.Packages[i]}
|
|
|
|
}
|
|
|
|
return raw
|
2017-03-01 23:52:09 +03:00
|
|
|
}
|
|
|
|
|
2017-04-05 04:47:09 +03:00
|
|
|
type rawLockedProjectDiffs struct {
|
2017-04-11 18:38:50 +03:00
|
|
|
Projects []rawLockedProjectDiff `toml:"projects"`
|
2017-04-05 04:47:09 +03:00
|
|
|
}
|
|
|
|
|
2017-04-11 18:38:50 +03:00
|
|
|
func toRawLockedProjectDiffs(diffs []gps.LockedProjectDiff) rawLockedProjectDiffs {
|
|
|
|
raw := rawLockedProjectDiffs{
|
|
|
|
Projects: make([]rawLockedProjectDiff, len(diffs)),
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(diffs); i++ {
|
|
|
|
raw.Projects[i] = toRawLockedProjectDiff(diffs[i])
|
2017-03-01 23:52:09 +03:00
|
|
|
}
|
|
|
|
|
2017-04-11 18:38:50 +03:00
|
|
|
return raw
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatLockDiff(diff gps.LockDiff) (string, error) {
|
2017-03-01 23:52:09 +03:00
|
|
|
var buf bytes.Buffer
|
|
|
|
|
2017-03-18 00:48:57 +03:00
|
|
|
if diff.HashDiff != nil {
|
2017-03-23 01:11:43 +03:00
|
|
|
buf.WriteString(fmt.Sprintf("Memo: %s\n\n", diff.HashDiff))
|
2017-03-18 00:48:57 +03:00
|
|
|
}
|
|
|
|
|
2017-04-11 18:38:50 +03:00
|
|
|
writeDiffs := func(diffs []gps.LockedProjectDiff) error {
|
|
|
|
raw := toRawLockedProjectDiffs(diffs)
|
2017-04-05 04:47:09 +03:00
|
|
|
chunk, err := toml.Marshal(raw)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-03-23 01:11:43 +03:00
|
|
|
}
|
2017-04-05 04:47:09 +03:00
|
|
|
buf.Write(chunk)
|
2017-03-23 01:11:43 +03:00
|
|
|
buf.WriteString("\n")
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-01 23:52:09 +03:00
|
|
|
|
2017-03-23 01:11:43 +03:00
|
|
|
if len(diff.Add) > 0 {
|
|
|
|
buf.WriteString("Add:")
|
|
|
|
err := writeDiffs(diff.Add)
|
2017-03-01 23:52:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "Unable to format LockDiff.Add")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(diff.Remove) > 0 {
|
2017-03-23 01:11:43 +03:00
|
|
|
buf.WriteString("Remove:")
|
|
|
|
err := writeDiffs(diff.Remove)
|
2017-03-01 23:52:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "Unable to format LockDiff.Remove")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(diff.Modify) > 0 {
|
2017-03-23 01:11:43 +03:00
|
|
|
buf.WriteString("Modify:")
|
|
|
|
err := writeDiffs(diff.Modify)
|
2017-03-01 23:52:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "Unable to format LockDiff.Modify")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String(), nil
|
2017-03-09 22:28:42 +03:00
|
|
|
}
|
|
|
|
|
2017-03-18 04:53:00 +03:00
|
|
|
// VendorBehavior defines when the vendor directory should be written.
|
|
|
|
type VendorBehavior int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// VendorOnChanged indicates that the vendor directory should be written when the lock is new or changed.
|
|
|
|
VendorOnChanged VendorBehavior = iota
|
|
|
|
// VendorAlways forces the vendor directory to always be written.
|
|
|
|
VendorAlways
|
|
|
|
// VendorNever indicates the vendor directory should never be written.
|
|
|
|
VendorNever
|
|
|
|
)
|
|
|
|
|
2017-05-01 14:34:38 +03:00
|
|
|
func (sw SafeWriter) validate(root string, sm gps.SourceManager) error {
|
2017-03-09 22:28:42 +03:00
|
|
|
if root == "" {
|
2016-12-30 06:15:23 +03:00
|
|
|
return errors.New("root path must be non-empty")
|
|
|
|
}
|
2017-05-10 19:21:00 +03:00
|
|
|
if is, err := fs.IsDir(root); !is {
|
2017-06-18 16:16:02 +03:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2016-12-30 06:15:23 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-03-22 18:58:50 +03:00
|
|
|
return errors.Errorf("root path %q does not exist", root)
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
|
2017-05-17 02:06:54 +03:00
|
|
|
if sw.writeVendor && sm == nil {
|
2017-01-25 00:48:14 +03:00
|
|
|
return errors.New("must provide a SourceManager if writing out a vendor dir")
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
|
2017-03-09 22:28:42 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write saves some combination of config yaml, lock, and a vendor tree.
|
|
|
|
// root is the absolute path of root dir in which to write.
|
|
|
|
// sm is only required if vendor is being written.
|
|
|
|
//
|
|
|
|
// It first writes to a temp dir, then moves them in place if and only if all the write
|
|
|
|
// operations succeeded. It also does its best to roll back if any moves fail.
|
|
|
|
// This mostly guarantees that dep cannot exit with a partial write that would
|
|
|
|
// leave an undefined state on disk.
|
2017-08-13 04:52:47 +03:00
|
|
|
func (sw *SafeWriter) Write(root string, sm gps.SourceManager, examples bool, logger *log.Logger) error {
|
2017-05-01 14:34:38 +03:00
|
|
|
err := sw.validate(root, sm)
|
2017-03-09 22:28:42 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-17 06:27:02 +03:00
|
|
|
if !sw.HasManifest() && !sw.writeLock && !sw.writeVendor {
|
2017-03-09 22:28:42 +03:00
|
|
|
// nothing to do
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mpath := filepath.Join(root, ManifestName)
|
|
|
|
lpath := filepath.Join(root, LockName)
|
|
|
|
vpath := filepath.Join(root, "vendor")
|
2016-12-30 06:56:11 +03:00
|
|
|
|
2017-01-19 03:11:38 +03:00
|
|
|
td, err := ioutil.TempDir(os.TempDir(), "dep")
|
2016-12-23 17:49:24 +03:00
|
|
|
if err != nil {
|
2016-12-23 18:34:20 +03:00
|
|
|
return errors.Wrap(err, "error while creating temp dir for writing manifest/lock/vendor")
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
2017-05-01 14:34:38 +03:00
|
|
|
if sw.HasManifest() {
|
2017-04-26 07:47:31 +03:00
|
|
|
// Always write the example text to the bottom of the TOML file.
|
2017-05-01 14:34:38 +03:00
|
|
|
tb, err := sw.Manifest.MarshalTOML()
|
2017-04-26 07:47:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to marshal manifest to TOML")
|
|
|
|
}
|
|
|
|
|
2017-05-21 13:39:23 +03:00
|
|
|
var initOutput []byte
|
2017-04-28 21:13:52 +03:00
|
|
|
|
2017-06-02 16:24:21 +03:00
|
|
|
// If examples are enabled, use the example text
|
|
|
|
if examples {
|
2017-04-28 21:13:52 +03:00
|
|
|
initOutput = exampleTOML
|
|
|
|
}
|
|
|
|
|
2017-05-21 13:39:23 +03:00
|
|
|
if err = ioutil.WriteFile(filepath.Join(td, ManifestName), append(initOutput, tb...), 0666); err != nil {
|
2016-12-23 18:34:20 +03:00
|
|
|
return errors.Wrap(err, "failed to write manifest file to temp dir")
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-17 06:27:02 +03:00
|
|
|
if sw.writeLock {
|
2017-05-21 13:39:23 +03:00
|
|
|
l, err := sw.lock.MarshalTOML()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to marshal lock to TOML")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = ioutil.WriteFile(filepath.Join(td, LockName), append(lockFileComment, l...), 0666); err != nil {
|
2017-03-09 22:28:42 +03:00
|
|
|
return errors.Wrap(err, "failed to write lock file to temp dir")
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 02:06:54 +03:00
|
|
|
if sw.writeVendor {
|
2017-12-05 17:52:43 +03:00
|
|
|
err = gps.WriteDepTree(filepath.Join(td, "vendor"), sw.lock, sm, sw.pruneOptions, logger)
|
2016-12-23 17:49:24 +03:00
|
|
|
if err != nil {
|
2016-12-30 06:15:23 +03:00
|
|
|
return errors.Wrap(err, "error while writing out vendor tree")
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-26 15:21:11 +03:00
|
|
|
// Ensure vendor/.git is preserved if present
|
|
|
|
if hasDotGit(vpath) {
|
2017-05-10 19:21:00 +03:00
|
|
|
err = fs.RenameWithFallback(filepath.Join(vpath, ".git"), filepath.Join(td, "vendor/.git"))
|
2017-04-26 15:21:11 +03:00
|
|
|
if _, ok := err.(*os.LinkError); ok {
|
|
|
|
return errors.Wrap(err, "failed to preserve vendor/.git")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-23 17:49:24 +03:00
|
|
|
// Move the existing files and dirs to the temp dir while we put the new
|
2017-01-25 10:29:04 +03:00
|
|
|
// ones in, to provide insurance against errors for as long as possible.
|
2016-12-23 17:49:24 +03:00
|
|
|
type pathpair struct {
|
|
|
|
from, to string
|
|
|
|
}
|
|
|
|
var restore []pathpair
|
2016-12-30 06:56:11 +03:00
|
|
|
var failerr error
|
|
|
|
var vendorbak string
|
2016-12-23 17:49:24 +03:00
|
|
|
|
2017-05-01 14:34:38 +03:00
|
|
|
if sw.HasManifest() {
|
2016-12-30 06:15:23 +03:00
|
|
|
if _, err := os.Stat(mpath); err == nil {
|
2017-01-25 10:29:04 +03:00
|
|
|
// Move out the old one.
|
2017-01-27 23:39:07 +03:00
|
|
|
tmploc := filepath.Join(td, ManifestName+".orig")
|
2017-05-10 19:21:00 +03:00
|
|
|
failerr = fs.RenameWithFallback(mpath, tmploc)
|
2016-12-23 17:49:24 +03:00
|
|
|
if failerr != nil {
|
2016-12-30 06:56:11 +03:00
|
|
|
goto fail
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
2016-12-30 06:56:11 +03:00
|
|
|
restore = append(restore, pathpair{from: tmploc, to: mpath})
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
|
2017-01-25 10:29:04 +03:00
|
|
|
// Move in the new one.
|
2017-05-10 19:21:00 +03:00
|
|
|
failerr = fs.RenameWithFallback(filepath.Join(td, ManifestName), mpath)
|
2016-12-23 17:49:24 +03:00
|
|
|
if failerr != nil {
|
2016-12-30 06:56:11 +03:00
|
|
|
goto fail
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-17 06:27:02 +03:00
|
|
|
if sw.writeLock {
|
2016-12-30 06:15:23 +03:00
|
|
|
if _, err := os.Stat(lpath); err == nil {
|
2017-01-25 10:29:04 +03:00
|
|
|
// Move out the old one.
|
2017-01-27 23:39:07 +03:00
|
|
|
tmploc := filepath.Join(td, LockName+".orig")
|
2016-12-23 17:49:24 +03:00
|
|
|
|
2017-05-10 19:21:00 +03:00
|
|
|
failerr = fs.RenameWithFallback(lpath, tmploc)
|
2016-12-23 17:49:24 +03:00
|
|
|
if failerr != nil {
|
2016-12-30 06:56:11 +03:00
|
|
|
goto fail
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
2016-12-30 06:56:11 +03:00
|
|
|
restore = append(restore, pathpair{from: tmploc, to: lpath})
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
|
2017-01-25 10:29:04 +03:00
|
|
|
// Move in the new one.
|
2017-05-10 19:21:00 +03:00
|
|
|
failerr = fs.RenameWithFallback(filepath.Join(td, LockName), lpath)
|
2016-12-23 17:49:24 +03:00
|
|
|
if failerr != nil {
|
2016-12-30 06:56:11 +03:00
|
|
|
goto fail
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 02:06:54 +03:00
|
|
|
if sw.writeVendor {
|
2016-12-30 06:15:23 +03:00
|
|
|
if _, err := os.Stat(vpath); err == nil {
|
2017-01-25 10:29:04 +03:00
|
|
|
// Move out the old vendor dir. just do it into an adjacent dir, to
|
2016-12-23 18:34:20 +03:00
|
|
|
// try to mitigate the possibility of a pointless cross-filesystem
|
2017-01-25 10:29:04 +03:00
|
|
|
// move with a temp directory.
|
2016-12-30 06:15:23 +03:00
|
|
|
vendorbak = vpath + ".orig"
|
2016-12-23 17:49:24 +03:00
|
|
|
if _, err := os.Stat(vendorbak); err == nil {
|
2016-12-30 06:56:11 +03:00
|
|
|
// If the adjacent dir already exists, bite the bullet and move
|
2017-01-25 10:29:04 +03:00
|
|
|
// to a proper tempdir.
|
2017-10-24 16:32:50 +03:00
|
|
|
vendorbak = filepath.Join(td, ".vendor.orig")
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
2016-12-23 18:34:20 +03:00
|
|
|
|
2017-05-10 19:21:00 +03:00
|
|
|
failerr = fs.RenameWithFallback(vpath, vendorbak)
|
2016-12-23 17:49:24 +03:00
|
|
|
if failerr != nil {
|
2016-12-30 06:56:11 +03:00
|
|
|
goto fail
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
2016-12-30 06:56:11 +03:00
|
|
|
restore = append(restore, pathpair{from: vendorbak, to: vpath})
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
|
2017-01-25 10:29:04 +03:00
|
|
|
// Move in the new one.
|
2017-05-10 19:21:00 +03:00
|
|
|
failerr = fs.RenameWithFallback(filepath.Join(td, "vendor"), vpath)
|
2016-12-23 17:49:24 +03:00
|
|
|
if failerr != nil {
|
2016-12-30 06:56:11 +03:00
|
|
|
goto fail
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renames all went smoothly. The deferred os.RemoveAll will get the temp
|
|
|
|
// dir, but if we wrote vendor, we have to clean that up directly
|
2017-05-17 02:06:54 +03:00
|
|
|
if sw.writeVendor {
|
2016-12-30 06:56:11 +03:00
|
|
|
// Nothing we can really do about an error at this point, so ignore it
|
2016-12-23 17:49:24 +03:00
|
|
|
os.RemoveAll(vendorbak)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-12-30 06:56:11 +03:00
|
|
|
|
|
|
|
fail:
|
2017-01-25 10:29:04 +03:00
|
|
|
// If we failed at any point, move all the things back into place, then bail.
|
2016-12-30 06:56:11 +03:00
|
|
|
for _, pair := range restore {
|
2017-01-25 10:29:04 +03:00
|
|
|
// Nothing we can do on err here, as we're already in recovery mode.
|
2017-05-10 19:21:00 +03:00
|
|
|
fs.RenameWithFallback(pair.from, pair.to)
|
2016-12-30 06:56:11 +03:00
|
|
|
}
|
|
|
|
return failerr
|
2016-12-23 17:49:24 +03:00
|
|
|
}
|
2017-03-09 22:28:42 +03:00
|
|
|
|
2017-06-01 03:21:52 +03:00
|
|
|
// PrintPreparedActions logs the actions a call to Write would perform.
|
2017-08-21 09:53:49 +03:00
|
|
|
func (sw *SafeWriter) PrintPreparedActions(output *log.Logger, verbose bool) error {
|
2017-05-01 14:34:38 +03:00
|
|
|
if sw.HasManifest() {
|
2017-08-21 09:53:49 +03:00
|
|
|
if verbose {
|
|
|
|
m, err := sw.Manifest.MarshalTOML()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "ensure DryRun cannot serialize manifest")
|
|
|
|
}
|
|
|
|
output.Printf("Would have written the following %s:\n%s\n", ManifestName, string(m))
|
|
|
|
} else {
|
|
|
|
output.Printf("Would have written %s.\n", ManifestName)
|
2017-03-09 22:28:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-17 06:27:02 +03:00
|
|
|
if sw.writeLock {
|
2017-05-17 03:41:19 +03:00
|
|
|
if sw.lockDiff == nil {
|
2017-08-21 09:53:49 +03:00
|
|
|
if verbose {
|
|
|
|
l, err := sw.lock.MarshalTOML()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "ensure DryRun cannot serialize lock")
|
|
|
|
}
|
|
|
|
output.Printf("Would have written the following %s:\n%s\n", LockName, string(l))
|
|
|
|
} else {
|
|
|
|
output.Printf("Would have written %s.\n", LockName)
|
2017-03-15 18:06:07 +03:00
|
|
|
}
|
|
|
|
} else {
|
2017-05-09 16:30:19 +03:00
|
|
|
output.Printf("Would have written the following changes to %s:\n", LockName)
|
2017-05-17 03:41:19 +03:00
|
|
|
diff, err := formatLockDiff(*sw.lockDiff)
|
2017-03-15 18:06:07 +03:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "ensure DryRun cannot serialize the lock diff")
|
|
|
|
}
|
2017-05-09 16:30:19 +03:00
|
|
|
output.Println(diff)
|
2017-03-09 22:28:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 02:06:54 +03:00
|
|
|
if sw.writeVendor {
|
2017-08-21 09:53:49 +03:00
|
|
|
if verbose {
|
|
|
|
output.Printf("Would have written the following %d projects to the vendor directory:\n", len(sw.lock.Projects()))
|
2017-08-21 14:35:12 +03:00
|
|
|
lps := sw.lock.Projects()
|
|
|
|
for i, p := range lps {
|
|
|
|
output.Printf("(%d/%d) %s@%s\n", i+1, len(lps), p.Ident(), p.Version())
|
2017-08-21 09:53:49 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
output.Printf("Would have written %d projects to the vendor directory.\n", len(sw.lock.Projects()))
|
2017-03-09 22:28:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-01 23:52:09 +03:00
|
|
|
|
2017-04-26 15:21:11 +03:00
|
|
|
// hasDotGit checks if a given path has .git file or directory in it.
|
|
|
|
func hasDotGit(path string) bool {
|
|
|
|
gitfilepath := filepath.Join(path, ".git")
|
|
|
|
_, err := os.Stat(gitfilepath)
|
2017-04-26 22:19:09 +03:00
|
|
|
return err == nil
|
2017-04-26 15:21:11 +03:00
|
|
|
}
|