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

Commit da2465d

Browse files
joshwgetimikushin
authored andcommitted
Support simpler file format
1 parent 8366ae9 commit da2465d

File tree

8 files changed

+116
-82
lines changed

8 files changed

+116
-82
lines changed

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,22 @@ Keeping the trash in your ./vendor dir to a minimum.
44

55
## How to use
66

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

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

13-
`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:
13+
`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:
14+
```
15+
github.com/rancher/trash
16+
17+
github.com/Sirupsen/logrus v0.8.7 https://github.com/imikushin/logrus.git
18+
github.com/codegangsta/cli b5232bb
19+
github.com/cloudfoundry-incubator/candiedyaml 5a459c2
20+
```
21+
22+
Or, in YML format:
1423
```yaml
1524
import:
1625
- package: github.com/Sirupsen/logrus # package name
@@ -37,7 +46,7 @@ I'd been slightly reluctant to the idea of writing it, but apparently the world
3746

3847
## Help
3948

40-
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:
49+
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:
4150

4251
```
4352
$ trash -h
@@ -57,7 +66,7 @@ COMMANDS:
5766
help, h Shows a list of commands or help for one command
5867
5968
GLOBAL OPTIONS:
60-
--file, -f "trash.yml" Vendored packages list
69+
--file, -f "trash.conf" Vendored packages list
6170
--directory, -C "." The directory in which to run, --file is relative to this
6271
--keep, -k Keep all downloaded vendor code (preserving .git dirs)
6372
--debug, -d Debug logging

conf/conf.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package conf
2+
3+
import (
4+
"bufio"
5+
"os"
6+
"strings"
7+
8+
"github.com/Sirupsen/logrus"
9+
yaml "github.com/cloudfoundry-incubator/candiedyaml"
10+
)
11+
12+
type Trash struct {
13+
Package string `yaml:"package,omitempty"`
14+
Imports []Import `yaml:"import,omitempty"`
15+
}
16+
17+
type Import struct {
18+
Package string `yaml:"package,omitempty"`
19+
Version string `yaml:"version,omitempty"`
20+
Repo string `yaml:"repo,omitempty"`
21+
}
22+
23+
func Parse(path string) (*Trash, error) {
24+
file, err := os.Open(path)
25+
if err != nil {
26+
return nil, err
27+
}
28+
defer file.Close()
29+
30+
trash := &Trash{}
31+
if err := yaml.NewDecoder(file).Decode(trash); err == nil {
32+
trash.deleteDups()
33+
return trash, nil
34+
}
35+
36+
trash = &Trash{}
37+
_, err = file.Seek(0, 0)
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
scanner := bufio.NewScanner(bufio.NewReader(file))
43+
for scanner.Scan() {
44+
line := scanner.Text()
45+
if commentStart := strings.Index(line, "#"); commentStart >= 0 {
46+
line = line[0:commentStart]
47+
}
48+
if line = strings.TrimSpace(line); line == "" {
49+
continue
50+
}
51+
fields := strings.Fields(line)
52+
53+
if len(fields) == 1 && trash.Package == "" {
54+
trash.Package = fields[0] // use the first 1-field line as the root package
55+
logrus.Infof("Using '%s' as the project's root package (from trash.conf)", trash.Package)
56+
continue
57+
}
58+
59+
packageImport := Import{}
60+
packageImport.Package = fields[0] // at least 1 field at this point: trimmed the line and skipped empty
61+
if len(fields) > 2 {
62+
packageImport.Repo = fields[2]
63+
}
64+
if len(fields) > 1 {
65+
packageImport.Version = fields[1]
66+
}
67+
trash.Imports = append(trash.Imports, packageImport)
68+
}
69+
70+
trash.deleteDups()
71+
return trash, nil
72+
}
73+
74+
// deleteDups delete duplicate imports
75+
func (t *Trash) deleteDups() {
76+
seen := make(map[string]bool)
77+
uniq := make([]Import, 0, len(t.Imports))
78+
for _, i := range t.Imports {
79+
if seen[i.Package] {
80+
logrus.Warnf("Package '%s' has duplicates (in trash.conf)", i.Package)
81+
continue
82+
}
83+
uniq = append(uniq, i)
84+
seen[i.Package] = true
85+
}
86+
t.Imports = uniq
87+
}
File renamed without changes.

conf/yaml.go

Lines changed: 0 additions & 47 deletions
This file was deleted.

trash.conf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#trash-conf
2+
3+
# package
4+
github.com/rancher/trash
5+
6+
github.com/Sirupsen/logrus v0.9.0
7+
github.com/codegangsta/cli v1.2.0
8+
github.com/cloudfoundry-incubator/candiedyaml 4e924c7
9+
github.com/stretchr/testify v1.1.3
10+
github.com/davecgh/go-spew 5215b55
11+
github.com/pmezard/go-difflib 792786c

trash.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func main() {
3434
app.Flags = []cli.Flag{
3535
cli.StringFlag{
3636
Name: "file, f",
37-
Value: "trash.yml",
37+
Value: "trash.conf",
3838
Usage: "Vendored packages list",
3939
},
4040
cli.StringFlag{
@@ -64,7 +64,7 @@ func main() {
6464
exit(app.Run(os.Args))
6565
}
6666

67-
var possibleTrashFiles = []string{"glide.yaml", "glide.yml", "trash.yaml"}
67+
var possibleTrashFiles = []string{"glide.yaml", "glide.yml", "trash.yaml", "trash.yml"}
6868

6969
func run(c *cli.Context) error {
7070
if c.Bool("debug") {
@@ -517,7 +517,6 @@ func removeEmptyDirs() error {
517517
func cleanup(dir string, trashConf *conf.Trash) error {
518518
rootPackage := trashConf.Package
519519
if rootPackage == "" {
520-
logrus.Warn("Project's root package not specified in trash.yml (put `package: github.com/your/project` before `import:`)")
521520
logrus.Info("Trying to guess the root package from directory structure")
522521
srcPath := path.Join(dir, "..", "..", "..", "..", "src")
523522
if _, err := os.Stat(srcPath); err != nil {
@@ -545,7 +544,7 @@ func cleanup(dir string, trashConf *conf.Trash) error {
545544
for _, i := range trashConf.Imports {
546545
if _, err := os.Stat(dir + "/vendor/" + i.Package); err != nil {
547546
if os.IsNotExist(err) {
548-
logrus.Warnf("Package '%s' has been completely removed: it's probably useless (in trash.yml)", i.Package)
547+
logrus.Warnf("Package '%s' has been completely removed: it's probably useless (in trash.conf)", i.Package)
549548
} else {
550549
logrus.Errorf("os.Stat() failed for: %s", dir+"/vendor/"+i.Package)
551550
}

trash.yml

Lines changed: 0 additions & 20 deletions
This file was deleted.

util/util_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ import (
66
"github.com/stretchr/testify/require"
77
)
88

9-
func TestTrue(t *testing.T) {
10-
assert := require.New(t)
11-
assert.True(true)
12-
}
13-
149
func TestOneMsg(t *testing.T) {
1510
assert := require.New(t)
1611
c := OneStr("qq")

0 commit comments

Comments
 (0)