-
Notifications
You must be signed in to change notification settings - Fork 0
/
unexpose.go
115 lines (109 loc) · 2.54 KB
/
unexpose.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
/**
* Created by zc on 2020/9/9.
*/
package api
import (
"bytes"
"errors"
specyaml "github.com/drone/drone-yaml/yaml"
"github.com/drone/drone-yaml/yaml/linter"
"github.com/drone/drone/core"
"github.com/drone/drone/trigger/dag"
"github.com/vinzenz/yaml"
"strings"
)
func CheckClone(data string) ([]byte, error) {
ds := strings.Split(data, "---\n")
bs := make([][]byte, 0, len(ds))
for _, d := range ds {
if strings.Trim(strings.Trim(d, "\n"), " ") == "" {
continue
}
dn, err := cloneDisabled(d)
if err != nil {
return nil, err
}
bs = append(bs, bytes.Trim(dn, "\n"))
}
if len(bs) == 1 {
return bs[0], nil
}
out := bytes.Join(bs, []byte("\n---\n"))
dst := [][]byte{
[]byte("---"),
out,
}
return bytes.Join(dst, []byte("\n")), nil
}
func cloneDisabled(data string) ([]byte, error) {
var out map[interface{}]interface{}
if err := yaml.Unmarshal([]byte(data), &out); err != nil {
return nil, err
}
out["clone"] = specyaml.Clone{Disable: true, SkipVerify: true}
return yaml.Marshal(&out)
}
func parsePipeline(data []byte, dd *dag.Dag) ([]*specyaml.Pipeline, error) {
manifest, err := specyaml.ParseBytes(data)
if err != nil {
return nil, errors.New("check: cannot parse yaml")
}
if err = linter.Manifest(manifest, false); err != nil {
return nil, errors.New("check: yaml linting error")
}
if dd == nil {
dd = dag.New()
}
var matched []*specyaml.Pipeline
for _, document := range manifest.Resources {
pipeline, ok := document.(*specyaml.Pipeline)
if !ok {
continue
}
completion(pipeline)
dd.Add(pipeline.Name, pipeline.DependsOn...)
matched = append(matched, pipeline)
}
if dd.DetectCycles() {
return nil, errors.New("check: dependency cycle detected in Pipeline")
}
if len(matched) == 0 {
return nil, errors.New("check: skipping build, no matching pipelines")
}
return matched, nil
}
func completion(pipeline *specyaml.Pipeline) {
if pipeline.Name == "" {
pipeline.Name = "default"
}
if pipeline.Kind == "" {
pipeline.Kind = "pipeline"
}
if pipeline.Type == "" {
pipeline.Type = "docker"
}
if pipeline.Platform.OS == "" {
pipeline.Platform.OS = "linux"
}
if pipeline.Platform.Arch == "" {
pipeline.Platform.Arch = "amd64"
}
pipeline.Clone = specyaml.Clone{Disable: true}
}
func completionStage(stage *core.Stage) {
if stage.Kind == "" {
stage.Kind = "pipeline"
}
if stage.Kind == "pipeline" && stage.Type == "" {
stage.Type = "docker"
}
if stage.OS == "" {
stage.OS = "linux"
}
if stage.Arch == "" {
stage.Arch = "amd64"
}
if stage.Name == "" {
stage.Name = "default"
}
}