Skip to content
Open
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
27 changes: 23 additions & 4 deletions cmd/command/cd/cd_services.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cd

import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -136,6 +138,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",
},
},
},
{
Expand Down Expand Up @@ -313,10 +319,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")
Expand Down Expand Up @@ -361,9 +363,26 @@ func (p *Plural) handleLuaTemplate(c *cli.Context) error {
L.SetGlobal("contexts", luautils.GoValueToLuaValue(L, ctx["contexts"]))
L.SetGlobal("imports", luautils.GoValueToLuaValue(L, ctx["imports"]))

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)
}

if err := luautils.MapLua(L.GetGlobal("values").(*lua.LTable), &values); err != nil {
return err
Expand Down
Loading