From 5c0fedd5201e151572c2e929cfe3e844f67166a5 Mon Sep 17 00:00:00 2001 From: Ivan Mikushin Date: Fri, 9 Sep 2016 17:03:44 -0700 Subject: [PATCH] Rename trash.conf to vendor.conf in messages and readme Also, rename Trash struct to Conf to better reflect the purpose. --- README.md | 8 ++++---- conf/conf.go | 41 +++++++++++++++++++++------------------ conf/conf_test.go | 2 +- trash.go | 30 ++++++++++++++-------------- trash.conf => vendor.conf | 2 -- 5 files changed, 42 insertions(+), 41 deletions(-) rename trash.conf => vendor.conf (96%) diff --git a/README.md b/README.md index 126342f..0d68b6e 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 diff --git a/conf/conf.go b/conf/conf.go index 2322cdb..a066e94 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -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 { @@ -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 @@ -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 } @@ -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 @@ -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 { @@ -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) @@ -124,3 +123,7 @@ func (t *Trash) Dump(path string) error { return nil } + +func (t *Conf) ConfFile() string { + return t.confFile +} diff --git a/conf/conf_test.go b/conf/conf_test.go index 797a977..adfc1a9 100644 --- a/conf/conf_test.go +++ b/conf/conf_test.go @@ -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) { diff --git a/trash.go b/trash.go index 85bb3b9..73d19fc 100644 --- a/trash.go +++ b/trash.go @@ -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") @@ -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 @@ -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 == "" { @@ -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 @@ -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) @@ -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!") @@ -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) @@ -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) } diff --git a/trash.conf b/vendor.conf similarity index 96% rename from trash.conf rename to vendor.conf index 17deee1..f164b9d 100644 --- a/trash.conf +++ b/vendor.conf @@ -1,5 +1,3 @@ -# trash.conf - # package github.com/rancher/trash