Skip to content

Commit

Permalink
cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
KnicKnic committed Jul 4, 2019
1 parent 2195458 commit 4dac4e9
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 237 deletions.
2 changes: 1 addition & 1 deletion pkg/powershell/chelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: ")
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/powershell/higherops.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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:
Expand Down
113 changes: 56 additions & 57 deletions pkg/powershell/higherops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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{}
Expand All @@ -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
}

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/powershell/powershell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
1 change: 0 additions & 1 deletion pkg/powershell/powershellobjects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 23 additions & 23 deletions test_app/simple/main.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit 4dac4e9

Please sign in to comment.