@@ -17,11 +17,9 @@ import (
1717 "github.com/Sirupsen/logrus"
1818 "github.com/urfave/cli"
1919
20- "reflect"
21-
20+ "github.com/Masterminds/glide/godep"
2221 "github.com/rancher/trash/conf"
2322 "github.com/rancher/trash/util"
24- "github.com/Masterminds/glide/godep"
2523 "gopkg.in/yaml.v2"
2624)
2725
@@ -52,9 +50,9 @@ func main() {
5250 Name : "keep, k" ,
5351 Usage : "Keep all downloaded vendor code (preserving .git dirs)" ,
5452 },
55- cli.BoolFlag {
53+ cli.StringSliceFlag {
5654 Name : "update, u" ,
57- Usage : "Update vendored packages, add missing ones " ,
55+ Usage : "specify a list of packages to be updated " ,
5856 },
5957 cli.BoolFlag {
6058 Name : "insecure" ,
@@ -100,11 +98,16 @@ func run(c *cli.Context) error {
10098 targetDir := c .String ("target" )
10199 confFile := c .String ("file" )
102100 keep := c .Bool ("keep" )
103- update := c .Bool ("update" )
104101 insecure := c .Bool ("insecure" )
105102 trashDir := c .String ("cache" )
106103 gopath = c .String ("gopath" )
107104
105+ update := false
106+ updateVendor := c .StringSlice ("update" )
107+ if len (updateVendor ) > 0 {
108+ update = true
109+ }
110+
108111 trashDir , err := filepath .Abs (trashDir )
109112 if err != nil {
110113 return err
@@ -141,56 +144,23 @@ func run(c *cli.Context) error {
141144 if err != nil {
142145 return err
143146 }
144- defer save (trashConf )
145147
146148 if update {
147- //return updateTrash(trashDir, dir, targetDir, confFile, trashConf, insecure)
148- data , err := ioutil .ReadFile (".trash-conf" )
149- if err != nil {
150- logrus .Fatal ("No .trash-conf found. Make sure to run `trash` to generate .trash-conf first before you run `trash -u`" )
151- }
152- var export conf.ExportMap
153- if err := yaml .Unmarshal (data , & export ); err != nil {
154- return err
155- }
156149 imports := []conf.Import {}
157- for k , imp := range trashConf .ImportMap {
158- if existingImp , ok := export .Imports [k ]; ! ok {
159- imp .Update = true
160- } else if ! reflect .DeepEqual (imp , existingImp ) {
161- imp .Update = true
150+ for _ , imp := range updateVendor {
151+ for k , existingImp := range trashConf .ImportMap {
152+ if strings .Contains (k , imp ) {
153+ existingImp .Update = true
154+ imports = append (imports , existingImp )
155+ }
162156 }
163- imports = append (imports , imp )
164157 }
165158 trashConf .Imports = imports
166159 }
167-
168- var extraImports []conf.Import
169- for _ , packageImport := range trashConf .Imports {
170- if packageImport .Transitive {
171- if update && ! packageImport .Update {
172- continue
173- }
174- repoDir := path .Join (trashDir , "src" , packageImport .Package )
175- transitiveDependencies , err := godep .Parse (repoDir )
176- if err != nil {
177- return err
178- }
179- for _ , transitiveDependency := range transitiveDependencies {
180- extraImports = append (extraImports , conf.Import {
181- Package : transitiveDependency .Name ,
182- Version : transitiveDependency .Reference ,
183- Repo : transitiveDependency .Repository ,
184- })
185- }
186- if len (transitiveDependencies ) == 0 {
187- imports , err := parseTransitiveVendor (repoDir )
188- if err != nil {
189- return err
190- }
191- extraImports = append (extraImports , imports ... )
192- }
193- }
160+ alreadyImported := map [string ]bool {}
161+ extraImports , err := updateTransitiveVendor (keep , update , trashDir , dir , targetDir , trashConf , insecure , alreadyImported )
162+ if err != nil {
163+ return err
194164 }
195165
196166 // clean duplicate imports
@@ -253,7 +223,61 @@ func run(c *cli.Context) error {
253223 return cleanup (update , dir , targetDir , trashConf )
254224}
255225
256- func parseTransitiveVendor (repoDir string ) ([]conf.Import , error ) {
226+ func updateTransitiveVendor (keep , update bool , trashDir , dir , targetDir string , trashConf * conf.Conf , insecure bool , alreadyImported map [string ]bool ) ([]conf.Import , error ) {
227+ extraImports := []conf.Import {}
228+ // we don't need to vendor files first if none of the imports are transitive
229+ updateVendor := false
230+ for _ , packageImport := range trashConf .Imports {
231+ if packageImport .Transitive {
232+ updateVendor = true
233+ break
234+ }
235+ }
236+ if updateVendor {
237+ if err := vendor (keep , update , trashDir , dir , targetDir , trashConf , insecure ); err != nil {
238+ return extraImports , err
239+ }
240+ }
241+ for _ , packageImport := range trashConf .Imports {
242+ if packageImport .Transitive {
243+ if alreadyImported [packageImport .Package ] {
244+ logrus .Warnf ("Already searched transitive dep %s. Skipping" , packageImport .Package )
245+ continue
246+ }
247+ alreadyImported [packageImport .Package ] = true
248+ if update && ! packageImport .Update {
249+ continue
250+ }
251+ repoDir := path .Join (trashDir , "src" , packageImport .Package )
252+ transitiveDependencies , err := godep .Parse (repoDir )
253+ if err != nil {
254+ return extraImports , err
255+ }
256+ for _ , transitiveDependency := range transitiveDependencies {
257+ extraImports = append (extraImports , conf.Import {
258+ Package : transitiveDependency .Name ,
259+ Version : transitiveDependency .Reference ,
260+ Repo : transitiveDependency .Repository ,
261+ })
262+ }
263+ if len (transitiveDependencies ) == 0 {
264+ config , err := parseTransitiveVendor (repoDir )
265+ if err != nil {
266+ return extraImports , err
267+ }
268+ if imports , err := updateTransitiveVendor (keep , update , trashDir , dir , targetDir , & config , insecure , alreadyImported ); err != nil {
269+ return extraImports , err
270+ } else {
271+ extraImports = append (extraImports , imports ... )
272+ }
273+ extraImports = append (extraImports , config .Imports ... )
274+ }
275+ }
276+ }
277+ return extraImports , nil
278+ }
279+
280+ func parseTransitiveVendor (repoDir string ) (conf.Conf , error ) {
257281 configFile := ""
258282 for _ , f := range []string {"vendor.conf" , "trash.conf" , "vndr.cfg" , "vendor.manifest" , "trash.yml" , "glide.yaml" , "glide.yml" , "trash.yaml" } {
259283 if _ , err := os .Stat (filepath .Join (repoDir , f )); err == nil {
@@ -262,13 +286,13 @@ func parseTransitiveVendor(repoDir string) ([]conf.Import, error) {
262286 }
263287 }
264288 if configFile == "" {
265- return [] conf.Import {}, nil
289+ return conf.Conf {}, nil
266290 }
267291 trashConf , err := conf .Parse (configFile )
268292 if err != nil {
269- return [] conf.Import {}, err
293+ return conf.Conf {}, err
270294 }
271- return trashConf . Imports , nil
295+ return * trashConf , nil
272296}
273297
274298func updateTrash (trashDir , dir , targetDir , trashFile string , trashConf * conf.Conf , insecure bool ) error {
@@ -965,6 +989,11 @@ func cleanup(update bool, dir, targetDir string, trashConf *conf.Conf) error {
965989 if err := removeEmptyDirs (targetDir ); err != nil {
966990 logrus .Errorf ("Error removing empty dirs: %v" , err )
967991 }
992+ writeConf := conf.Conf {
993+ Package : trashConf .Package ,
994+ Imports : []conf.Import {},
995+ Excludes : trashConf .Excludes ,
996+ }
968997 for _ , i := range trashConf .Imports {
969998 pth := dir + "/" + targetDir + "/" + i .Package
970999 if _ , err := os .Stat (pth ); err != nil {
@@ -973,20 +1002,14 @@ func cleanup(update bool, dir, targetDir string, trashConf *conf.Conf) error {
9731002 } else {
9741003 logrus .Errorf ("os.Stat() failed for: %s" , pth )
9751004 }
1005+ } else {
1006+ writeConf .Imports = append (writeConf .Imports , i )
9761007 }
9771008 }
978- return nil
979- }
980-
981- func save (config * conf.Conf ) {
982- export := conf.ExportMap {
983- Imports : config .ImportMap ,
984- }
985- data , err := yaml .Marshal (export )
1009+ data , err := yaml .Marshal (writeConf )
9861010 if err != nil {
987- logrus .Fatal (err )
988- }
989- if err := ioutil .WriteFile (".trash-conf" , data , 0644 ); err != nil {
990- logrus .Fatal (err )
1011+ return err
9911012 }
1013+ os .RemoveAll (path .Join (dir , "trash.lock" ))
1014+ return ioutil .WriteFile ("trash.lock" , data , 0755 )
9921015}
0 commit comments