-
Notifications
You must be signed in to change notification settings - Fork 39
/
ko.go
52 lines (39 loc) · 1.25 KB
/
ko.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
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package ko
import (
"bytes"
"fmt"
"io"
"os/exec"
"strings"
ctlbdk "github.com/k14s/kbld/pkg/kbld/builder/docker"
"github.com/k14s/kbld/pkg/kbld/config"
ctllog "github.com/k14s/kbld/pkg/kbld/logger"
)
type Ko struct {
logger ctllog.Logger
}
func NewKo(logger ctllog.Logger) Ko {
return Ko{logger: logger}
}
func (k *Ko) Build(image, directory string, opts config.SourceKoBuildOpts) (ctlbdk.DockerTmpRef, error) {
prefixedLogger := k.logger.NewPrefixedWriter(image + " | ")
prefixedLogger.Write([]byte(fmt.Sprintf("starting build (using ko): %s\n", directory)))
defer prefixedLogger.Write([]byte("finished build (using ko)\n"))
var stdoutBuf, stderrBuf bytes.Buffer
cmdArgs := []string{"publish", ".", "--local"}
if opts.RawOptions != nil {
cmdArgs = append(cmdArgs, *opts.RawOptions...)
}
cmd := exec.Command("ko", cmdArgs...)
cmd.Dir = directory
cmd.Stdout = io.MultiWriter(&stdoutBuf, prefixedLogger)
cmd.Stderr = io.MultiWriter(&stderrBuf, prefixedLogger)
err := cmd.Run()
if err != nil {
prefixedLogger.Write([]byte(fmt.Sprintf("error: %s\n", err)))
return ctlbdk.DockerTmpRef{}, err
}
return ctlbdk.NewDockerTmpRef(strings.Trim(stdoutBuf.String(), "\n")), nil
}