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 #34 from imikushin/trash-conf
Browse files Browse the repository at this point in the history
Support simpler file format
  • Loading branch information
imikushin committed May 31, 2016
2 parents 8366ae9 + da2465d commit 2edf757
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 82 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@ Keeping the trash in your ./vendor dir to a minimum.

## How to use

Make sure you're using Go 1.5+ and **GO15VENDOREXPERIMENT=1** env var is exported.
Make sure you're using Go-1.6+ (or Go-1.5.x and `GO15VENDOREXPERIMENT=1` is in your environment).

0. `go get github.com/rancher/trash`
1. Copy `trash.yml` file to your project and edit to your needs.
0. Download and extract `trash` to your PATH
1. Copy `trash.conf` file to your project and edit to your needs.
2. Run `trash`

`trash.yml` (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:
`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:
```
github.com/rancher/trash
github.com/Sirupsen/logrus v0.8.7 https://github.com/imikushin/logrus.git
github.com/codegangsta/cli b5232bb
github.com/cloudfoundry-incubator/candiedyaml 5a459c2
```

Or, in YML format:
```yaml
import:
- package: github.com/Sirupsen/logrus # package name
Expand All @@ -37,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.yml (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 trash.conf (and you can Force it to use any other file). Just in case, here's the program help:

```
$ trash -h
Expand All @@ -57,7 +66,7 @@ COMMANDS:
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--file, -f "trash.yml" Vendored packages list
--file, -f "trash.conf" Vendored packages list
--directory, -C "." The directory in which to run, --file is relative to this
--keep, -k Keep all downloaded vendor code (preserving .git dirs)
--debug, -d Debug logging
Expand Down
87 changes: 87 additions & 0 deletions conf/conf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package conf

import (
"bufio"
"os"
"strings"

"github.com/Sirupsen/logrus"
yaml "github.com/cloudfoundry-incubator/candiedyaml"
)

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

type Import struct {
Package string `yaml:"package,omitempty"`
Version string `yaml:"version,omitempty"`
Repo string `yaml:"repo,omitempty"`
}

func Parse(path string) (*Trash, 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.deleteDups()
return trash, nil
}

trash = &Trash{}
_, err = file.Seek(0, 0)
if err != nil {
return nil, err
}

scanner := bufio.NewScanner(bufio.NewReader(file))
for scanner.Scan() {
line := scanner.Text()
if commentStart := strings.Index(line, "#"); commentStart >= 0 {
line = line[0:commentStart]
}
if line = strings.TrimSpace(line); line == "" {
continue
}
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)
continue
}

packageImport := Import{}
packageImport.Package = fields[0] // at least 1 field at this point: trimmed the line and skipped empty
if len(fields) > 2 {
packageImport.Repo = fields[2]
}
if len(fields) > 1 {
packageImport.Version = fields[1]
}
trash.Imports = append(trash.Imports, packageImport)
}

trash.deleteDups()
return trash, nil
}

// deleteDups delete duplicate imports
func (t *Trash) deleteDups() {
seen := make(map[string]bool)
uniq := make([]Import, 0, len(t.Imports))
for _, i := range t.Imports {
if seen[i.Package] {
logrus.Warnf("Package '%s' has duplicates (in trash.conf)", i.Package)
continue
}
uniq = append(uniq, i)
seen[i.Package] = true
}
t.Imports = uniq
}
File renamed without changes.
47 changes: 0 additions & 47 deletions conf/yaml.go

This file was deleted.

11 changes: 11 additions & 0 deletions trash.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#trash-conf

# package
github.com/rancher/trash

github.com/Sirupsen/logrus v0.9.0
github.com/codegangsta/cli v1.2.0
github.com/cloudfoundry-incubator/candiedyaml 4e924c7
github.com/stretchr/testify v1.1.3
github.com/davecgh/go-spew 5215b55
github.com/pmezard/go-difflib 792786c
7 changes: 3 additions & 4 deletions trash.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "file, f",
Value: "trash.yml",
Value: "trash.conf",
Usage: "Vendored packages list",
},
cli.StringFlag{
Expand Down Expand Up @@ -64,7 +64,7 @@ func main() {
exit(app.Run(os.Args))
}

var possibleTrashFiles = []string{"glide.yaml", "glide.yml", "trash.yaml"}
var possibleTrashFiles = []string{"glide.yaml", "glide.yml", "trash.yaml", "trash.yml"}

func run(c *cli.Context) error {
if c.Bool("debug") {
Expand Down Expand Up @@ -517,7 +517,6 @@ func removeEmptyDirs() error {
func cleanup(dir string, trashConf *conf.Trash) error {
rootPackage := trashConf.Package
if rootPackage == "" {
logrus.Warn("Project's root package not specified in trash.yml (put `package: github.com/your/project` before `import:`)")
logrus.Info("Trying to guess the root package from directory structure")
srcPath := path.Join(dir, "..", "..", "..", "..", "src")
if _, err := os.Stat(srcPath); err != nil {
Expand Down Expand Up @@ -545,7 +544,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.yml)", i.Package)
logrus.Warnf("Package '%s' has been completely removed: it's probably useless (in trash.conf)", i.Package)
} else {
logrus.Errorf("os.Stat() failed for: %s", dir+"/vendor/"+i.Package)
}
Expand Down
20 changes: 0 additions & 20 deletions trash.yml

This file was deleted.

5 changes: 0 additions & 5 deletions util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ import (
"github.com/stretchr/testify/require"
)

func TestTrue(t *testing.T) {
assert := require.New(t)
assert.True(true)
}

func TestOneMsg(t *testing.T) {
assert := require.New(t)
c := OneStr("qq")
Expand Down

0 comments on commit 2edf757

Please sign in to comment.