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

import (
"bytes"
"fmt"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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",
},
},
},
{
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
Expand Down
Loading