github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/engine/function/csharpfuncs.go (about)

     1  package funcs
     2  
     3  import (
     4  	//	"bytes"
     5  	//	"encoding/json"
     6  	"encoding/json"
     7  	"fmt"
     8  	"log"
     9  	"os"
    10  	"time"
    11  
    12  	//	"os"
    13  	"os/exec"
    14  	//	"reflect"
    15  	"github.com/mdaxf/iac/logger"
    16  )
    17  
    18  type CSharpFuncs struct {
    19  }
    20  
    21  func (cf *CSharpFuncs) Execute(f *Funcs) {
    22  	startTime := time.Now()
    23  	defer func() {
    24  		elapsed := time.Since(startTime)
    25  		f.iLog.PerformanceWithDuration("engine.func.CSharpFuncs.Execute", elapsed)
    26  	}()
    27  	defer func() {
    28  		if err := recover(); err != nil {
    29  			f.iLog.Error(fmt.Sprintf("There is error to engine.func.CSharpFuncs.Execute with error: %s", err))
    30  			return
    31  		}
    32  	}()
    33  	namelist, _, inputs := f.SetInputs()
    34  
    35  	cmdArgs := []string{"-c", f.Fobj.Content}
    36  	for i := range namelist {
    37  
    38  		cmdArgs = append(cmdArgs, fmt.Sprintf("-p:%s=%s", namelist[i], inputs[namelist[i]]))
    39  	}
    40  	cmd := exec.Command("dotnet", cmdArgs...)
    41  
    42  	// Capture standard output and error
    43  	output, err := cmd.CombinedOutput()
    44  	if err != nil {
    45  		log.Fatal(err)
    46  	}
    47  	// Decode the output object from the command output
    48  	f.SetOutputs(f.ConvertfromBytes(output))
    49  }
    50  
    51  func (cf *CSharpFuncs) Validate(f *Funcs) (bool, error) {
    52  	startTime := time.Now()
    53  	defer func() {
    54  		elapsed := time.Since(startTime)
    55  		f.iLog.PerformanceWithDuration("engine.func.CSharpFuncs.Validate", elapsed)
    56  	}()
    57  	defer func() {
    58  		if err := recover(); err != nil {
    59  			f.iLog.Error(fmt.Sprintf("There is error to engine.func.CSharpFuncs.Validate with error: %s", err))
    60  			return
    61  		}
    62  	}()
    63  
    64  	return true, nil
    65  }
    66  
    67  func (cf *CSharpFuncs) Testfunction(content string, inputs interface{}, outputs []string) (map[string]interface{}, error) {
    68  	iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "CSharp Function"}
    69  	startTime := time.Now()
    70  	defer func() {
    71  		elapsed := time.Since(startTime)
    72  		iLog.PerformanceWithDuration("engine.func.CSharpFuncs.Testfunction", elapsed)
    73  	}()
    74  	defer func() {
    75  		if err := recover(); err != nil {
    76  			iLog.Error(fmt.Sprintf("There is error to engine.func.CSharpFuncs.Testfunction with error: %s", err))
    77  			return
    78  		}
    79  	}()
    80  
    81  	iLog.Debug(fmt.Sprintf("Test Exec Function"))
    82  
    83  	/* pass, err := cf.Validate(f)
    84  
    85  	if !pass {
    86  		return nil, err
    87  	}
    88  	*/
    89  	namelist := make([]string, 0)
    90  	valuelist := make([]interface{}, 0)
    91  
    92  	for key, value := range inputs.(map[string]interface{}) {
    93  		namelist = append(namelist, key)
    94  		valuelist = append(valuelist, value)
    95  	}
    96  	iLog.Debug(fmt.Sprintf("Test Exec Function namelist: %s valuelist:%v", namelist, valuelist))
    97  	iLog.Debug(fmt.Sprintf("Test Exec Function content: %s", content))
    98  
    99  	cmdArgs := []string{"-c", content}
   100  	for i := range namelist {
   101  
   102  		cmdArgs = append(cmdArgs, fmt.Sprintf("-p:%s=%s", namelist[i], valuelist[i]))
   103  	}
   104  
   105  	iLog.Debug(fmt.Sprintf("Test Exec Function cmdArgs: %s", cmdArgs))
   106  
   107  	cmd := exec.Command("dotnet", cmdArgs...)
   108  
   109  	iLog.Debug(fmt.Sprintf("Test Exec Function cmd: %s", cmd))
   110  
   111  	cmd.Stdin = os.Stdin
   112  	cmd.Stdout = os.Stdout
   113  	cmd.Stderr = os.Stderr
   114  
   115  	err := cmd.Run()
   116  	if err != nil {
   117  		iLog.Error(fmt.Sprintf("Test Exec Function error: %s", err.Error()))
   118  		return nil, err
   119  	}
   120  
   121  	// Capture standard output and error
   122  	combinedoutput, err := cmd.CombinedOutput()
   123  	if err != nil {
   124  		log.Fatal(err)
   125  	}
   126  	iLog.Debug(fmt.Sprintf("Test Exec Function combinedoutput: %s", combinedoutput))
   127  	var functionoutputs map[string]interface{}
   128  
   129  	err = json.Unmarshal(combinedoutput, &functionoutputs)
   130  	if err != nil {
   131  		fmt.Println("Error:", err)
   132  		return nil, err
   133  	}
   134  
   135  	return functionoutputs, nil
   136  }