github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_useragent.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  
    11  	"github.com/GuanceCloud/cliutils/pipeline/ptinput"
    12  	"github.com/GuanceCloud/platypus/pkg/ast"
    13  	"github.com/GuanceCloud/platypus/pkg/engine/runtime"
    14  	"github.com/GuanceCloud/platypus/pkg/errchain"
    15  )
    16  
    17  func UserAgentChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    18  	if len(funcExpr.Param) != 1 {
    19  		return runtime.NewRunError(ctx, fmt.Sprintf(
    20  			"func `%s' expects 1 args", funcExpr.Name), funcExpr.NamePos)
    21  	}
    22  	if _, err := getKeyName(funcExpr.Param[0]); err != nil {
    23  		return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos())
    24  	}
    25  	return nil
    26  }
    27  
    28  func UserAgent(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    29  	if len(funcExpr.Param) != 1 {
    30  		return runtime.NewRunError(ctx, fmt.Sprintf(
    31  			"func `%s' expects 1 args", funcExpr.Name), funcExpr.NamePos)
    32  	}
    33  
    34  	key, err := getKeyName(funcExpr.Param[0])
    35  	if err != nil {
    36  		return runtime.NewRunError(ctx, err.Error(), funcExpr.Param[0].StartPos())
    37  	}
    38  
    39  	cont, err := ctx.GetKeyConv2Str(key)
    40  	if err != nil {
    41  		l.Debugf("key `%v' not exist, ignored", key)
    42  		return nil //nolint:nilerr
    43  	}
    44  
    45  	dic, dicT := UserAgentHandle(cont)
    46  
    47  	for k, val := range dic {
    48  		if dtype, ok := dicT[k]; ok {
    49  			_ = addKey2PtWithVal(ctx.InData(), k, val, dtype, ptinput.KindPtDefault)
    50  		}
    51  	}
    52  
    53  	return nil
    54  }