Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #44 from imikushin/vendor-conf
Browse files Browse the repository at this point in the history
Cleanup: rename trash.conf to vendor.conf in messages and readme
  • Loading branch information
imikushin authored Sep 10, 2016
2 parents 6f78de3 + 5c0fedd commit 88b2cc6
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 41 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ Keeping the trash in your ./vendor dir to a minimum.
Make sure you're using go1.6 or later version.

0. Download and extract `trash` to your PATH
1. Copy `trash.conf` file to your project and edit to your needs.
1. Copy `vendor.conf` file to your project and edit to your needs.
2. Run `trash`

`trash.conf` (in your project root dir) specifies the revisions (git tags or commits, or branches - if you're drunk) of the libraries to be fetched, checked out and copied to ./vendor dir. For example:
`vendor.conf` (in your project root dir) specifies the revisions (git tags or commits, or branches - if you're drunk) of the libraries to be fetched, checked out and copied to ./vendor dir. For example:
```
github.com/rancher/trash
Expand Down Expand Up @@ -46,7 +46,7 @@ I'd been slightly reluctant to the idea of writing it, but apparently the world

## Help

For the world's convenience, `trash` can detect glide.yaml (and glide.yml, as well as trash.yaml) and use that instead of trash.conf (and you can Force it to use any other file). Just in case, here's the program help:
For the world's convenience, `trash` can detect glide.yaml (and glide.yml, as well as trash.yaml) and use that instead of vendor.conf (and you can Force it to use any other file). Just in case, here's the program help:

```
$ trash -h
Expand All @@ -66,7 +66,7 @@ COMMANDS:
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--file value, -f value Vendored packages list (default: "trash.conf")
--file value, -f value Vendored packages list (default: "vendor.conf")
--directory value, -C value The directory in which to run, --file is relative to this (default: ".")
--keep, -k Keep all downloaded vendor code (preserving .git dirs)
--update, -u Update vendored packages, add missing ones
Expand Down
41 changes: 22 additions & 19 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (
yaml "github.com/cloudfoundry-incubator/candiedyaml"
)

type Trash struct {
type Conf struct {
Package string `yaml:"package,omitempty"`
Imports []Import `yaml:"import,omitempty"`
importMap map[string]Import
confFile string
}

type Import struct {
Expand All @@ -23,20 +24,20 @@ type Import struct {
Repo string `yaml:"repo,omitempty"`
}

func Parse(path string) (*Trash, error) {
func Parse(path string) (*Conf, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()

trash := &Trash{}
if err := yaml.NewDecoder(file).Decode(trash); err == nil {
trash.Dedupe()
return trash, nil
trashConf := &Conf{confFile: path}
if err := yaml.NewDecoder(file).Decode(trashConf); err == nil {
trashConf.Dedupe()
return trashConf, nil
}

trash = &Trash{}
trashConf = &Conf{confFile: path}
_, err = file.Seek(0, 0)
if err != nil {
return nil, err
Expand All @@ -53,9 +54,9 @@ func Parse(path string) (*Trash, error) {
}
fields := strings.Fields(line)

if len(fields) == 1 && trash.Package == "" {
trash.Package = fields[0] // use the first 1-field line as the root package
logrus.Infof("Using '%s' as the project's root package (from trash.conf)", trash.Package)
if len(fields) == 1 && trashConf.Package == "" {
trashConf.Package = fields[0] // use the first 1-field line as the root package
logrus.Infof("Using '%s' as the project's root package (from %s)", trashConf.Package, trashConf.confFile)
continue
}

Expand All @@ -67,19 +68,19 @@ func Parse(path string) (*Trash, error) {
if len(fields) > 1 {
packageImport.Version = fields[1]
}
trash.Imports = append(trash.Imports, packageImport)
trashConf.Imports = append(trashConf.Imports, packageImport)
}

trash.Dedupe()
return trash, nil
trashConf.Dedupe()
return trashConf, nil
}

// Dedupe deletes duplicates and sorts the imports
func (t *Trash) Dedupe() {
func (t *Conf) Dedupe() {
t.importMap = map[string]Import{}
for _, i := range t.Imports {
if _, ok := t.importMap[i.Package]; ok {
logrus.Debugf("Package '%s' has duplicates (in trash.conf)", i.Package)
logrus.Debugf("Package '%s' has duplicates (in %s)", i.Package, t.confFile)
continue
}
t.importMap[i.Package] = i
Expand All @@ -96,12 +97,12 @@ func (t *Trash) Dedupe() {
t.Imports = imports
}

func (t *Trash) Get(pkg string) (Import, bool) {
func (t *Conf) Get(pkg string) (Import, bool) {
i, ok := t.importMap[pkg]
return i, ok
}

func (t *Trash) Dump(path string) error {
func (t *Conf) Dump(path string) error {
file, err := os.Create(path)
defer file.Close()
if err != nil {
Expand All @@ -111,8 +112,6 @@ func (t *Trash) Dump(path string) error {
w := bufio.NewWriter(file)
defer w.Flush()

fmt.Fprintln(w, "# trash.conf")
fmt.Fprintln(w)
fmt.Fprintln(w, "# package")
fmt.Fprintln(w, t.Package)
fmt.Fprintln(w)
Expand All @@ -124,3 +123,7 @@ func (t *Trash) Dump(path string) error {

return nil
}

func (t *Conf) ConfFile() string {
return t.confFile
}
2 changes: 1 addition & 1 deletion conf/conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestDuplicates(t *testing.T) {
}

for i, d := range testData {
trash := Trash{"", d.imports, nil}
trash := Conf{"", d.imports, nil}
trash.Dedupe()

if d.duplicates != len(d.imports)-len(trash.Imports) {
Expand Down
30 changes: 15 additions & 15 deletions trash.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func run(c *cli.Context) error {
}

dir := c.String("directory")
trashFile := c.String("file")
confFile := c.String("file")
keep := c.Bool("keep")
update := c.Bool("update")
trashDir := c.String("cache")
Expand All @@ -94,30 +94,30 @@ func run(c *cli.Context) error {
}
logrus.Debugf("dir: '%s'", dir)

for _, trashFile = range []string{trashFile, "trash.conf", "vndr.cfg", "vendor.manifest", "trash.yml", "glide.yaml", "glide.yml", "trash.yaml"} {
if _, err = os.Stat(trashFile); err == nil {
for _, confFile = range []string{confFile, "trash.conf", "vndr.cfg", "vendor.manifest", "trash.yml", "glide.yaml", "glide.yml", "trash.yaml"} {
if _, err = os.Stat(confFile); err == nil {
break
}
}
if err != nil {
if os.IsNotExist(err) && update {
trashFile = c.String("file")
logrus.Warnf("Trash! '%s' not found, creating a new one!", trashFile)
if _, err = os.Create(trashFile); err != nil {
confFile = c.String("file")
logrus.Warnf("Trash! '%s' not found, creating a new one!", confFile)
if _, err = os.Create(confFile); err != nil {
return err
}
} else {
return err
}
}
logrus.Infof("Trash! Reading file: '%s'", trashFile)
logrus.Infof("Trash! Reading file: '%s'", confFile)

trashConf, err := conf.Parse(trashFile)
trashConf, err := conf.Parse(confFile)
if err != nil {
return err
}
if update {
return updateTrash(trashDir, dir, trashFile, trashConf)
return updateTrash(trashDir, dir, confFile, trashConf)
}
if err := vendor(keep, trashDir, dir, trashConf); err != nil {
return err
Expand All @@ -128,7 +128,7 @@ func run(c *cli.Context) error {
return cleanup(dir, trashConf)
}

func updateTrash(trashDir, dir, trashFile string, trashConf *conf.Trash) error {
func updateTrash(trashDir, dir, trashFile string, trashConf *conf.Conf) error {
// TODO collect imports, create `trashConf *conf.Trash`
rootPackage := trashConf.Package
if rootPackage == "" {
Expand Down Expand Up @@ -161,7 +161,7 @@ func updateTrash(trashDir, dir, trashFile string, trashConf *conf.Trash) error {
imports = collectImports(rootPackage, libRoot)
}

trashConf = &conf.Trash{Package: rootPackage}
trashConf = &conf.Conf{Package: rootPackage}
for pkg := range imports {
if pkg == rootPackage || strings.HasPrefix(pkg, rootPackage+"/") {
continue
Expand Down Expand Up @@ -213,7 +213,7 @@ func getLatestVersion(libRoot, pkg string) (string, error) {
return s, nil
}

func vendor(keep bool, trashDir, dir string, trashConf *conf.Trash) error {
func vendor(keep bool, trashDir, dir string, trashConf *conf.Conf) error {
logrus.WithFields(logrus.Fields{"keep": keep, "dir": dir, "trashConf": trashConf}).Debug("vendor")
defer os.Chdir(dir)

Expand Down Expand Up @@ -647,7 +647,7 @@ func removeEmptyDirs() error {
}

func guessRootPackage(dir string) string {
logrus.Warn("Trying to guess the root package using GOPATH. It's best to specify it in `trash.conf`")
logrus.Warn("Trying to guess the root package using GOPATH. It's best to specify it in `vendor.conf`")
logrus.Warnf("GOPATH is '%s'", gopath)
if gopath == "" || strings.Contains(gopath, ":") {
logrus.Fatalf("GOPATH not set or is not a single path. You need to specify the root package!")
Expand All @@ -663,7 +663,7 @@ func guessRootPackage(dir string) string {
return dir[len(srcPath+"/"):]
}

func cleanup(dir string, trashConf *conf.Trash) error {
func cleanup(dir string, trashConf *conf.Conf) error {
rootPackage := trashConf.Package
if rootPackage == "" {
rootPackage = guessRootPackage(dir)
Expand All @@ -683,7 +683,7 @@ func cleanup(dir string, trashConf *conf.Trash) error {
for _, i := range trashConf.Imports {
if _, err := os.Stat(dir + "/vendor/" + i.Package); err != nil {
if os.IsNotExist(err) {
logrus.Warnf("Package '%s' has been completely removed: it's probably useless (in trash.conf)", i.Package)
logrus.Warnf("Package '%s' has been completely removed: it's probably useless (in %s)", i.Package, trashConf.ConfFile())
} else {
logrus.Errorf("os.Stat() failed for: %s", dir+"/vendor/"+i.Package)
}
Expand Down
2 changes: 0 additions & 2 deletions trash.conf → vendor.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# trash.conf

# package
github.com/rancher/trash

Expand Down

0 comments on commit 88b2cc6

Please sign in to comment.