This repository has been archived by the owner on Aug 9, 2021. It is now read-only.
/
engine_base.go
125 lines (109 loc) · 3.18 KB
/
engine_base.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
125
package engine
import (
"capsulecd/pkg/config"
"capsulecd/pkg/errors"
"capsulecd/pkg/pipeline"
"capsulecd/pkg/utils"
stderrors "errors"
"fmt"
"github.com/Masterminds/semver"
)
type engineBase struct {
Config config.Interface
PipelineData *pipeline.Data
}
// default Compile Step.
func (g *engineBase) CompileStep() error {
if terr := g.ExecuteCmdList("engine_cmd_compile",
g.PipelineData.GitLocalPath,
nil,
"",
"Compile command (%s) failed. Check log for more details.",
); terr != nil {
return terr
}
return nil
}
// default Test step
// assumes that the lint and code fmt commands are very similar and that engine_cmd_fmt includes engine_cmd_lint.
func (g *engineBase) TestStep() error {
//skip the lint commands if disabled
if !g.Config.GetBool("engine_disable_lint") {
//run test command
lintKey := "engine_cmd_lint"
if g.Config.GetBool("engine_enable_code_mutation") {
lintKey = "engine_cmd_fmt"
}
if terr := g.ExecuteCmdList(lintKey,
g.PipelineData.GitLocalPath,
nil,
"",
"Lint command (%s) failed. Check log for more details.",
); terr != nil {
return terr
}
}
//run test command
if terr := g.ExecuteCmdList("engine_cmd_test",
g.PipelineData.GitLocalPath,
nil,
"",
"Test command (%s) failed. Check log for more details.",
); terr != nil {
return terr
}
//skip the security test commands if disabled
if !g.Config.GetBool("engine_disable_security_check") {
//run security check command
if terr := g.ExecuteCmdList("engine_cmd_security_check",
g.PipelineData.GitLocalPath,
nil,
"",
"Dependency vulnerability check command (%s) failed. Check log for more details.",
); terr != nil {
return terr
}
}
return nil
}
//Helper functions
func (e *engineBase) BumpVersion(currentVersion string) (string, error) {
v, nerr := semver.NewVersion(currentVersion)
if nerr != nil {
return "", nerr
}
switch bumpType := e.Config.GetString("engine_version_bump_type"); bumpType {
case "major":
return fmt.Sprintf("%d.%d.%d", v.Major()+1, 0, 0), nil
case "minor":
return fmt.Sprintf("%d.%d.%d", v.Major(), v.Minor()+1, 0), nil
case "patch":
return fmt.Sprintf("%d.%d.%d", v.Major(), v.Minor(), v.Patch()+1), nil
default:
return "", stderrors.New("Unknown version bump interval")
}
}
func (e *engineBase) ExecuteCmdList(configKey string, workingDir string, environ []string, logPrefix string, errorTemplate string) error {
cmd := e.Config.GetString(configKey)
// we have to support 2 types of cmds.
// - simple commands (engine_cmd_compile: 'compile command')
// and list commands (engine_cmd_compile: - 'compile command' \n - 'compile command 2' \n ..)
// GetString will return "" if this is a list of commands.
if cmd != "" {
//code formatter
if terr := utils.BashCmdExec(cmd, workingDir, environ, logPrefix); terr != nil {
return errors.EngineTestRunnerError(fmt.Sprintf(errorTemplate, cmd))
}
} else {
cmdList := e.Config.GetStringSlice(configKey)
if cmdList == nil {
return nil
}
for i := range cmdList {
if terr := utils.BashCmdExec(cmdList[i], workingDir, environ, logPrefix); terr != nil {
return errors.EngineTestRunnerError(fmt.Sprintf(errorTemplate, cmdList[i]))
}
}
}
return nil
}