From ba73c535a3b41a8ed2aec0f50cfb181343a952cb Mon Sep 17 00:00:00 2001 From: Dax Raad Date: Thu, 12 Sep 2024 17:15:30 -0400 Subject: [PATCH] dev: add .sst/log/function.log --- cmd/sst/mosaic/ui/ui.go | 23 +++++++++++++++++++++-- cmd/sst/ui.go | 19 +++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/cmd/sst/mosaic/ui/ui.go b/cmd/sst/mosaic/ui/ui.go index 814db0bef..503b33c77 100644 --- a/cmd/sst/mosaic/ui/ui.go +++ b/cmd/sst/mosaic/ui/ui.go @@ -54,10 +54,12 @@ type UI struct { hasBlank bool hasHeader bool options *Options + log *os.File } type Options struct { Silent bool + Log *os.File Dev bool } @@ -71,6 +73,12 @@ func WithDev(u *Options) { u.Dev = true } +func WithLog(file *os.File) Option { + return func(opts *Options) { + opts.Log = file + } +} + func New(ctx context.Context, options ...Option) *UI { opts := &Options{} for _, option := range options { @@ -84,6 +92,9 @@ func New(ctx context.Context, options ...Option) *UI { hasBlank: false, options: opts, } + if opts.Log != nil { + result.log = opts.Log + } if isTTY && !opts.Silent { result.footer = NewFooter() go result.footer.Start(ctx) @@ -102,11 +113,16 @@ func (u *UI) printf(tmpl string, args ...interface{}) { func (u *UI) println(args ...interface{}) { u.buffer = append(u.buffer, args...) + line := fmt.Sprint(u.buffer...) if u.footer == nil { - fmt.Println(fmt.Sprint(u.buffer...)) + fmt.Println(line) } if u.footer != nil { - u.footer.Send(lineMsg(fmt.Sprint(u.buffer...))) + u.footer.Send(lineMsg(line)) + } + if u.log != nil { + stripped := ansi.Strip(line) + u.log.WriteString(stripped + "\n") } u.buffer = []interface{}{} u.hasBlank = false @@ -523,6 +539,9 @@ func (u *UI) Destroy() { if u.footer != nil { u.footer.Destroy() } + if u.log != nil { + u.log.Close() + } } func (u *UI) header(version, app, stage string) { diff --git a/cmd/sst/ui.go b/cmd/sst/ui.go index ee4a3e5fc..9d3f2fa02 100644 --- a/cmd/sst/ui.go +++ b/cmd/sst/ui.go @@ -3,6 +3,7 @@ package main import ( "fmt" "log/slog" + "os" "github.com/pulumi/pulumi/sdk/v3/go/common/apitype" "github.com/sst/ion/cmd/sst/cli" @@ -23,7 +24,14 @@ func CmdUI(c *cli.Cli) error { } types := []interface{}{} filter := c.String("filter") + var u *ui.UI + opts := []ui.Option{ + ui.WithDev, + } if filter == "function" || filter == "" { + if err != nil { + return err + } if filter != "" { fmt.Println(ui.TEXT_HIGHLIGHT_BOLD.Render("Function Logs")) fmt.Println() @@ -42,6 +50,7 @@ func CmdUI(c *cli.Cli) error { ) } if filter == "sst" || filter == "" { + u = ui.New(c.Context, ui.WithDev) types = append(types, common.StdoutEvent{}, deployer.DeployFailedEvent{}, @@ -60,8 +69,14 @@ func CmdUI(c *cli.Cli) error { if err != nil { return err } - - u := ui.New(c.Context, ui.WithDev) + if filter == "function" { + log, err := os.Create(".sst/log/function.log") + if err != nil { + return err + } + opts = append(opts, ui.WithLog(log)) + } + u = ui.New(c.Context, opts...) slog.Info("initialized ui") if filter == "sst" || filter == "" { err = dev.Deploy(c.Context, url)