github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_use.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/platypus/pkg/ast"
    12  	"github.com/GuanceCloud/platypus/pkg/engine/runtime"
    13  	"github.com/GuanceCloud/platypus/pkg/errchain"
    14  )
    15  
    16  func UseChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    17  	if len(funcExpr.Param) != 1 {
    18  		return runtime.NewRunError(ctx, fmt.Sprintf(
    19  			"func %s expects 1 args", funcExpr.Name), funcExpr.NamePos)
    20  	}
    21  
    22  	switch funcExpr.Param[0].NodeType { //nolint:exhaustive
    23  	case ast.TypeStringLiteral:
    24  		ctx.SetCallRef(funcExpr)
    25  	default:
    26  		return runtime.NewRunError(ctx, fmt.Sprintf("param key expects StringLiteral, got %s",
    27  			funcExpr.Param[0].NodeType), funcExpr.Param[0].StartPos())
    28  	}
    29  
    30  	return nil
    31  }
    32  
    33  func Use(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    34  	if len(funcExpr.Param) != 1 {
    35  		return runtime.NewRunError(ctx, fmt.Sprintf(
    36  			"func %s expects 1 args", funcExpr.Name), funcExpr.NamePos)
    37  	}
    38  
    39  	var refScript *runtime.Script
    40  	switch funcExpr.Param[0].NodeType { //nolint:exhaustive
    41  	case ast.TypeStringLiteral:
    42  		if funcExpr.PrivateData != nil {
    43  			if s, ok := funcExpr.PrivateData.(*runtime.Script); ok {
    44  				refScript = s
    45  			} else {
    46  				l.Debugf("unknown error: %s", funcExpr.Param[0].StringLiteral().Val)
    47  				return nil
    48  			}
    49  		} else {
    50  			l.Debugf("script not found: %s", funcExpr.Param[0].StringLiteral().Val)
    51  			return nil
    52  		}
    53  	default:
    54  		return runtime.NewRunError(ctx, fmt.Sprintf(
    55  			"param key expects StringLiteral, got %s",
    56  			funcExpr.Param[0].NodeType), funcExpr.Param[0].StartPos())
    57  	}
    58  
    59  	if err := refScript.RefRun(ctx); err != nil {
    60  		return err.ChainAppend(ctx.Name(), funcExpr.NamePos)
    61  	}
    62  	return nil
    63  }