github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_b64dec.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 "encoding/base64" 10 "fmt" 11 12 "github.com/GuanceCloud/cliutils/pipeline/ptinput" 13 "github.com/GuanceCloud/platypus/pkg/ast" 14 "github.com/GuanceCloud/platypus/pkg/engine/runtime" 15 "github.com/GuanceCloud/platypus/pkg/errchain" 16 ) 17 18 func B64decChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 19 if len(funcExpr.Param) != 1 { 20 return runtime.NewRunError(ctx, fmt.Sprintf( 21 "func %s expects 1 arg", funcExpr.Name), funcExpr.NamePos) 22 } 23 if funcExpr.Param[0].NodeType != ast.TypeIdentifier { 24 return runtime.NewRunError(ctx, fmt.Sprintf( 25 "expect Identifier, got %s", funcExpr.Param[0].NodeType), 26 funcExpr.Param[0].StartPos()) 27 } 28 return nil 29 } 30 31 func B64dec(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 32 if len(funcExpr.Param) != 1 { 33 return runtime.NewRunError(ctx, fmt.Sprintf( 34 "func %s expects 1 arg", funcExpr.Name), funcExpr.NamePos) 35 } 36 if funcExpr.Param[0].NodeType != ast.TypeIdentifier { 37 return runtime.NewRunError(ctx, fmt.Sprintf( 38 "expect Identifier, got %s", funcExpr.Param[0].NodeType), 39 funcExpr.Param[0].StartPos()) 40 } 41 42 key, err := getKeyName(funcExpr.Param[0]) 43 if err != nil { 44 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos()) 45 } 46 val, err := ctx.GetKey(key) 47 if err != nil { 48 l.Debugf("key `%v` does not exist, ignored", key) 49 return nil //nolint:nilerr 50 } 51 var cont string 52 53 switch v := val.Value.(type) { 54 case string: 55 cont = v 56 default: 57 l.Debugf("key `%s` has value not of type string, ignored", key) 58 return nil 59 } 60 res, err := base64.StdEncoding.DecodeString(cont) 61 if err != nil { 62 return runtime.NewRunError(ctx, err.Error(), funcExpr.NamePos) 63 } 64 _ = addKey2PtWithVal(ctx.InData(), key, string(res), ast.String, ptinput.KindPtDefault) 65 return nil 66 }