From 4dac4e9c43db78549918a3738a1d0842bab9dcec Mon Sep 17 00:00:00 2001 From: KnicKnic Date: Wed, 3 Jul 2019 21:00:55 -0700 Subject: [PATCH] cleanup code --- pkg/powershell/chelpers.go | 2 +- pkg/powershell/higherops.go | 6 +- pkg/powershell/higherops_test.go | 113 ++++++----- pkg/powershell/powershell.go | 4 +- pkg/powershell/powershellobjects.go | 1 - test_app/simple/main.go | 46 ++--- tests/a_test.go | 300 ++++++++++++++-------------- 7 files changed, 235 insertions(+), 237 deletions(-) diff --git a/pkg/powershell/chelpers.go b/pkg/powershell/chelpers.go index ce679d6..60cc4cb 100644 --- a/pkg/powershell/chelpers.go +++ b/pkg/powershell/chelpers.go @@ -68,7 +68,7 @@ func commandWchart(context uint64, cMessage *C.wchar_t, input *C.PowerShellObjec } message := makeString(cMessage) contextInterface.Callback.Callback(message, inputArr, &resultsWriter) - }else{ + } else { // glog.Info("In Command callback, failed to load context key: ", context) panic("In Command callback, failed to load context key: ") } diff --git a/pkg/powershell/higherops.go b/pkg/powershell/higherops.go index 8401866..3402d25 100644 --- a/pkg/powershell/higherops.go +++ b/pkg/powershell/higherops.go @@ -5,7 +5,7 @@ import ( ) // ExecStr - executes a commandline in powershell -func (runspace Runspace) ExecStr(commandStr string, useLocalScope bool, args... interface{}) InvokeResults { +func (runspace Runspace) ExecStr(commandStr string, useLocalScope bool, args ...interface{}) InvokeResults { command := runspace.CreateCommand() defer command.Delete() @@ -14,8 +14,8 @@ func (runspace Runspace) ExecStr(commandStr string, useLocalScope bool, args... } else { command.AddScript(commandStr, useLocalScope) } - for _,arg := range args{ - switch v := arg.(type){ + for _, arg := range args { + switch v := arg.(type) { case string: command.AddArgumentString(v) case Object: diff --git a/pkg/powershell/higherops_test.go b/pkg/powershell/higherops_test.go index 9161d77..873c1e8 100644 --- a/pkg/powershell/higherops_test.go +++ b/pkg/powershell/higherops_test.go @@ -27,9 +27,9 @@ func (c callbackTest) Callback(str string, input []Object, results CallbackResul } // PrintAndExecuteCommand executes a command in powershell and prints the results -func PrintAndExecuteCommand(runspace Runspace, command string, useLocalScope bool, args ... interface{}) { +func PrintAndExecuteCommand(runspace Runspace, command string, useLocalScope bool, args ...interface{}) { fmt.Print("Executing powershell command:", command, "\n") - results := runspace.ExecStr(command, useLocalScope, args ... ) + results := runspace.ExecStr(command, useLocalScope, args...) defer results.Close() fmt.Print("Completed Executing powershell command:", command, "\n") if !results.Success() { @@ -98,89 +98,87 @@ func Example_createRunspaceWithArgs() { defer runspace.Delete() results := runspace.ExecStr(`$args[0]; $args[1] + "changed"`, true, "string1", "string2") defer results.Close() - if(!results.Success()){ + if !results.Success() { runspace.ExecStr(`write-host $args[0]`, true, results.Exception) } results2 := runspace.ExecStr(`write-debug $($args[0] + 1); write-debug $args[1]`, false, "myString", results.Objects[1]) defer results2.Close() // Output: -// In Logging : Debug: myString1 -// In Logging : Debug: string2changed + // In Logging : Debug: myString1 + // In Logging : Debug: string2changed } - func Example_createRunspaceUsingCommand() { runspace := CreateRunspace(fmtPrintLogger{}, callbackTest{}) defer runspace.Delete() - results := runspace.ExecStr(`..\\..\\tests\\t1.ps1`, false ) + results := runspace.ExecStr(`..\\..\\tests\\t1.ps1`, false) defer results.Close() - results2 := runspace.ExecStr(`..\\..\\tests\\t2.ps1`, false ) + results2 := runspace.ExecStr(`..\\..\\tests\\t2.ps1`, false) defer results2.Close() // Output: -// In Logging : Debug: ab ba -// In Logging : Debug: ab ba -// In Logging : Debug: ab ba -// In callback: asdfasdf -// In callback: index 0 type: System.Int32 with value: 1 -// In callback: index 1 type: System.Int32 with value: 2 -// In callback: index 2 type: System.Int32 with value: 3 -// In Logging : Debug: asdfasdf 1 2 3 -// In callback: two -// In Logging : Debug: two -// In callback: three -// In Logging : Debug: three -// In callback: four -// In callback: index 0 Object Is Null -// In callback: index 0 type: nullptr with value: nullptr -// In callback: index 1 Object Is Null -// In callback: index 1 type: nullptr with value: nullptr -// In Logging : Debug: ab four ba -// In Logging : Error: someerror -// In Logging : Debug: start t2 -// In Logging : Debug: 5 -// In Logging : Debug: 5 -// In Logging : Debug: asdf + // In Logging : Debug: ab ba + // In Logging : Debug: ab ba + // In Logging : Debug: ab ba + // In callback: asdfasdf + // In callback: index 0 type: System.Int32 with value: 1 + // In callback: index 1 type: System.Int32 with value: 2 + // In callback: index 2 type: System.Int32 with value: 3 + // In Logging : Debug: asdfasdf 1 2 3 + // In callback: two + // In Logging : Debug: two + // In callback: three + // In Logging : Debug: three + // In callback: four + // In callback: index 0 Object Is Null + // In callback: index 0 type: nullptr with value: nullptr + // In callback: index 1 Object Is Null + // In callback: index 1 type: nullptr with value: nullptr + // In Logging : Debug: ab four ba + // In Logging : Error: someerror + // In Logging : Debug: start t2 + // In Logging : Debug: 5 + // In Logging : Debug: 5 + // In Logging : Debug: asdf } - func Example_globalScope() { runspace := CreateRunspace(fmtPrintLogger{}, callbackTest{}) defer runspace.Delete() - results := runspace.ExecStr(`..\\..\\tests\\test_scope.ps1`, false ) + results := runspace.ExecStr(`..\\..\\tests\\test_scope.ps1`, false) defer results.Close() - results2 := runspace.ExecStr(`..\\..\\tests\\test_scope.ps1`, false ) + results2 := runspace.ExecStr(`..\\..\\tests\\test_scope.ps1`, false) defer results2.Close() // Output: -// In Logging : Debug: ab ba -// In Logging : Debug: ab ba -// In Logging : Debug: ab ba -// In Logging : Debug: ab ba -// In Logging : Debug: ab 1 ba -// In Logging : Debug: ab 2 ba -// In Logging : Debug: ab 3 ba -// In Logging : Debug: ab 4 ba + // In Logging : Debug: ab ba + // In Logging : Debug: ab ba + // In Logging : Debug: ab ba + // In Logging : Debug: ab ba + // In Logging : Debug: ab 1 ba + // In Logging : Debug: ab 2 ba + // In Logging : Debug: ab 3 ba + // In Logging : Debug: ab 4 ba } func Example_localScope() { runspace := CreateRunspace(fmtPrintLogger{}, callbackTest{}) defer runspace.Delete() - results := runspace.ExecStr(`..\\..\\tests\\test_scope.ps1`, true ) + results := runspace.ExecStr(`..\\..\\tests\\test_scope.ps1`, true) defer results.Close() - results2 := runspace.ExecStr(`..\\..\\tests\\test_scope.ps1`, true ) + results2 := runspace.ExecStr(`..\\..\\tests\\test_scope.ps1`, true) defer results2.Close() // Output: -// In Logging : Debug: ab ba -// In Logging : Debug: ab ba -// In Logging : Debug: ab ba -// In Logging : Debug: ab ba -// In Logging : Debug: ab ba -// In Logging : Debug: ab ba -// In Logging : Debug: ab 3 ba -// In Logging : Debug: ab ba + // In Logging : Debug: ab ba + // In Logging : Debug: ab ba + // In Logging : Debug: ab ba + // In Logging : Debug: ab ba + // In Logging : Debug: ab ba + // In Logging : Debug: ab ba + // In Logging : Debug: ab 3 ba + // In Logging : Debug: ab ba } type callbackAddRef struct{} @@ -194,11 +192,12 @@ func (c callbackAddRef) Callback(str string, input []Object, results CallbackRes func Example_callbackWriteTrue() { runspace := CreateRunspace(fmtPrintLogger{}, callbackAddRef{}) defer runspace.Delete() - results := runspace.ExecStr(`1 | send-hostcommand -message 'empty'`, true ) + results := runspace.ExecStr(`1 | send-hostcommand -message 'empty'`, true) defer results.Close() // Output: } -type callbackAddRefSave struct{ + +type callbackAddRefSave struct { objects *[]Object } @@ -212,11 +211,11 @@ func Example_callbackSaveObject() { callback.objects = &([]Object{}) runspace := CreateRunspace(fmtPrintLogger{}, callback) defer runspace.Delete() - results := runspace.ExecStr(`1 | send-hostcommand -message 'empty'`, true ) + results := runspace.ExecStr(`1 | send-hostcommand -message 'empty'`, true) defer results.Close() - results2 := runspace.ExecStr(`write-host $args[0]`, true, (*callback.objects)[0] ) + results2 := runspace.ExecStr(`write-host $args[0]`, true, (*callback.objects)[0]) defer results2.Close() - for _,object := range *callback.objects{ + for _, object := range *callback.objects { object.Close() } // Output: In Logging : Debug: 1 diff --git a/pkg/powershell/powershell.go b/pkg/powershell/powershell.go index 8479f3a..298e865 100644 --- a/pkg/powershell/powershell.go +++ b/pkg/powershell/powershell.go @@ -80,8 +80,8 @@ func (command Command) AddArgumentString(argument string) { } // AddArgument add a Object argument to an existing powershell command -func (command Command) AddArgument(objects ... Object) { - for _,object := range(objects){ +func (command Command) AddArgument(objects ...Object) { + for _, object := range objects { _ = C.AddPSObjectArgument(command.handle, object.handle) } } diff --git a/pkg/powershell/powershellobjects.go b/pkg/powershell/powershellobjects.go index 1e2e04f..a4beef4 100644 --- a/pkg/powershell/powershellobjects.go +++ b/pkg/powershell/powershellobjects.go @@ -39,7 +39,6 @@ func (obj Object) toCHandle() C.PowerShellObject { // return cHandles // } - // Close allows the memory for the powershell object to be reclaimed // // Should be called on all objects returned from Command.Invoke unless you have called CallbackResultsWriter.Write() with autoclose diff --git a/test_app/simple/main.go b/test_app/simple/main.go index e0670e1..cc59d3d 100644 --- a/test_app/simple/main.go +++ b/test_app/simple/main.go @@ -1,23 +1,23 @@ -package main - -import ( - "fmt" - "github.com/KnicKnic/go-powershell/pkg/powershell" -) - -func main() { - // create a runspace (where you run your powershell statements in) - runspace := powershell.CreateRunspaceSimple() - // auto cleanup your runspace - defer runspace.Delete() - - // execute a statement in powershell consisting of "emit this string" - // this will output that object into the results - results := runspace.ExecStr( `"emit this string"`, true) - // auto cleanup all results returned - defer results.Close() - - // print the string result of the first object - fmt.Println(results.Objects[0].ToString()) - // Output: emit this string -} +package main + +import ( + "fmt" + "github.com/KnicKnic/go-powershell/pkg/powershell" +) + +func main() { + // create a runspace (where you run your powershell statements in) + runspace := powershell.CreateRunspaceSimple() + // auto cleanup your runspace + defer runspace.Delete() + + // execute a statement in powershell consisting of "emit this string" + // this will output that object into the results + results := runspace.ExecStr(`"emit this string"`, true) + // auto cleanup all results returned + defer results.Close() + + // print the string result of the first object + fmt.Println(results.Objects[0].ToString()) + // Output: emit this string +} diff --git a/tests/a_test.go b/tests/a_test.go index daf2e44..1634095 100644 --- a/tests/a_test.go +++ b/tests/a_test.go @@ -1,150 +1,150 @@ -package tests - -import ( - "fmt" - "github.com/KnicKnic/go-powershell/pkg/powershell" -) - -// GLogInfoLogger is a simple struct that provides ability to send logs to glog at Info level -type fmtPrintLogger struct { -} - -func (logger fmtPrintLogger) Write(arg string) { - fmt.Print(" In Logging : ", arg) -} - -type callbackTest struct{} - -func (c callbackTest) Callback(str string, input []powershell.Object, results powershell.CallbackResultsWriter) { - fmt.Println(" In callback:", str) - results.WriteString(str) - for i, object := range input { - if object.IsNull() { - fmt.Println(" In callback: index", i, "Object Is Null") // ToString and Type are still valid - } - fmt.Println(" In callback: index", i, "type:", object.Type(), "with value:", object.ToString()) - results.Write(object, false) - } -} - -// PrintAndExecuteCommand executes a command in powershell and prints the results -func PrintAndExecuteCommand(runspace powershell.Runspace, command string, useLocalScope bool) { - fmt.Print("Executing powershell command:", command, "\n") - results := runspace.ExecStr(command, useLocalScope) - defer results.Close() - fmt.Print("Completed Executing powershell command:", command, "\n") - if !results.Success() { - fmt.Println(" Command threw exception of type", results.Exception.Type(), "and ToString", results.Exception.ToString()) - } else { - fmt.Println("Command returned", len(results.Objects), "objects") - for i, object := range results.Objects { - fmt.Println(" Object", i, "is of type", object.Type(), "and ToString", object.ToString()) - } - } -} - -// // Example on how to use powershell wrappers -// func TestFoo(*testing.T) { -// runspace := powershell.CreateRunspace(fmtPrintLogger{}, callbackTest{}) -// // runspace := CreateRunspaceSimple() -// defer runspace.Delete() - -// PrintAndExecuteCommand(runspace, ".\\t1.ps1", false) -// PrintAndExecuteCommand(runspace, ".\\t2.ps1", false) -// } - -// func ExampleCreateRunspaceSimple(){ - -// runspace := powershell.CreateRunspace(fmtPrintLogger{}, callbackTest{}) -// // runspace := CreateRunspaceSimple() -// defer runspace.Delete() - -// PrintAndExecuteCommand(runspace, ".\\t1.ps1", false) -// PrintAndExecuteCommand(runspace, ".\\t2.ps1", false) -// // Output: -// // Executing powershell command: .\t1.ps1 -// // In Logging : Debug: ab ba -// // In Logging : Debug: ab ba -// // In Logging : Debug: ab ba -// // In callback: asdfasdf -// // In callback: index 0 type: System.Int32 with value: 1 -// // In callback: index 1 type: System.Int32 with value: 2 -// // In callback: index 2 type: System.Int32 with value: 3 -// // In Logging : Debug: asdfasdf 1 2 3 -// // In callback: two -// // In Logging : Debug: two -// // In callback: three -// // In Logging : Debug: three -// // In callback: four -// // In callback: index 0 Object Is Null -// // In callback: index 0 type: nullptr with value: nullptr -// // In callback: index 1 Object Is Null -// // In callback: index 1 type: nullptr with value: nullptr -// // In Logging : Debug: four -// // In Logging : Error: someerror -// // Completed Executing powershell command: .\t1.ps1 -// // Command returned 0 objects -// // Executing powershell command: .\t2.ps1 -// // In Logging : Debug: start t2 -// // In Logging : Debug: 5 -// // In Logging : Debug: 5 -// // In Logging : Debug: asdf -// // Completed Executing powershell command: .\t2.ps1 -// // Command returned 0 objects -// } - -func ExampleCreateRunspaceWitLoggerWithCallback(){ - - runspace := powershell.CreateRunspace(fmtPrintLogger{}, callbackTest{}) - // runspace := CreateRunspaceSimple() - defer runspace.Delete() -input_str :=` -write-host 'simple output statement' -"s1" -"s2"` - PrintAndExecuteCommand(runspace, input_str, false) - // Output: -// Executing powershell command: -// write-host 'simple output statement' -// "s1" -// "s2" -// In Logging : Debug: simple output statement -// Completed Executing powershell command: -// write-host 'simple output statement' -// "s1" -// "s2" -// Command returned 2 objects -// Object 0 is of type System.String and ToString s1 -// Object 1 is of type System.String and ToString s2 -} - -// func ExampleCreateRunspaceSimple(){ -// // ensure directory is gone -// _ = os.Remove("ExampleCreateRunspaceSimple") - -// // show directory is gone -// printDirectoryStatus := func (){ -// _ = os.stat("ExampleCreateRunspaceSimple") -// if _, err := os.Stat(path); os.IsNotExist(err) { -// fmt.Println("ExampleCreateRunspaceSimple does not exist") -// }else { -// fmt.Println("ExampleCreateRunspaceSimple exists") -// } -// } - -// runspace := powershell.CreateRunspaceSimple() -// defer runspace.Delete() -// runspace.ExecStr( `[System.Console]::WriteLine("hi")`, true) -// // Output: hi -// } - - -func ExampleCreateRunspaceSimple(){ - runspace := powershell.CreateRunspaceSimple() - defer runspace.Delete() - results := runspace.ExecStr( `"emit this string"`, true) - defer results.Close() - // print the string result of the first object - fmt.Println(results.Objects[0].ToString()) - // Output: emit this string -} \ No newline at end of file +package tests + +import ( + "fmt" + + "github.com/KnicKnic/go-powershell/pkg/powershell" +) + +// GLogInfoLogger is a simple struct that provides ability to send logs to glog at Info level +type fmtPrintLogger struct { +} + +func (logger fmtPrintLogger) Write(arg string) { + fmt.Print(" In Logging : ", arg) +} + +type callbackTest struct{} + +func (c callbackTest) Callback(str string, input []powershell.Object, results powershell.CallbackResultsWriter) { + fmt.Println(" In callback:", str) + results.WriteString(str) + for i, object := range input { + if object.IsNull() { + fmt.Println(" In callback: index", i, "Object Is Null") // ToString and Type are still valid + } + fmt.Println(" In callback: index", i, "type:", object.Type(), "with value:", object.ToString()) + results.Write(object, false) + } +} + +// PrintAndExecuteCommand executes a command in powershell and prints the results +func PrintAndExecuteCommand(runspace powershell.Runspace, command string, useLocalScope bool) { + fmt.Print("Executing powershell command:", command, "\n") + results := runspace.ExecStr(command, useLocalScope) + defer results.Close() + fmt.Print("Completed Executing powershell command:", command, "\n") + if !results.Success() { + fmt.Println(" Command threw exception of type", results.Exception.Type(), "and ToString", results.Exception.ToString()) + } else { + fmt.Println("Command returned", len(results.Objects), "objects") + for i, object := range results.Objects { + fmt.Println(" Object", i, "is of type", object.Type(), "and ToString", object.ToString()) + } + } +} + +// // Example on how to use powershell wrappers +// func TestFoo(*testing.T) { +// runspace := powershell.CreateRunspace(fmtPrintLogger{}, callbackTest{}) +// // runspace := CreateRunspaceSimple() +// defer runspace.Delete() + +// PrintAndExecuteCommand(runspace, ".\\t1.ps1", false) +// PrintAndExecuteCommand(runspace, ".\\t2.ps1", false) +// } + +// func ExampleCreateRunspaceSimple(){ + +// runspace := powershell.CreateRunspace(fmtPrintLogger{}, callbackTest{}) +// // runspace := CreateRunspaceSimple() +// defer runspace.Delete() + +// PrintAndExecuteCommand(runspace, ".\\t1.ps1", false) +// PrintAndExecuteCommand(runspace, ".\\t2.ps1", false) +// // Output: +// // Executing powershell command: .\t1.ps1 +// // In Logging : Debug: ab ba +// // In Logging : Debug: ab ba +// // In Logging : Debug: ab ba +// // In callback: asdfasdf +// // In callback: index 0 type: System.Int32 with value: 1 +// // In callback: index 1 type: System.Int32 with value: 2 +// // In callback: index 2 type: System.Int32 with value: 3 +// // In Logging : Debug: asdfasdf 1 2 3 +// // In callback: two +// // In Logging : Debug: two +// // In callback: three +// // In Logging : Debug: three +// // In callback: four +// // In callback: index 0 Object Is Null +// // In callback: index 0 type: nullptr with value: nullptr +// // In callback: index 1 Object Is Null +// // In callback: index 1 type: nullptr with value: nullptr +// // In Logging : Debug: four +// // In Logging : Error: someerror +// // Completed Executing powershell command: .\t1.ps1 +// // Command returned 0 objects +// // Executing powershell command: .\t2.ps1 +// // In Logging : Debug: start t2 +// // In Logging : Debug: 5 +// // In Logging : Debug: 5 +// // In Logging : Debug: asdf +// // Completed Executing powershell command: .\t2.ps1 +// // Command returned 0 objects +// } + +func Example_createRunspaceWitLoggerWithCallback() { + + runspace := powershell.CreateRunspace(fmtPrintLogger{}, callbackTest{}) + // runspace := CreateRunspaceSimple() + defer runspace.Delete() + inputStr := ` +write-host 'simple output statement' +"s1" +"s2"` + PrintAndExecuteCommand(runspace, inputStr, false) + // Output: + // Executing powershell command: + // write-host 'simple output statement' + // "s1" + // "s2" + // In Logging : Debug: simple output statement + // Completed Executing powershell command: + // write-host 'simple output statement' + // "s1" + // "s2" + // Command returned 2 objects + // Object 0 is of type System.String and ToString s1 + // Object 1 is of type System.String and ToString s2 +} + +// func ExampleCreateRunspaceSimple(){ +// // ensure directory is gone +// _ = os.Remove("ExampleCreateRunspaceSimple") + +// // show directory is gone +// printDirectoryStatus := func (){ +// _ = os.stat("ExampleCreateRunspaceSimple") +// if _, err := os.Stat(path); os.IsNotExist(err) { +// fmt.Println("ExampleCreateRunspaceSimple does not exist") +// }else { +// fmt.Println("ExampleCreateRunspaceSimple exists") +// } +// } + +// runspace := powershell.CreateRunspaceSimple() +// defer runspace.Delete() +// runspace.ExecStr( `[System.Console]::WriteLine("hi")`, true) +// // Output: hi +// } + +func Example_createRunspaceSimple() { + runspace := powershell.CreateRunspaceSimple() + defer runspace.Delete() + results := runspace.ExecStr(`"emit this string"`, true) + defer results.Close() + // print the string result of the first object + fmt.Println(results.Objects[0].ToString()) + // Output: emit this string +}