-
Notifications
You must be signed in to change notification settings - Fork 787
/
step_syntax_validate_buildpacks.go
127 lines (111 loc) · 3.16 KB
/
step_syntax_validate_buildpacks.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
126
127
package syntax
import (
"fmt"
"io/ioutil"
"path/filepath"
"sort"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/jenkinsfile"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
repoNames = []string{
"classic",
"kubernetes",
}
)
// StepSyntaxValidateBuildPacksOptions contains the command line flags
type StepSyntaxValidateBuildPacksOptions struct {
step.StepOptions
}
// NewCmdStepSyntaxValidateBuildPacks Creates a new Command object
func NewCmdStepSyntaxValidateBuildPacks(commonOpts *opts.CommonOptions) *cobra.Command {
options := &StepSyntaxValidateBuildPacksOptions{
StepOptions: step.StepOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "buildpacks",
Short: "Validates all available build packs",
Example: "buildpacks",
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
return cmd
}
// Run implements this command
func (o *StepSyntaxValidateBuildPacksOptions) Run() error {
var err error
draftDir, err := util.DraftDir()
if err != nil {
return errors.Wrapf(err, "couldn't get root directory of build packs")
}
var packNames []string
errorsByPack := make(map[string][]string)
for _, repo := range repoNames {
packsDir := filepath.Join(draftDir, "packs", "github.com", "jenkins-x-buildpacks", fmt.Sprintf("jenkins-x-%s", repo), "packs")
exists, err := util.DirExists(packsDir)
if err != nil {
return errors.Wrapf(err, "error reading packs dir %s", packsDir)
}
if !exists {
return fmt.Errorf("packs directory %s does not exist or is not a directory", packsDir)
}
packs, err := ioutil.ReadDir(packsDir)
if err != nil {
return errors.Wrapf(err, "there was an error reading %s", packsDir)
}
for _, file := range packs {
if file.IsDir() {
yamlFile := filepath.Join(packsDir, file.Name(), jenkinsfile.PipelineConfigFileName)
exists, err := util.FileExists(yamlFile)
if err != nil {
return errors.Wrapf(err, "error reading %s", yamlFile)
}
if exists {
data, err := ioutil.ReadFile(yamlFile)
if err != nil {
return errors.Wrapf(err, "Failed to load file %s", yamlFile)
}
validationErrors, err := util.ValidateYaml(&jenkinsfile.PipelineConfig{}, data)
if err != nil {
return errors.Wrapf(err, "failed to validate YAML file %s", yamlFile)
}
packID := fmt.Sprintf("%s: %s", repo, file.Name())
packNames = append(packNames, packID)
if len(validationErrors) > 0 {
errorsByPack[packID] = validationErrors
}
}
}
}
}
hasError := false
sort.Strings(packNames)
for _, k := range packNames {
v, exists := errorsByPack[k]
if !exists {
log.Logger().Infof("SUCCESS: %s", k)
} else {
hasError = true
log.Logger().Errorf("FAILURE: %s", k)
for _, e := range v {
log.Logger().Errorf("\t%s", e)
}
}
}
if hasError {
return errors.New("one or more build packs failed validation")
}
return nil
}