sourcegraph/dev/sg/tail/tail.go
Jean-Hadrien Chabran bc4acd1fbd
feat(local): sg tail (#64146)
This PR brings back https://github.com/sourcegraph/sgtail back in `sg`,
plus a few adjustments to make it easier to use. I'll archive that repo
once this PR lands.

@camdencheek mentioned you here as you've been the most recent beta
tester, it's more an FYI than a request for a review, though it's
welcome if you want to spend a bit of time reading this.

Closes DINF-155

## Test plan

Locally tested + new unit test + CI

## Changelog

- Adds a new `sg tail` command that provides a better UI to tail and
filter log messages from `sg start --tail`.
2024-07-30 14:03:27 +02:00

63 lines
1.4 KiB
Go

package tail
import (
"net"
"os"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/sourcegraph/sourcegraph/dev/sg/internal/category"
"github.com/urfave/cli/v2"
)
var Command = &cli.Command{
Name: "tail",
Usage: "Listens for 'sg start' log events and streams them with a nice UI",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "only-name",
Usage: "--only-name [service_name] Starts with a new tab that display only logs from service named [service_name]",
Value: "",
},
},
Category: category.Dev,
Action: func(cctx *cli.Context) error {
l, err := net.Listen("unix", "/tmp/sg.sock")
if err != nil {
panic(err)
}
defer func() {
_ = os.Remove("/tmp/sg.sock")
}()
m := model{
ch: make(chan string, 10),
l: l,
tabs: []*tab{
{title: "all", preds: []activityPred{}},
},
promptInput: textinput.New(),
help: help.New(),
}
if cctx.String("only-name") != "" {
onlyCmd := commandMsg{
name: "only",
args: []string{"name", cctx.String("only-name")},
}
m.tabs = append(m.tabs, &tab{title: "^" + cctx.String("only-name"), preds: []activityPred{onlyCmd.toPred()}})
m.tabIndex = len(m.tabs) - 1
}
p := tea.NewProgram(
m,
tea.WithAltScreen(),
tea.WithMouseCellMotion(),
)
_, err = p.Run()
return err
},
}