github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_replace.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the MIT License. 3 // This product includes software developed at Guance Cloud (https://www.guance.com/). 4 // Copyright 2021-present Guance, Inc. 5 6 package funcs 7 8 import ( 9 "fmt" 10 "reflect" 11 "regexp" 12 13 "github.com/GuanceCloud/cliutils/pipeline/ptinput" 14 "github.com/GuanceCloud/platypus/pkg/ast" 15 "github.com/GuanceCloud/platypus/pkg/engine/runtime" 16 "github.com/GuanceCloud/platypus/pkg/errchain" 17 ) 18 19 func ReplaceChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 20 if len(funcExpr.Param) != 3 { 21 return runtime.NewRunError(ctx, fmt.Sprintf( 22 "func %s expects 3 args", funcExpr.Name), funcExpr.NamePos) 23 } 24 25 if _, err := getKeyName(funcExpr.Param[0]); err != nil { 26 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos()) 27 } 28 29 var pattern string 30 switch funcExpr.Param[1].NodeType { //nolint:exhaustive 31 case ast.TypeStringLiteral: 32 pattern = funcExpr.Param[1].StringLiteral().Val 33 default: 34 return runtime.NewRunError(ctx, fmt.Sprintf( 35 "expect StringLiteral, got %s", 36 funcExpr.Param[1].NodeType), funcExpr.Param[1].StartPos()) 37 } 38 39 reg, err := regexp.Compile(pattern) 40 if err != nil { 41 return runtime.NewRunError(ctx, fmt.Sprintf("regular expression %s parse err: %s", 42 reflect.TypeOf(funcExpr.Param[1]).String(), err.Error()), funcExpr.Param[1].StartPos()) 43 } 44 45 funcExpr.PrivateData = reg 46 47 switch funcExpr.Param[2].NodeType { //nolint:exhaustive 48 case ast.TypeStringLiteral: 49 default: 50 return runtime.NewRunError(ctx, fmt.Sprintf( 51 "expect StringLiteral, got %s", 52 funcExpr.Param[2].NodeType), funcExpr.Param[2].StartPos()) 53 } 54 return nil 55 } 56 57 func Replace(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 58 reg, ok := funcExpr.PrivateData.(*regexp.Regexp) 59 if !ok { 60 return runtime.NewRunError(ctx, "regexp obj not found", funcExpr.Param[1].StartPos()) 61 } 62 63 if len(funcExpr.Param) != 3 { 64 return runtime.NewRunError(ctx, fmt.Sprintf( 65 "func %s expects 3 args", funcExpr.Name), funcExpr.NamePos) 66 } 67 68 key, err := getKeyName(funcExpr.Param[0]) 69 if err != nil { 70 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos()) 71 } 72 73 var dz string 74 75 switch funcExpr.Param[2].NodeType { //nolint:exhaustive 76 case ast.TypeStringLiteral: 77 dz = funcExpr.Param[2].StringLiteral().Val 78 default: 79 return runtime.NewRunError(ctx, fmt.Sprintf("expect StringLiteral, got %s", 80 funcExpr.Param[2].NodeType), funcExpr.Param[2].StartPos()) 81 } 82 83 cont, err := ctx.GetKeyConv2Str(key) 84 if err != nil { 85 l.Debugf("key `%v' not exist, ignored", key) 86 return nil //nolint:nilerr 87 } 88 89 newCont := reg.ReplaceAllString(cont, dz) 90 _ = addKey2PtWithVal(ctx.InData(), key, newCont, ast.String, ptinput.KindPtDefault) 91 92 return nil 93 }