Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(logs): dumped logs while running #45

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Logs
logs
*.log
*.err
npm-debug.log*
yarn-debug.log*
yarn-error.log*
Expand Down
13 changes: 9 additions & 4 deletions pkg/executor/executor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package executor

import (
"fmt"
"io"

"github.com/go-cmd/cmd"
"github.com/google/shlex"
)
Expand All @@ -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

Expand All @@ -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() {
Expand All @@ -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{}{}
}
Expand Down
16 changes: 15 additions & 1 deletion pkg/ui/flat/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package flat
import (
"CLI2UI/pkg/config"
"CLI2UI/pkg/ui"
"fmt"
"os"
"time"

"github.com/labstack/gommon/log"
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
16 changes: 15 additions & 1 deletion pkg/ui/naive/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package naive

import (
"CLI2UI/pkg/ui"
"fmt"
"os"
"time"

"github.com/labstack/gommon/log"
Expand Down Expand Up @@ -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
Expand All @@ -77,6 +89,8 @@ func (u UI) registerEvents() {
log.Error(err)
}
case <-finishedCh:
stdoutFile.Close()
stderrFile.Close()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the meaning?

err := execState.SetState(sess.Exec.State, &connId)
if err != nil {
log.Error(err)
Expand Down