Skip to content

Commit

Permalink
implement restart
Browse files Browse the repository at this point in the history
marhaupe committed Sep 6, 2024

Unverified

This user has not yet uploaded their public signing key.
1 parent bccf1ce commit 7fcfac3
Showing 4 changed files with 73 additions and 12 deletions.
5 changes: 4 additions & 1 deletion backlog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
Features:

- Restart session (need to prototype this, maybe we need to change the API, e.g. uitail "nx run website:serve")
- Bisect why nx run website:serve doesn't work in the cli anymore
- Test: Restart session (need to prototype this, maybe we need to change the API, e.g. uitail "nx run website:serve")
- In particular environment variables
- persist filter state in URL
- filter out messages
- figure out what to do with the histogram
- fix flicker when typing
- unify log / fmt.Println usage in cli
2 changes: 1 addition & 1 deletion packages/cli/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
dev:
node testlogger.js 2>&1 | go run main.go
go run main.go "node testlogger.js"

ui:
rm -rf internal/static/dist
77 changes: 68 additions & 9 deletions packages/cli/app/cmd/root.go
Original file line number Diff line number Diff line change
@@ -4,9 +4,11 @@ import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"syscall"
"time"
@@ -21,10 +23,15 @@ import (
var rootCmd = &cobra.Command{
Use: "uitail",
Short: "uitail is a tail-like tool with a beautiful UI",
Example: "ping google.com 2>&1 | uitail",
Example: "uitail \"ping google.com\"",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
root := New()
root.Start()
root := New(args[0])
err := root.Start()
if err != nil {
fmt.Println("error starting uitail", err)
os.Exit(1)
}
},
}

@@ -48,25 +55,62 @@ type Root struct {
staticServer *static.Static
in *bufio.Scanner
line []byte
command string
cmd *exec.Cmd
}

type Log struct {
Timestamp time.Time `json:"timestamp"`
Message string `json:"message"`
}

func New() *Root {
in := os.Stdin
scanner := bufio.NewScanner(in)
scanner.Split(bufio.ScanBytes)
func New(command string) *Root {
return &Root{
in: scanner,
logService: logs.New(),
staticServer: static.New(),
command: command,
}
}

func (a *Root) startCommand() error {
if a.cmd != nil {
return errors.New("command already started")
}
a.cmd = exec.Command("bash", "-c", a.command)
stdout, err := a.cmd.StdoutPipe()
a.cmd.Stderr = a.cmd.Stdout
if err != nil {
return err
}
scanner := bufio.NewScanner(stdout)
scanner.Split(bufio.ScanBytes)
a.in = scanner
go func() {
if err := a.cmd.Run(); err != nil {
log.Printf("error running command: %s", err)
}
}()
return nil
}

func (a *Root) stopCommand() error {
if a.cmd == nil {
return errors.New("command not started")
}

err := a.cmd.Process.Kill()
if err != nil {
return err
}
a.cmd = nil
return nil
}

func (a *Root) Start() error {
err := a.startCommand()
if err != nil {
return err
}
go func() {
config := iris.WithConfiguration(iris.Configuration{
DisableStartupLog: true,
@@ -86,7 +130,22 @@ func (a *Root) Start() error {
// Maybe close event service here?
})

app.Post("/restart", a.logService.RestartHandler())
app.Post("/restart", func(ctx iris.Context) {
err := a.stopCommand()
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.WriteString(fmt.Sprintf("error stopping command: %s", err))
return
}
err = a.startCommand()
if err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.WriteString(fmt.Sprintf("error starting command: %s", err))
return
}
ctx.StatusCode(iris.StatusOK)
ctx.WriteString("OK")
})
app.Any("/events", a.logService.EventHandler())
app.Post("/clear", a.logService.ClearHandler())
app.Get("/{asset:path}", a.staticServer.Handler())
1 change: 0 additions & 1 deletion packages/frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -55,7 +55,6 @@ export function App() {
}
);
if (response.ok) {
setLogs([]);
toast.success("Agent restarted");
}
} catch (error) {

0 comments on commit 7fcfac3

Please sign in to comment.