diff --git a/pkg/powershell/hostcommand.go b/pkg/powershell/hostcommand.go index 480133f..c2c5267 100644 --- a/pkg/powershell/hostcommand.go +++ b/pkg/powershell/hostcommand.go @@ -23,6 +23,16 @@ type CallbackHolder interface { Callback(runspace Runspace, message string, input []Object, results CallbackResultsWriter) } +// CallbackFuncPtr a simple implementation of CallbackHolder that lets you pass in a function pointer for the callback +type CallbackFuncPtr struct { + FuncPtr func(runspace Runspace, message string, input []Object, results CallbackResultsWriter) +} + +// Callback is the function that will call the function pointer in CallbackFuncPtr +func (callback CallbackFuncPtr) Callback(runspace Runspace, message string, input []Object, results CallbackResultsWriter) { + callback.FuncPtr(runspace, message, input, results) +} + // callbackResultsWriter is the internal implementation of CallbackResultsWriter type callbackResultsWriter struct { objects []C.GenericPowershellObject diff --git a/pkg/powershell/hostcommand_test.go b/pkg/powershell/hostcommand_test.go deleted file mode 100644 index 1a04477..0000000 --- a/pkg/powershell/hostcommand_test.go +++ /dev/null @@ -1,45 +0,0 @@ -package powershell - -import ( - "fmt" - "strconv" -) - -// the callback we want to add -type callbackAdd10 struct { -} - -func (callbackAdd10) Callback(runspace Runspace, str string, input []Object, results CallbackResultsWriter) { - switch str { - // check if we are processing the "add 10" message - case "add 10": - // iterate through all items passed in - for _, object := range input { - numStr := object.ToString() - num, _ := strconv.Atoi(numStr) - - // write the object back to powershell as a string - results.WriteString(fmt.Sprint(num + 10)) - } - } -} -func ExampleCallbackHolder() { - // create a runspace (where you run your powershell statements in) - runspace := CreateRunspace(nil, callbackAdd10{}) - // auto cleanup your runspace - defer runspace.Close() - - statements := `1..3 | Send-HostCommand -message "add 10"` - results := runspace.ExecScript(statements, true, nil) - // auto cleanup all results returned - defer results.Close() - - for _, num := range results.Objects { - fmt.Println(num.ToString()) - } - - // OUTPUT: - // 11 - // 12 - // 13 -} diff --git a/pkg/powershell/runspace_test.go b/pkg/powershell/runspace_test.go index 1ac728e..a41d676 100644 --- a/pkg/powershell/runspace_test.go +++ b/pkg/powershell/runspace_test.go @@ -3,6 +3,7 @@ package powershell import ( "fmt" "github.com/KnicKnic/go-powershell/pkg/logger" + "strconv" ) func ExampleRunspace_ExecScript() { @@ -143,6 +144,42 @@ func ExampleRunspace_ExecScriptJSONMarshalUnknown() { // OUTPUT: Name: Knic, Category: 4, Human: true } +func ExampleCallbackHolder() { + // create a callback object + callback := CallbackFuncPtr{func(runspace Runspace, str string, input []Object, results CallbackResultsWriter) { + switch str { + // check if we are processing the "add 10" message + case "add 10": + // iterate through all items passed in + for _, object := range input { + numStr := object.ToString() + num, _ := strconv.Atoi(numStr) + + // write the object back to powershell as a string + results.WriteString(fmt.Sprint(num + 10)) + } + } + }} + // create a runspace (where you run your powershell statements in) + runspace := CreateRunspace(nil, callback) + // auto cleanup your runspace + defer runspace.Close() + + statements := `1..3 | Send-HostCommand -message "add 10"` + results := runspace.ExecScript(statements, true, nil) + // auto cleanup all results returned + defer results.Close() + + for _, num := range results.Objects { + fmt.Println(num.ToString()) + } + + // OUTPUT: + // 11 + // 12 + // 13 +} + // func Example_powershellCommandWithNamedParametersComplex() { // // create a runspace (where you run your powershell statements in) // runspace := CreateRunspaceSimple()