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

Commit 9d5c83a

Browse files
authored
Merge pull request #35 from imikushin/update
Generate or update trash.conf
2 parents 2edf757 + ab2cb62 commit 9d5c83a

File tree

194 files changed

+108042
-52
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

194 files changed

+108042
-52
lines changed

conf/conf.go

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@ package conf
22

33
import (
44
"bufio"
5+
"fmt"
56
"os"
7+
"sort"
68
"strings"
79

810
"github.com/Sirupsen/logrus"
911
yaml "github.com/cloudfoundry-incubator/candiedyaml"
1012
)
1113

1214
type Trash struct {
13-
Package string `yaml:"package,omitempty"`
14-
Imports []Import `yaml:"import,omitempty"`
15+
Package string `yaml:"package,omitempty"`
16+
Imports []Import `yaml:"import,omitempty"`
17+
importMap map[string]Import
1518
}
1619

1720
type Import struct {
@@ -29,7 +32,7 @@ func Parse(path string) (*Trash, error) {
2932

3033
trash := &Trash{}
3134
if err := yaml.NewDecoder(file).Decode(trash); err == nil {
32-
trash.deleteDups()
35+
trash.Dedupe()
3336
return trash, nil
3437
}
3538

@@ -67,21 +70,57 @@ func Parse(path string) (*Trash, error) {
6770
trash.Imports = append(trash.Imports, packageImport)
6871
}
6972

70-
trash.deleteDups()
73+
trash.Dedupe()
7174
return trash, nil
7275
}
7376

74-
// deleteDups delete duplicate imports
75-
func (t *Trash) deleteDups() {
76-
seen := make(map[string]bool)
77-
uniq := make([]Import, 0, len(t.Imports))
77+
// Dedupe deletes duplicates and sorts the imports
78+
func (t *Trash) Dedupe() {
79+
t.importMap = map[string]Import{}
7880
for _, i := range t.Imports {
79-
if seen[i.Package] {
80-
logrus.Warnf("Package '%s' has duplicates (in trash.conf)", i.Package)
81+
if _, ok := t.importMap[i.Package]; ok {
82+
logrus.Debugf("Package '%s' has duplicates (in trash.conf)", i.Package)
8183
continue
8284
}
83-
uniq = append(uniq, i)
84-
seen[i.Package] = true
85+
t.importMap[i.Package] = i
8586
}
86-
t.Imports = uniq
87+
ps := make([]string, 0, len(t.importMap))
88+
for p := range t.importMap {
89+
ps = append(ps, p)
90+
}
91+
sort.Strings(ps)
92+
imports := make([]Import, 0, len(t.importMap))
93+
for _, p := range ps {
94+
imports = append(imports, t.importMap[p])
95+
}
96+
t.Imports = imports
97+
}
98+
99+
func (t *Trash) Get(pkg string) (Import, bool) {
100+
i, ok := t.importMap[pkg]
101+
return i, ok
102+
}
103+
104+
func (t *Trash) Dump(path string) error {
105+
file, err := os.Create(path)
106+
defer file.Close()
107+
if err != nil {
108+
return err
109+
}
110+
111+
w := bufio.NewWriter(file)
112+
defer w.Flush()
113+
114+
fmt.Fprintln(w, "# trash.conf")
115+
fmt.Fprintln(w)
116+
fmt.Fprintln(w, "# package")
117+
fmt.Fprintln(w, t.Package)
118+
fmt.Fprintln(w)
119+
120+
for _, i := range t.Imports {
121+
s := fmt.Sprintf("%s\t%s\t%s", i.Package, i.Version, i.Repo)
122+
fmt.Fprintln(w, strings.TrimSpace(s))
123+
}
124+
125+
return nil
87126
}

conf/conf_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestDuplicates(t *testing.T) {
4040

4141
for i, d := range testData {
4242
trash := Trash{"", d.imports}
43-
trash.deleteDups()
43+
trash.Dedupe()
4444

4545
if d.duplicates != len(d.imports)-len(trash.Imports) {
4646
t.Errorf("Case %d failed: expected %d duplicates but removed %d", i, d.duplicates, len(d.imports)-len(trash.Imports))

trash.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#trash-conf
1+
# trash.conf
22

33
# package
44
github.com/rancher/trash
@@ -9,3 +9,4 @@ github.com/cloudfoundry-incubator/candiedyaml 4e924c7
99
github.com/stretchr/testify v1.1.3
1010
github.com/davecgh/go-spew 5215b55
1111
github.com/pmezard/go-difflib 792786c
12+
golang.org/x/sys 076b546

0 commit comments

Comments
 (0)