From 96bbe3e17164667b76b797a59f6ca578d810f1db Mon Sep 17 00:00:00 2001 From: Kiyoshi Guo <44930252+Kiyo5hi@users.noreply.github.com> Date: Tue, 29 Aug 2023 15:03:55 +0800 Subject: [PATCH] feat(logs): dumped logs while running --- .gitignore | 1 + pkg/executor/executor.go | 13 +++++++++---- pkg/ui/flat/events.go | 16 +++++++++++++++- pkg/ui/naive/events.go | 16 +++++++++++++++- 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index eb65772..fb222e8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Logs logs *.log +*.err npm-debug.log* yarn-debug.log* yarn-error.log* diff --git a/pkg/executor/executor.go b/pkg/executor/executor.go index a6e3230..1e48f31 100644 --- a/pkg/executor/executor.go +++ b/pkg/executor/executor.go @@ -1,6 +1,9 @@ package executor import ( + "fmt" + "io" + "github.com/go-cmd/cmd" "github.com/google/shlex" ) @@ -18,7 +21,7 @@ type state struct { IsRunning bool `json:"isRunning"` } -func (e *Executor) Run(script string) (chan struct{}, error) { +func (e *Executor) Run(stdin io.Reader, stdout io.Writer, stderr io.Writer, script string) (chan struct{}, error) { e.resetState() e.State.IsRunning = true @@ -32,7 +35,7 @@ func (e *Executor) Run(script string) (chan struct{}, error) { Streaming: true, }, frags[0], frags[1:]...) - finStatusCh := c.Start() + finStatusCh := c.StartWithStdin(stdin) stdioStatusCh := make(chan struct{}) go func() { @@ -44,13 +47,15 @@ func (e *Executor) Run(script string) (chan struct{}, error) { c.Stdout = nil continue } - e.State.Stdout = e.State.Stdout + line + "\r\n" + e.State.Stdout = fmt.Sprintln(e.State.Stdout, line) + fmt.Fprintln(stdout, line) case line, open := <-c.Stderr: if !open { c.Stderr = nil continue } - e.State.Stderr = e.State.Stderr + line + "\r\n" + e.State.Stderr = fmt.Sprintln(e.State.Stderr, line) + fmt.Fprintln(stderr, line) } e.StateCh <- struct{}{} } diff --git a/pkg/ui/flat/events.go b/pkg/ui/flat/events.go index e9356d6..e874909 100644 --- a/pkg/ui/flat/events.go +++ b/pkg/ui/flat/events.go @@ -3,6 +3,8 @@ package flat import ( "CLI2UI/pkg/config" "CLI2UI/pkg/ui" + "fmt" + "os" "time" "github.com/labstack/gommon/log" @@ -69,7 +71,17 @@ func (u UI) registerEvents() { script, f := u.CLIs[0].Script(*sess.Form) formatState.SetState(f, &connId) - finishedCh, err := sess.Exec.Run(script) + stdoutFile, err := os.OpenFile(fmt.Sprintf("stdout-%d.log", connId), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + return err + } + + stderrFile, err := os.OpenFile(fmt.Sprintf("stdout-%d.err", connId), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + return err + } + + finishedCh, err := sess.Exec.Run(nil, stdoutFile, stderrFile, script) if err != nil { log.Error(err) return err @@ -84,6 +96,8 @@ func (u UI) registerEvents() { log.Error(err) } case <-finishedCh: + stdoutFile.Close() + stderrFile.Close() err := execState.SetState(sess.Exec.State, &connId) if err != nil { log.Error(err) diff --git a/pkg/ui/naive/events.go b/pkg/ui/naive/events.go index 1ab5c0f..68de1ae 100644 --- a/pkg/ui/naive/events.go +++ b/pkg/ui/naive/events.go @@ -2,6 +2,8 @@ package naive import ( "CLI2UI/pkg/ui" + "fmt" + "os" "time" "github.com/labstack/gommon/log" @@ -62,7 +64,17 @@ func (u UI) registerEvents() { script, f := u.CLIs[0].Script(*sess.Form) formatState.SetState(f, &connId) - finishedCh, err := sess.Exec.Run(script) + stdoutFile, err := os.OpenFile(fmt.Sprintf("stdout-%d.log", connId), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + return err + } + + stderrFile, err := os.OpenFile(fmt.Sprintf("stdout-%d.err", connId), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + return err + } + + finishedCh, err := sess.Exec.Run(nil, stdoutFile, stderrFile, script) if err != nil { log.Error(err) return err @@ -77,6 +89,8 @@ func (u UI) registerEvents() { log.Error(err) } case <-finishedCh: + stdoutFile.Close() + stderrFile.Close() err := execState.SetState(sess.Exec.State, &connId) if err != nil { log.Error(err)