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

Commit 8563489

Browse files
author
Craig Jellick
authored
Merge pull request #82 from StrongMonkey/master
more transitive
2 parents 2a2476d + 8d9ff98 commit 8563489

File tree

18 files changed

+564
-146
lines changed

18 files changed

+564
-146
lines changed

conf/conf.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ import (
1212
)
1313

1414
type Conf struct {
15-
Package string `yaml:"package,omitempty"`
16-
Imports []Import `yaml:"import,omitempty"`
17-
Excludes []string `yaml:"exclude,omitempty"`
18-
ImportMap map[string]Import
19-
confFile string
20-
yamlType bool
15+
Package string `yaml:"package,omitempty"`
16+
Imports []Import `yaml:"import,omitempty"`
17+
Excludes []string `yaml:"exclude,omitempty"`
18+
ImportMap map[string]Import `yaml:"-"`
19+
confFile string `yaml:"-"`
20+
yamlType bool `yaml:"-"`
2121
}
2222

2323
type Import struct {
2424
Package string `yaml:"package,omitempty"`
2525
Version string `yaml:"version,omitempty"`
2626
Repo string `yaml:"repo,omitempty"`
27-
Update bool
28-
Options
27+
Update bool `yaml:"-"`
28+
Options `yaml:"-"`
2929
}
3030

3131
type Options struct {

trash.go

Lines changed: 88 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -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

274298
func 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
}

vendor.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ github.com/stretchr/testify v1.1.3
88
github.com/davecgh/go-spew 5215b55
99
github.com/pmezard/go-difflib 792786c
1010
golang.org/x/sys a408501
11-
github.com/Masterminds/glide v0.12.3
11+
github.com/Masterminds/glide fb6c62596ce1b29128f1ef7e329c367a648ee9b1 https://github.com/StrongMonkey/glide.git
12+
github.com/mitchellh/go-homedir b8bc1bf767474819792c23f32d8286a45736f1c6
1213
github.com/Masterminds/vcs v1.12.0
1314
gopkg.in/yaml.v2 eb3733d160e74a9c7e442f435eb3bea458e1d19f
1415

vendor/github.com/Masterminds/glide/.travis.yml

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/Masterminds/glide/CHANGELOG.md

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/Masterminds/glide/Makefile

Lines changed: 18 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)