From dd588258d746235a083bc411775fd581947aaa42 Mon Sep 17 00:00:00 2001 From: Lukasz Zajaczkowski Date: Tue, 21 Oct 2025 12:08:14 +0200 Subject: [PATCH 1/2] add debug flag to 'plural cd services lua' command --- cmd/command/cd/cd_services.go | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/cmd/command/cd/cd_services.go b/cmd/command/cd/cd_services.go index 2bff850f..b2b59b12 100644 --- a/cmd/command/cd/cd_services.go +++ b/cmd/command/cd/cd_services.go @@ -1,6 +1,7 @@ package cd import ( + "bytes" "fmt" "path/filepath" "strings" @@ -136,6 +137,10 @@ func (p *Plural) cdServiceCommands() []cli.Command { Name: "dir", Usage: "The directory to run the lua script from, defaults to the current working directory", }, + cli.BoolFlag{ + Name: "debug", + Usage: "Prints the lua script to stdout during execution for debugging purposes", + }, }, }, { @@ -313,10 +318,6 @@ func (p *Plural) handleTemplateService(c *cli.Context) error { } func (p *Plural) handleLuaTemplate(c *cli.Context) error { - if err := p.InitConsoleClient(consoleToken, consoleURL); err != nil { - return err - } - luaFile := c.String("lua-file") context := c.String("context") dir := c.String("dir") @@ -361,10 +362,35 @@ func (p *Plural) handleLuaTemplate(c *cli.Context) error { L.SetGlobal("contexts", luautils.GoValueToLuaValue(L, ctx["contexts"])) L.SetGlobal("imports", luautils.GoValueToLuaValue(L, ctx["imports"])) + // Register a print function to print to stdout when debugging + output := &bytes.Buffer{} + L.SetGlobal("print", L.NewFunction(func(L *lua.LState) int { + top := L.GetTop() + for i := 1; i <= top; i++ { + val := L.ToStringMeta(L.Get(i)).String() + if _, err := fmt.Fprint(output, val); err != nil { + return 1 + } + if i != top { + if _, err := fmt.Fprint(output, "\t"); err != nil { + return 1 + } + } + } + if _, err := fmt.Fprintln(output); err != nil { + return 1 + } + return 0 + })) + if err := L.DoString(luaStr); err != nil { return err } + if c.Bool("debug") { + fmt.Println(output.String()) + } + if err := luautils.MapLua(L.GetGlobal("values").(*lua.LTable), &values); err != nil { return err } From b2bc7086fde162f9d2b6656216a9e32a1ee03a44 Mon Sep 17 00:00:00 2001 From: Lukasz Zajaczkowski Date: Tue, 21 Oct 2025 18:01:47 +0200 Subject: [PATCH 2/2] capture stdout --- cmd/command/cd/cd_services.go | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/cmd/command/cd/cd_services.go b/cmd/command/cd/cd_services.go index b2b59b12..b2cee983 100644 --- a/cmd/command/cd/cd_services.go +++ b/cmd/command/cd/cd_services.go @@ -3,6 +3,7 @@ package cd import ( "bytes" "fmt" + "os" "path/filepath" "strings" @@ -362,33 +363,25 @@ func (p *Plural) handleLuaTemplate(c *cli.Context) error { L.SetGlobal("contexts", luautils.GoValueToLuaValue(L, ctx["contexts"])) L.SetGlobal("imports", luautils.GoValueToLuaValue(L, ctx["imports"])) - // Register a print function to print to stdout when debugging - output := &bytes.Buffer{} - L.SetGlobal("print", L.NewFunction(func(L *lua.LState) int { - top := L.GetTop() - for i := 1; i <= top; i++ { - val := L.ToStringMeta(L.Get(i)).String() - if _, err := fmt.Fprint(output, val); err != nil { - return 1 - } - if i != top { - if _, err := fmt.Fprint(output, "\t"); err != nil { - return 1 - } - } - } - if _, err := fmt.Fprintln(output); err != nil { - return 1 - } - return 0 - })) + old := os.Stdout + r, w, _ := os.Pipe() + os.Stdout = w if err := L.DoString(luaStr); err != nil { return err } + if err := w.Close(); err != nil { + return err + } + os.Stdout = old + var buf bytes.Buffer + if _, err := buf.ReadFrom(r); err != nil { + return err + } + output := buf.String() if c.Bool("debug") { - fmt.Println(output.String()) + fmt.Println(output) } if err := luautils.MapLua(L.GetGlobal("values").(*lua.LTable), &values); err != nil {