Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 640786c

Browse files
authored
Merge pull request #374 from EwanValentine/feature/create-toml-file-examples-on-init
ADDED example comment to toml file on init #343
2 parents bd967f7 + c1aa041 commit 640786c

File tree

5 files changed

+56
-1
lines changed

5 files changed

+56
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
# Example:
3+
# [[dependencies]]
4+
# source = "https://github.com/myfork/package.git"
5+
# branch = "master"
6+
# name = "github.com/vendor/package"
7+
# Note: revision will depend on your repository type, i.e git, svc, bzr etc...
8+
# revision = "abc123"
9+
# version = "1.0.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
# Example:
3+
# [[dependencies]]
4+
# source = "https://github.com/myfork/package.git"
5+
# branch = "master"
6+
# name = "github.com/vendor/package"
7+
# Note: revision will depend on your repository type, i.e git, svc, bzr etc...
8+
# revision = "abc123"
9+
# version = "1.0.0"

fs.go

+8
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ func writeFile(path string, in toml.Marshaler) error {
6565
return err
6666
}
6767

68+
// modifyWithString modifies a given file with a new string input.
69+
// This is used to write arbitrary string data to a file, such as
70+
// updating the `Gopkg.toml` file with example data if no deps found
71+
// on init.
72+
func modifyWithString(path, data string) error {
73+
return ioutil.WriteFile(path, []byte(data), 0644)
74+
}
75+
6876
// renameWithFallback attempts to rename a file or directory, but falls back to
6977
// copying in the event of a cross-link device error. If the fallback copy
7078
// succeeds, src is still removed, emulating normal rename behavior.

manifest.go

+8
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ func (m *Manifest) toRaw() rawManifest {
140140
return raw
141141
}
142142

143+
// IsEmpty - Checks if payload is empty
144+
func (m *Manifest) IsEmpty() bool {
145+
if len(m.Ovr) == 0 && len(m.Ignored) == 0 && len(m.Dependencies) == 0 && len(m.Required) == 0 {
146+
return true
147+
}
148+
return false
149+
}
150+
143151
type sortedRawProjects []rawProject
144152

145153
func (s sortedRawProjects) Len() int { return len(s) }

txn_writer.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ import (
1818
"github.com/sdboyer/gps"
1919
)
2020

21+
// Example string to be written to the manifest file
22+
// if no dependencies are found in the project
23+
// during `dep init`
24+
const exampleToml = `
25+
# Example:
26+
# [[dependencies]]
27+
# source = "https://github.com/myfork/package.git"
28+
# branch = "master"
29+
# name = "github.com/vendor/package"
30+
# Note: revision will depend on your repository type, i.e git, svc, bzr etc...
31+
# revision = "abc123"
32+
# version = "1.0.0"
33+
`
34+
2135
// SafeWriter transactionalizes writes of manifest, lock, and vendor dir, both
2236
// individually and in any combination, into a pseudo-atomic action with
2337
// transactional rollback.
@@ -175,6 +189,7 @@ const (
175189
// - If oldLock is provided without newLock, error.
176190
// - If vendor is VendorAlways without a newLock, error.
177191
func (sw *SafeWriter) Prepare(manifest *Manifest, oldLock, newLock *Lock, vendor VendorBehavior) error {
192+
178193
sw.Payload = &SafeWriterPayload{
179194
Manifest: manifest,
180195
Lock: newLock,
@@ -230,6 +245,7 @@ func (payload SafeWriterPayload) validate(root string, sm gps.SourceManager) err
230245
// This mostly guarantees that dep cannot exit with a partial write that would
231246
// leave an undefined state on disk.
232247
func (sw *SafeWriter) Write(root string, sm gps.SourceManager) error {
248+
233249
if sw.Payload == nil {
234250
return errors.New("Cannot call SafeWriter.Write before SafeWriter.Prepare")
235251
}
@@ -255,7 +271,12 @@ func (sw *SafeWriter) Write(root string, sm gps.SourceManager) error {
255271
defer os.RemoveAll(td)
256272

257273
if sw.Payload.HasManifest() {
258-
if err := writeFile(filepath.Join(td, ManifestName), sw.Payload.Manifest); err != nil {
274+
if sw.Payload.Manifest.IsEmpty() {
275+
err := modifyWithString(filepath.Join(td, ManifestName), exampleToml)
276+
if err != nil {
277+
return errors.Wrap(err, "failed to generate example text")
278+
}
279+
} else if err := writeFile(filepath.Join(td, ManifestName), sw.Payload.Manifest); err != nil {
259280
return errors.Wrap(err, "failed to write manifest file to temp dir")
260281
}
261282
}

0 commit comments

Comments
 (0)