github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_parse_duration.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 "time" 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 ParseDurationChecking(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 _, err := getKeyName(funcExpr.Param[0]); err != nil { 24 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos()) 25 } 26 return nil 27 } 28 29 func ParseDuration(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 30 if len(funcExpr.Param) != 1 { 31 l.Debugf("parse_duration(): invalid param") 32 33 return runtime.NewRunError(ctx, fmt.Sprintf( 34 "func %s expects 1 arg", funcExpr.Name), funcExpr.NamePos) 35 } 36 37 key, err := getKeyName(funcExpr.Param[0]) 38 if err != nil { 39 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos()) 40 } 41 42 cont, err := ctx.GetKey(key) 43 if err != nil { 44 l.Debug(err) 45 return nil 46 } 47 48 duStr, ok := cont.Value.(string) 49 if !ok { 50 l.Debug(err) 51 return nil 52 // return runtime.NewRunError(ctx, "parse_duration() expect string arg", 53 // funcExpr.Param[0].StartPos()) 54 } 55 56 l.Debugf("parse duration %s", duStr) 57 du, err := time.ParseDuration(duStr) 58 if err != nil { 59 l.Debug(err) 60 return nil 61 } 62 63 _ = addKey2PtWithVal(ctx.InData(), key, int64(du), ast.Int, ptinput.KindPtDefault) 64 return nil 65 }