-
Notifications
You must be signed in to change notification settings - Fork 32
/
builddep.go
67 lines (55 loc) · 1.55 KB
/
builddep.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package recipes
import (
"fmt"
"github.com/godarch/darch/pkg/recipes"
"github.com/godarch/darch/pkg/utils"
"github.com/urfave/cli"
)
var builddepCommand = cli.Command{
Name: "build-dep",
Usage: "list dependencies for the given recipes",
ArgsUsage: "<recipes>*N",
Action: func(clicontext *cli.Context) error {
var (
recipeNames = clicontext.Args()
)
allRecipes, err := recipes.GetAllRecipes(getRecipesDir(clicontext))
if err != nil {
return err
}
if len(recipeNames) == 0 {
// We want dependencies for all images.
for _, recipe := range allRecipes {
recipeNames = append(recipeNames, recipe.Name)
}
}
// First, let's make sure all the recipes we are building exist.
for _, recipeName := range recipeNames {
if _, ok := allRecipes[recipeName]; !ok {
return fmt.Errorf("recipe %s doesn't exist", recipeName)
}
}
dependencies := make([]string, 0)
for _, r := range allRecipes {
if utils.Contains(recipeNames, r.Name) {
parents := walkRecipeRecursively(r, allRecipes)
parents = utils.Reverse(parents)
dependencies = append(dependencies, parents...)
}
}
dependencies = utils.RemoveDuplicates(dependencies)
for _, dependency := range dependencies {
fmt.Println(dependency)
}
return err
},
}
func walkRecipeRecursively(r recipes.Recipe, rs map[string]recipes.Recipe) []string {
result := make([]string, 0)
result = append(result, r.Name)
if !r.InheritsExternal {
children := walkRecipeRecursively(rs[r.Inherits], rs)
result = append(result, children...)
}
return result
}