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

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

Merged
8 changes: 8 additions & 0 deletions fs.go
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.
Copy link
Member

Choose a reason for hiding this comment

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

nit: s/modifyWithString - Modifies/modifyWithString modifies/

Just slightly more standard practice for docblocks.

// 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
1 change: 1 addition & 0 deletions manifest.go
Expand Up @@ -9,6 +9,7 @@ import (
"sort"

"bytes"

"github.com/pelletier/go-toml"
"github.com/pkg/errors"
"github.com/sdboyer/gps"
Expand Down
19 changes: 19 additions & 0 deletions txn_writer.go
Expand Up @@ -19,6 +19,16 @@ 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]]
# branch = "master"
# name = "github.com/vendor/package"
`

// 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 +182,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 +238,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 Down Expand Up @@ -349,6 +361,13 @@ func (sw *SafeWriter) Write(root string, sm gps.SourceManager) error {
os.RemoveAll(vendorbak)
}

if len(sw.Payload.Manifest.Dependencies) == 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

If the goal is to only print out the example help text when the manifest is empty, then you will also need to test the manifest's Ovr, Ignores and Required fields as well. It may be useful to combine those checks into an IsEmpty function on the Manifest struct.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I looked into the nil pointer error from the build and it is because the Write function doesn't always have a manifest given to it, so sw.Payload.Manifest was nil.

If you move this check under the sw.Payload.HasManifest() check above (https://github.com/golang/dep/blob/master/txn_writer.go#L254), then it will only append the help text when we have a manifest file to write.

err := modifyWithString(mpath, exampleToml)
if err != nil {
goto fail
}
}

return nil

fail:
Expand Down