diff --git a/cmd/dep/testdata/harness_tests/ensure/empty/case1/final/Gopkg.toml b/cmd/dep/testdata/harness_tests/ensure/empty/case1/final/Gopkg.toml index e69de29bb2..77028df6ae 100644 --- a/cmd/dep/testdata/harness_tests/ensure/empty/case1/final/Gopkg.toml +++ b/cmd/dep/testdata/harness_tests/ensure/empty/case1/final/Gopkg.toml @@ -0,0 +1,9 @@ + +# Example: +# [[dependencies]] +# source = "https://github.com/myfork/package.git" +# branch = "master" +# name = "github.com/vendor/package" +# Note: revision will depend on your repository type, i.e git, svc, bzr etc... +# revision = "abc123" +# version = "1.0.0" diff --git a/cmd/dep/testdata/harness_tests/init/skip-hidden/final/Gopkg.toml b/cmd/dep/testdata/harness_tests/init/skip-hidden/final/Gopkg.toml index e69de29bb2..77028df6ae 100644 --- a/cmd/dep/testdata/harness_tests/init/skip-hidden/final/Gopkg.toml +++ b/cmd/dep/testdata/harness_tests/init/skip-hidden/final/Gopkg.toml @@ -0,0 +1,9 @@ + +# Example: +# [[dependencies]] +# source = "https://github.com/myfork/package.git" +# branch = "master" +# name = "github.com/vendor/package" +# Note: revision will depend on your repository type, i.e git, svc, bzr etc... +# revision = "abc123" +# version = "1.0.0" diff --git a/fs.go b/fs.go index 5837342590..03616ff3aa 100644 --- a/fs.go +++ b/fs.go @@ -75,6 +75,14 @@ func writeFile(path string, in toml.Marshaler) error { return err } +// modifyWithString modifies a given file with a new string input. +// This is used to write arbitrary string data to a file, such as +// updating the `Gopkg.toml` file with example data if no deps found +// on init. +func modifyWithString(path, data string) error { + return ioutil.WriteFile(path, []byte(data), 0644) +} + // renameWithFallback attempts to rename a file or directory, but falls back to // copying in the event of a cross-link device error. If the fallback copy // succeeds, src is still removed, emulating normal rename behavior. diff --git a/manifest.go b/manifest.go index db35aa5bd6..3066d35c10 100644 --- a/manifest.go +++ b/manifest.go @@ -140,6 +140,14 @@ func (m *Manifest) toRaw() rawManifest { return raw } +// IsEmpty - Checks if payload is empty +func (m *Manifest) IsEmpty() bool { + if len(m.Ovr) == 0 && len(m.Ignored) == 0 && len(m.Dependencies) == 0 && len(m.Required) == 0 { + return true + } + return false +} + type sortedRawProjects []rawProject func (s sortedRawProjects) Len() int { return len(s) } diff --git a/txn_writer.go b/txn_writer.go index 6321504d98..fc6fc9aa6c 100644 --- a/txn_writer.go +++ b/txn_writer.go @@ -19,6 +19,20 @@ import ( "github.com/sdboyer/gps" ) +// Example string to be written to the manifest file +// if no dependencies are found in the project +// during `dep init` +const exampleToml = ` +# Example: +# [[dependencies]] +# source = "https://github.com/myfork/package.git" +# branch = "master" +# name = "github.com/vendor/package" +# Note: revision will depend on your repository type, i.e git, svc, bzr etc... +# revision = "abc123" +# version = "1.0.0" +` + // SafeWriter transactionalizes writes of manifest, lock, and vendor dir, both // individually and in any combination, into a pseudo-atomic action with // transactional rollback. @@ -172,6 +186,7 @@ const ( // - If oldLock is provided without newLock, error. // - If vendor is VendorAlways without a newLock, error. func (sw *SafeWriter) Prepare(manifest *Manifest, oldLock, newLock *Lock, vendor VendorBehavior) error { + sw.Payload = &SafeWriterPayload{ Manifest: manifest, Lock: newLock, @@ -227,6 +242,7 @@ func (payload SafeWriterPayload) validate(root string, sm gps.SourceManager) err // This mostly guarantees that dep cannot exit with a partial write that would // leave an undefined state on disk. func (sw *SafeWriter) Write(root string, sm gps.SourceManager) error { + if sw.Payload == nil { return errors.New("Cannot call SafeWriter.Write before SafeWriter.Prepare") } @@ -252,7 +268,12 @@ func (sw *SafeWriter) Write(root string, sm gps.SourceManager) error { defer os.RemoveAll(td) if sw.Payload.HasManifest() { - if err := writeFile(filepath.Join(td, ManifestName), sw.Payload.Manifest); err != nil { + if sw.Payload.Manifest.IsEmpty() { + err := modifyWithString(filepath.Join(td, ManifestName), exampleToml) + if err != nil { + return errors.Wrap(err, "failed to generate example text") + } + } else if err := writeFile(filepath.Join(td, ManifestName), sw.Payload.Manifest); err != nil { return errors.Wrap(err, "failed to write manifest file to temp dir") } }