/
project.go
124 lines (95 loc) · 2.53 KB
/
project.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
114
115
116
117
118
119
120
121
122
123
124
package werf
import (
"encoding/json"
"os"
. "github.com/onsi/gomega"
"github.com/werf/werf/pkg/build"
iutils "github.com/werf/werf/test/pkg/utils"
)
func NewProject(werfBinPath, gitRepoPath string) *Project {
return &Project{
WerfBinPath: werfBinPath,
GitRepoPath: gitRepoPath,
}
}
type Project struct {
GitRepoPath string
WerfBinPath string
}
type CommonOptions struct {
ShouldFail bool
ExtraArgs []string
}
type BuildOptions struct {
CommonOptions
}
type BuildWithReportOptions struct {
CommonOptions
}
type KubeRunOptions struct {
CommonOptions
Command []string
Image string
}
type KubeCtlOptions struct {
CommonOptions
}
type runCommandOptions struct {
ShouldFail bool
Args []string
}
func (p *Project) Build(opts *BuildOptions) (combinedOut string) {
if opts == nil {
opts = &BuildOptions{}
}
args := append([]string{"build"}, opts.ExtraArgs...)
outb, err := iutils.RunCommand(p.GitRepoPath, p.WerfBinPath, args...)
if opts.ShouldFail {
Expect(err).To(HaveOccurred())
} else {
Expect(err).NotTo(HaveOccurred())
}
return string(outb)
}
func (p *Project) BuildWithReport(buildReportPath string, opts *BuildWithReportOptions) (string, build.ImagesReport) {
if opts == nil {
opts = &BuildWithReportOptions{}
}
args := append([]string{"build", "--report-path", buildReportPath}, opts.ExtraArgs...)
out := p.runCommand(runCommandOptions{Args: args, ShouldFail: opts.ShouldFail})
buildReportRaw, err := os.ReadFile(buildReportPath)
Expect(err).NotTo(HaveOccurred())
var buildReport build.ImagesReport
Expect(json.Unmarshal(buildReportRaw, &buildReport)).To(Succeed())
return out, buildReport
}
func (p *Project) KubeRun(opts *KubeRunOptions) string {
if opts == nil {
opts = &KubeRunOptions{}
}
args := append([]string{"kube-run"}, opts.ExtraArgs...)
if opts.Image != "" {
args = append(args, opts.Image)
}
if len(opts.Command) > 0 {
args = append(args, "--")
args = append(args, opts.Command...)
}
return p.runCommand(runCommandOptions{Args: args, ShouldFail: opts.ShouldFail})
}
func (p *Project) KubeCtl(opts *KubeCtlOptions) string {
if opts == nil {
opts = &KubeCtlOptions{}
}
args := append([]string{"kubectl"}, opts.ExtraArgs...)
return p.runCommand(runCommandOptions{Args: args, ShouldFail: opts.ShouldFail})
}
func (p *Project) runCommand(opts runCommandOptions) string {
outb, err := iutils.RunCommand(p.GitRepoPath, p.WerfBinPath, opts.Args...)
if opts.ShouldFail {
Expect(err).To(HaveOccurred())
} else {
Expect(err).NotTo(HaveOccurred())
}
return string(outb)
}