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 #35 from imikushin/update
Browse files Browse the repository at this point in the history
Generate or update trash.conf
  • Loading branch information
imikushin authored Jun 17, 2016
2 parents 2edf757 + ab2cb62 commit 9d5c83a
Show file tree
Hide file tree
Showing 194 changed files with 108,042 additions and 52 deletions.
65 changes: 52 additions & 13 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ package conf

import (
"bufio"
"fmt"
"os"
"sort"
"strings"

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

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

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

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

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

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

// deleteDups delete duplicate imports
func (t *Trash) deleteDups() {
seen := make(map[string]bool)
uniq := make([]Import, 0, len(t.Imports))
// Dedupe deletes duplicates and sorts the imports
func (t *Trash) Dedupe() {
t.importMap = map[string]Import{}
for _, i := range t.Imports {
if seen[i.Package] {
logrus.Warnf("Package '%s' has duplicates (in trash.conf)", i.Package)
if _, ok := t.importMap[i.Package]; ok {
logrus.Debugf("Package '%s' has duplicates (in trash.conf)", i.Package)
continue
}
uniq = append(uniq, i)
seen[i.Package] = true
t.importMap[i.Package] = i
}
t.Imports = uniq
ps := make([]string, 0, len(t.importMap))
for p := range t.importMap {
ps = append(ps, p)
}
sort.Strings(ps)
imports := make([]Import, 0, len(t.importMap))
for _, p := range ps {
imports = append(imports, t.importMap[p])
}
t.Imports = imports
}

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

func (t *Trash) Dump(path string) error {
file, err := os.Create(path)
defer file.Close()
if err != nil {
return err
}

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)

for _, i := range t.Imports {
s := fmt.Sprintf("%s\t%s\t%s", i.Package, i.Version, i.Repo)
fmt.Fprintln(w, strings.TrimSpace(s))
}

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

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

if d.duplicates != len(d.imports)-len(trash.Imports) {
t.Errorf("Case %d failed: expected %d duplicates but removed %d", i, d.duplicates, len(d.imports)-len(trash.Imports))
Expand Down
3 changes: 2 additions & 1 deletion trash.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#trash-conf
# trash.conf

# package
github.com/rancher/trash
Expand All @@ -9,3 +9,4 @@ 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
golang.org/x/sys 076b546
Loading

0 comments on commit 9d5c83a

Please sign in to comment.