forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.go
76 lines (61 loc) · 1.84 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
package main
import (
"context"
"fmt"
"time"
"github.com/alibaba/pouch/cli/build"
"github.com/spf13/cobra"
)
// buildDescription is used to describe build command in detail and auto generate command doc.
var buildDescription = "Build an image from a Dockerfile"
// BuildCommand use to implement 'build' command, it download image.
type BuildCommand struct {
baseCommand
buildArgs []string
tagList []string
target string
addr string
}
// Init initialize pull command.
func (b *BuildCommand) Init(c *Cli) {
b.cli = c
b.cmd = &cobra.Command{
Use: "build [OPTION] PATH",
Short: "Build an image from a Dockerfile",
Long: buildDescription,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return b.runBuild(args)
},
}
b.addFlags()
}
func (b *BuildCommand) addFlags() {
flagSet := b.cmd.Flags()
flagSet.StringArrayVar(&b.buildArgs, "build-arg", nil, "Set build-time variables")
flagSet.StringArrayVarP(&b.tagList, "tag", "t", nil, "Name and optionally a tag in the 'name:tag' format")
flagSet.StringVar(&b.target, "target", "", "Set the target build stage to build")
flagSet.StringVar(&b.addr, "addr", "unix:///run/buildkit/buildkitd.sock", "buildkitd address")
}
func (b *BuildCommand) runBuild(args []string) error {
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
opts := b.buildOptions(args[0])
return build.Build(ctx, b.addr, opts)
}
func (b *BuildCommand) buildOptions(workdir string) *build.Options {
opts := &build.Options{
TagList: b.tagList,
// TODO: build args
Target: b.target,
}
opts.LocalDirs = map[string]string{
"dockerfile": workdir,
"context": workdir,
}
// using unknown:timestamp if there is no tag
if len(opts.TagList) == 0 {
opts.TagList = append(opts.TagList, fmt.Sprintf("unknown:%v", time.Now().UnixNano()))
}
return opts
}