Skip to content

Commit 9336f38

Browse files
committed
Add WithUser for windows
1 parent 52b464a commit 9336f38

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

command_windows.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,18 @@ package cmd
22

33
import (
44
"os/exec"
5+
"syscall"
56
)
67

78
func createBaseCommand(c *Command) *exec.Cmd {
89
cmd := exec.Command(`C:\windows\system32\cmd.exe`, "/C", c.Command)
910
return cmd
1011
}
12+
13+
func WithUser(token syscall.Token) func(c *Command) {
14+
return func(c *Command) {
15+
c.baseCommand.SysProcAttr = &syscall.SysProcAttr{
16+
Token: token,
17+
}
18+
}
19+
}

command_windows_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package cmd
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"strings"
5+
"syscall"
66
"testing"
77
"time"
8+
"unsafe"
9+
10+
"github.com/stretchr/testify/assert"
811
)
912

1013
func TestCommand_ExecuteStderr(t *testing.T) {
@@ -34,3 +37,11 @@ func TestCommand_WithValidTimeout(t *testing.T) {
3437

3538
assert.Nil(t, err)
3639
}
40+
41+
func TestCommand_WithUser(t *testing.T) {
42+
onehundred := 100
43+
token := syscall.Token(uintptr(unsafe.Pointer(&onehundred)))
44+
cmd := NewCommand("echo hello", WithUser(token))
45+
err := cmd.Execute()
46+
assert.Error(t, err)
47+
}

0 commit comments

Comments
 (0)