@@ -17,23 +17,36 @@ import (
17
17
)
18
18
19
19
type Session struct {
20
- dir string
21
20
pid chan int
22
21
logWriter io.Writer
22
+ * exec.Cmd
23
+ Result string
24
+ CurrentDirectory string
23
25
}
24
26
25
27
func MakeArg (arg ... string ) string {
26
28
args := make ([]string , 0 )
27
29
args = append (args , arg ... )
28
30
return strings .Join (args , " " )
29
31
}
30
- func RunArgs (arg ... string ) string { return Run (MakeArg (arg ... )) }
31
- func Run (command string ) string {
32
- return NewSession ().run (context .Background (), command )
32
+ func RunArgs (arg ... string ) * Session { return Run (MakeArg (arg ... )) }
33
+ func Run (command string ) * Session {
34
+ session := NewSession ()
35
+ session .Result = session .run (context .Background (), command )
36
+ return session
33
37
}
34
38
35
- func NewSession () * Session { return & Session {pid : make (chan int , 1 )} }
36
- func (s * Session ) SetDir (dir string ) { s .dir = strings .TrimSpace (dir ) }
39
+ func NewSession () * Session {
40
+ return & Session {
41
+ pid : make (chan int , 1 ),
42
+ logWriter : nil ,
43
+ Cmd : nil ,
44
+ Result : "" ,
45
+ CurrentDirectory : "" ,
46
+ }
47
+ }
48
+
49
+ func (s * Session ) SetDir (dir string ) { s .Dir = strings .TrimSpace (dir ) }
37
50
func (s * Session ) SetLog (wr io.Writer ) { s .logWriter = wr }
38
51
func (s * Session ) GetPid () <- chan int { return s .pid }
39
52
func (s * Session ) run (ctx context.Context , command string ) (ss string ) {
@@ -44,8 +57,13 @@ func (s *Session) run(ctx context.Context, command string) (ss string) {
44
57
return exec .Command ("bash" , "-c" , command ) //"linux", "darwin", "freebsd":
45
58
}
46
59
cmd := fnInitCmd ()
60
+ s .Cmd = cmd
61
+ dir , err := os .Getwd ()
62
+ if ! mylog .Error (err ) {
63
+ return
64
+ }
65
+ s .CurrentDirectory = dir
47
66
48
- cmd .Dir = s .dir
49
67
mylog .Warning ("go-command" , cmd .String ())
50
68
outputErr := & bytes.Buffer {}
51
69
outputOut := & bytes.Buffer {}
0 commit comments