github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_url_parse.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 "net/url" 11 "strings" 12 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 URLParseChecking(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 && funcExpr.Param[0].NodeType != ast.TypeAttrExpr { 24 return runtime.NewRunError(ctx, fmt.Sprintf( 25 "expect Identifier or AttrExpr, got %s", funcExpr.Param[0].NodeType), funcExpr.Param[0].StartPos()) 26 } 27 return nil 28 } 29 30 func URLParse(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError { 31 if len(funcExpr.Param) != 1 { 32 return runtime.NewRunError(ctx, fmt.Sprintf( 33 "func %s expects 1 arg", funcExpr.Name), funcExpr.NamePos) 34 } 35 if funcExpr.Param[0].NodeType != ast.TypeIdentifier && funcExpr.Param[0].NodeType != ast.TypeAttrExpr { 36 return runtime.NewRunError(ctx, fmt.Sprintf( 37 "expect Identifier or AttrExpr, got %s", funcExpr.Param[0].NodeType), 38 funcExpr.Param[0].StartPos()) 39 } 40 key, err := getKeyName(funcExpr.Param[0]) 41 if err != nil { 42 return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos()) 43 } 44 u, err := ctx.GetKeyConv2Str(key) 45 if err != nil { 46 l.Debug(err) 47 return nil 48 } 49 50 uu, err := url.Parse(u) 51 if err != nil { 52 return runtime.NewRunError(ctx, fmt.Sprintf( 53 "failed to parse url %s: %s", u, err.Error()), funcExpr.NamePos) 54 } 55 56 params := make(map[string]any) 57 for k, vs := range uu.Query() { 58 params[k] = strings.Join(vs, ",") 59 } 60 res := map[string]any{ 61 "scheme": uu.Scheme, 62 "host": uu.Host, 63 "port": uu.Port(), 64 "path": uu.Path, 65 "params": params, 66 } 67 ctx.Regs.ReturnAppend(res, ast.Map) 68 return nil 69 }