Skip to content

Commit

Permalink
Merge pull request golang#374 from EwanValentine/feature/create-toml-…
Browse files Browse the repository at this point in the history
…file-examples-on-init

ADDED example comment to toml file on init golang#343
  • Loading branch information
sdboyer committed Apr 26, 2017
2 parents 10149f1 + bd827f4 commit 2fb9fd4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
@@ -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"
@@ -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"
8 changes: 8 additions & 0 deletions fs.go
Expand Up @@ -65,6 +65,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.
Expand Down
8 changes: 8 additions & 0 deletions manifest.go
Expand Up @@ -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) }
Expand Down
23 changes: 22 additions & 1 deletion txn_writer.go
Expand Up @@ -18,6 +18,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.
Expand Down Expand Up @@ -175,6 +189,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,
Expand Down Expand Up @@ -230,6 +245,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")
}
Expand All @@ -255,7 +271,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")
}
}
Expand Down

0 comments on commit 2fb9fd4

Please sign in to comment.