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

ADDED example comment to toml file on init #343 #374

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions manifest.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the remaining available options, revision, version, and source (e.g. https://github.com/myfork/package)? That way a user will know all the values that they can provide, and what an acceptable value looks like for each one.

# [[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 @@ -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,
Expand Down Expand Up @@ -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")
}
Expand All @@ -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")
}
}
Expand Down