@@ -2,16 +2,19 @@ package conf
22
33import (
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
1214type 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
1720type 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}
0 commit comments