forked from godarch/darch
-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.go
113 lines (99 loc) · 2.63 KB
/
build.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package recipes
import (
"context"
"fmt"
"github.com/godarch/darch/pkg/cmd/darch/commands"
"github.com/godarch/darch/pkg/recipes"
"github.com/godarch/darch/pkg/repository"
"github.com/urfave/cli"
"strings"
)
var buildCommand = cli.Command{
Name: "build",
Usage: "build a recipe(s)",
ArgsUsage: "<recipes>",
Flags: []cli.Flag{
cli.StringFlag{
Name: "tags, t",
Usage: "the tag(s) to use when building the recipe",
Value: "latest",
},
cli.StringFlag{
Name: "image-prefix, p",
Usage: "the value to prepend to all image names (inherited and built)",
Value: "",
},
cli.StringSliceFlag{
Name: "environment, e",
},
},
Action: func(clicontext *cli.Context) error {
var (
tags = clicontext.String("tags")
imagePrefix = clicontext.String("image-prefix")
recipeNames = clicontext.Args()
env = clicontext.StringSlice("environment")
)
if len(recipeNames) == 0 {
return fmt.Errorf("no recipes provided")
}
defaultTag, additionalTags, err := parseTags(tags)
if err != nil {
return err
}
allRecipes, err := recipes.GetAllRecipes(getRecipesDir(clicontext))
if err != nil {
return err
}
// 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)
}
}
session, err := repository.NewSession(repository.DefaultContainerdSocketLocation)
if err != nil {
return err
}
resolver, err := commands.GetResolver(clicontext)
if err != nil {
return err
}
// Now, let's go through each recipe and build it.
for _, recipeName := range recipeNames {
fmt.Printf("building %s...\n", recipeName)
image, err := session.BuildRecipe(context.Background(), allRecipes[recipeName], defaultTag, imagePrefix, env, resolver)
if err != nil {
return err
}
fmt.Printf("built %s as %s\n", recipeName, image.FullName())
// Add additional tags.
if len(additionalTags) > 0 {
for _, tag := range additionalTags {
newImageRef, err := image.WithTag(tag)
if err != nil {
return err
}
fmt.Printf("tagging as %s\n", newImageRef.FullName())
err = session.TagImage(context.Background(), image, newImageRef)
if err != nil {
return err
}
}
}
}
return err
},
}
func parseTags(tags string) (string, []string, error) {
if len(tags) == 0 {
return "", nil, fmt.Errorf("invalid tag")
}
split := strings.Split(tags, ",")
for _, tag := range split {
if len(tag) == 0 {
return "", nil, fmt.Errorf("invalid tag")
}
}
return split[0], split[1:], nil
}