Skip to content

Commit ce14b0d

Browse files
committed
sync
1 parent 28c0009 commit ce14b0d

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

stream/cmd/command.go

+25-7
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,36 @@ import (
1717
)
1818

1919
type Session struct {
20-
dir string
2120
pid chan int
2221
logWriter io.Writer
22+
*exec.Cmd
23+
Result string
24+
CurrentDirectory string
2325
}
2426

2527
func MakeArg(arg ...string) string {
2628
args := make([]string, 0)
2729
args = append(args, arg...)
2830
return strings.Join(args, " ")
2931
}
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
3337
}
3438

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) }
3750
func (s *Session) SetLog(wr io.Writer) { s.logWriter = wr }
3851
func (s *Session) GetPid() <-chan int { return s.pid }
3952
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) {
4457
return exec.Command("bash", "-c", command) //"linux", "darwin", "freebsd":
4558
}
4659
cmd := fnInitCmd()
60+
s.Cmd = cmd
61+
dir, err := os.Getwd()
62+
if !mylog.Error(err) {
63+
return
64+
}
65+
s.CurrentDirectory = dir
4766

48-
cmd.Dir = s.dir
4967
mylog.Warning("go-command", cmd.String())
5068
outputErr := &bytes.Buffer{}
5169
outputOut := &bytes.Buffer{}

0 commit comments

Comments
 (0)