/
visitor_params.go
89 lines (76 loc) · 2.71 KB
/
visitor_params.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
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pipelineyml
import (
"fmt"
"strconv"
"strings"
"gopkg.in/yaml.v3"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/pkg/expression"
)
type ParamsVisitor struct {
Data []byte
RunPipelineParams []apistructs.PipelineRunParam
}
func NewParamsVisitor(data []byte, runPipelineParam []apistructs.PipelineRunParam) *ParamsVisitor {
v := ParamsVisitor{}
v.Data = data
v.RunPipelineParams = runPipelineParam
return &v
}
func (v *ParamsVisitor) Visit(s *Spec) {
// 运行时输入参数转化为map
var runParamsMap = make(map[string]interface{})
for _, v := range v.RunPipelineParams {
runParamsMap[v.Name] = v.Value
}
replaced := ReplacePipelineParams(string(v.Data), runParamsMap)
if err := yaml.Unmarshal([]byte(replaced), s); err != nil {
s.appendError(fmt.Errorf("failed to unmarshal to spec after replaced params, err: %v", err))
}
}
func ReplacePipelineParams(pipeline string, params map[string]interface{}) string {
for k, v := range params {
replaceStr := ""
switch v.(type) {
case int:
replaceStr = strconv.Itoa(v.(int))
case float64:
replaceStr = strconv.FormatFloat(v.(float64), 'f', -1, 64)
case float32:
replaceStr = strconv.FormatFloat(v.(float64), 'f', -1, 32)
case bool:
replaceStr = strconv.FormatBool(v.(bool))
case string:
replaceStr = v.(string)
default:
replaceStr = fmt.Sprintf("%v", v)
}
// generate random params before replace
replaceStr = expression.ReplaceRandomParams(replaceStr)
// 替换老的
pipeline = strings.ReplaceAll(pipeline, fmt.Sprintf("%s%s.%s%s", expression.OldLeftPlaceholder, expression.Params, k, expression.OldRightPlaceholder), replaceStr)
// 替换新的语法
pipeline = strings.ReplaceAll(pipeline, fmt.Sprintf("%s %s.%s %s", expression.LeftPlaceholder, expression.Params, k, expression.RightPlaceholder), replaceStr)
//placeholderParams := map[string]string{}
//placeholderParams[fmt.Sprintf("%s.%s", expression.Params, k)] = replaceStr
//result, err := expression.MatchEval(pipeline, placeholderParams, expression.Params)
//if err != nil {
// continue
//}
//pipeline = result
}
return pipeline
}