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

Commit afed821

Browse files
joshwgetDenise
authored andcommitted
Support pulling in transitive dependencies
1 parent 456f0aa commit afed821

File tree

2 files changed

+104
-4
lines changed

2 files changed

+104
-4
lines changed

conf/conf.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ type Import struct {
2424
Package string `yaml:"package,omitempty"`
2525
Version string `yaml:"version,omitempty"`
2626
Repo string `yaml:"repo,omitempty"`
27+
Options
28+
}
29+
30+
type Options struct {
31+
Transitive bool `yaml:"transitive,omitempty"`
32+
Staging bool `yaml:"staging,omitempty"`
2733
}
2834

2935
func Parse(path string) (*Conf, error) {
@@ -67,11 +73,19 @@ func Parse(path string) (*Conf, error) {
6773
trashConf.Excludes = append(trashConf.Excludes, strings.TrimSpace(fields[0][1:]))
6874
continue
6975
}
76+
7077
// Otherwise it's an import pattern
7178
packageImport := Import{}
7279
packageImport.Package = fields[0] // at least 1 field at this point: trimmed the line and skipped empty
80+
if len(fields) > 3 {
81+
packageImport.Options = parseOptions(fields[3])
82+
}
7383
if len(fields) > 2 {
74-
packageImport.Repo = fields[2]
84+
if strings.Contains(fields[2], "=") {
85+
packageImport.Options = parseOptions(fields[2])
86+
} else {
87+
packageImport.Repo = fields[2]
88+
}
7589
}
7690
if len(fields) > 1 {
7791
packageImport.Version = fields[1]
@@ -83,6 +97,24 @@ func Parse(path string) (*Conf, error) {
8397
return trashConf, nil
8498
}
8599

100+
// Other options besides include_transitive can be included in the future
101+
func parseOptions(options string) Options {
102+
var importOptions Options
103+
parts := strings.Split(options, ",")
104+
for _, part := range parts {
105+
kvParts := strings.Split(part, "=")
106+
if len(kvParts) > 1 && kvParts[1] == "true" {
107+
switch kvParts[0] {
108+
case "transitive":
109+
importOptions.Transitive = true
110+
case "staging":
111+
importOptions.Staging = true
112+
}
113+
}
114+
}
115+
return importOptions
116+
}
117+
86118
// Dedupe deletes duplicates and sorts the imports
87119
func (t *Conf) Dedupe() {
88120
t.importMap = map[string]Import{}

trash.go

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"go/ast"
88
"go/parser"
99
"go/token"
10+
"io/ioutil"
1011
"os"
1112
"os/exec"
1213
"path"
@@ -16,6 +17,7 @@ import (
1617
"github.com/Sirupsen/logrus"
1718
"github.com/urfave/cli"
1819

20+
"github.com/Masterminds/glide/godep"
1921
"github.com/rancher/trash/conf"
2022
"github.com/rancher/trash/util"
2123
)
@@ -128,12 +130,75 @@ func run(c *cli.Context) error {
128130
if err != nil {
129131
return err
130132
}
133+
131134
if update {
132135
return updateTrash(trashDir, dir, targetDir, confFile, trashConf, insecure)
133136
}
137+
138+
if err := vendor(keep, trashDir, dir, targetDir, trashConf, insecure); err != nil {
139+
return err
140+
}
141+
142+
var extraImports []conf.Import
143+
for _, packageImport := range trashConf.Imports {
144+
if packageImport.Transitive {
145+
repoDir := path.Join(trashDir, "src", packageImport.Package)
146+
transitiveDependencies, err := godep.Parse(repoDir)
147+
if err != nil {
148+
return err
149+
}
150+
for _, transitiveDependency := range transitiveDependencies {
151+
extraImports = append(extraImports, conf.Import{
152+
Package: transitiveDependency.Name,
153+
Version: transitiveDependency.Reference,
154+
Repo: transitiveDependency.Repository,
155+
})
156+
}
157+
}
158+
}
159+
160+
var filteredExtraImports []conf.Import
161+
for _, extraImport := range extraImports {
162+
packageAlreadyImported := false
163+
for _, packageImport := range trashConf.Imports {
164+
if packageImport.Package == extraImport.Package {
165+
packageAlreadyImported = true
166+
break
167+
}
168+
}
169+
if !packageAlreadyImported {
170+
filteredExtraImports = append(filteredExtraImports, extraImport)
171+
}
172+
}
173+
trashConf.Imports = append(trashConf.Imports, filteredExtraImports...)
174+
134175
if err := vendor(keep, trashDir, dir, targetDir, trashConf, insecure); err != nil {
135176
return err
136177
}
178+
179+
vendorDir := path.Join(dir, targetDir)
180+
for _, packageImport := range trashConf.Imports {
181+
if !packageImport.Staging {
182+
continue
183+
}
184+
185+
packageLocation := path.Dir(packageImport.Package)
186+
baseDir := path.Join(trashDir, "src", packageImport.Package, "staging/src", packageLocation)
187+
188+
files, err := ioutil.ReadDir(baseDir)
189+
if err != nil {
190+
return err
191+
}
192+
for _, f := range files {
193+
repoDir := path.Join(baseDir, f.Name())
194+
target := path.Join(vendorDir, packageLocation)
195+
os.MkdirAll(target, 0755)
196+
if bytes, err := exec.Command("cp", "-a", repoDir, target).CombinedOutput(); err != nil {
197+
return fmt.Errorf("`cp -a %s %s` failed:\n%s", repoDir, target, bytes)
198+
}
199+
}
200+
}
201+
137202
if keep {
138203
return nil
139204
}
@@ -250,7 +315,9 @@ func vendor(keep bool, trashDir, dir, targetDir string, trashConf *conf.Conf, in
250315

251316
logrus.Info("Copying deps...")
252317
for _, i := range trashConf.Imports {
253-
cpy(vendorDir, trashDir, i)
318+
if err := cpy(vendorDir, trashDir, i); err != nil {
319+
return err
320+
}
254321
}
255322
logrus.Info("Copying deps... Done")
256323
if !keep {
@@ -331,13 +398,14 @@ func checkout(trashDir string, i conf.Import) {
331398
}
332399
}
333400

334-
func cpy(vendorDir, trashDir string, i conf.Import) {
401+
func cpy(vendorDir, trashDir string, i conf.Import) error {
335402
repoDir := path.Join(trashDir, "src", i.Package)
336403
target, _ := path.Split(path.Join(vendorDir, i.Package))
337404
os.MkdirAll(target, 0755)
338405
if bytes, err := exec.Command("cp", "-a", repoDir, target).CombinedOutput(); err != nil {
339-
logrus.Fatalf("`cp -a %s %s` failed:\n%s", repoDir, target, bytes)
406+
return fmt.Errorf("`cp -a %s %s` failed:\n%s", repoDir, target, bytes)
340407
}
408+
return nil
341409
}
342410

343411
func checkGitRepo(trashDir, repoDir string, i conf.Import, insecure bool) error {

0 commit comments

Comments
 (0)