Skip to content

Commit

Permalink
Gadget hotfix (#1755)
Browse files Browse the repository at this point in the history
* Implement a very simple command to dump parsed recipe/cookbook data, to make debugging easier in the absence of nicer data validation stuff

* Sometimes vscode doesn't save your file contents! That is fine and normal!

* test + provisional bugfix

* gofumpt

* fix this test

* fix the regex...
  • Loading branch information
afti-githobo authored Jan 23, 2023
1 parent 4cc5ad9 commit e86c828
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 6 deletions.
19 changes: 14 additions & 5 deletions cmd/pylonsd/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ import (
"github.com/gogo/protobuf/jsonpb"
)

var Out io.Writer = os.Stdout // modified during testing
var (
Out io.Writer = os.Stdout // modified during testing
Verbose = false
)

// group 1: (whole raw string tokens encapsulated ```like this```)
// group 2: (tokens not containing whitespace, separated by whitespace)
// group 3: (whatever is in front of the first whitespace)
var gadgetParamParseRegex = regexp.MustCompile(`(\s'''.*''')|(\s.\S*)|(\S*)`)
var gadgetParamParseRegex = regexp.MustCompile(`(\s'''.*?''')|(\s.\S*)|(\S*)`)

const (
cookbookExtension = ".plc"
Expand Down Expand Up @@ -82,7 +85,7 @@ func loadModuleFromPath(modulePath, currentPath string) string {
return string(bytes)
}

func loadModulesInline(bytes []byte, path string, info os.FileInfo, gadgets *[]Gadget) string {
func LoadModulesInline(bytes []byte, path string, info os.FileInfo, gadgets *[]Gadget) string {
lines := strings.Split(string(bytes), "\n")
for i, line := range lines {
line = strings.TrimSpace(line)
Expand Down Expand Up @@ -114,7 +117,10 @@ func loadCookbookFromPath(path string, gadgets *[]Gadget) (types.Cookbook, strin
info, _ := os.Stat(path)
var cb types.Cookbook

json := loadModulesInline(bytes, path, info, gadgets)
json := LoadModulesInline(bytes, path, info, gadgets)
if Verbose {
println(json)
}
err := jsonpb.UnmarshalString(json, &cb)

return cb, json, err
Expand All @@ -125,7 +131,10 @@ func loadRecipeFromPath(path string, gadgets *[]Gadget) (types.Recipe, string, e
info, _ := os.Stat(path)
var rcp types.Recipe

json := loadModulesInline(bytes, path, info, gadgets)
json := LoadModulesInline(bytes, path, info, gadgets)
if Verbose {
println(json)
}
err := jsonpb.UnmarshalString(json, &rcp)
return rcp, string(bytes), err
}
25 changes: 25 additions & 0 deletions cmd/pylonsd/cmd/dev_parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import (
"github.com/spf13/cobra"

"github.com/Pylons-tech/pylons/x/pylons/types"
)

func DevParse() *cobra.Command {
cmd := &cobra.Command{
Use: "parse [path]",
Short: "Parses all Pylons recipe or cookbook files in the provided path and processes macros",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
path := args[0]
// This is slightly goofy but it's the fastest/least invasive way to implement this behavior right now.
// We just set a flag to output the assembled JSON and then let the batch handler run with empty callbacks.
// Down the road it'd be nice to have something a little nicer, but this is enough as-is to make debugging more
// manageable.
Verbose = true
ForFiles(path, func(path string, cookbook types.Cookbook) {}, func(path string, recipe types.Recipe) {})
},
}
return cmd
}
2 changes: 1 addition & 1 deletion cmd/pylonsd/cmd/gadgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func parseGadgets(path string, s string) ([]Gadget, error) {
func ExpandGadget(gadget *Gadget, params []string) string {
str := gadget.json
for i := 0; i < gadget.parametersCount; i++ {
str = strings.ReplaceAll(str, "%"+strconv.Itoa(i), strings.TrimSpace(params[i]))
str = strings.ReplaceAll(strings.ReplaceAll(str, "%"+strconv.Itoa(i), strings.TrimSpace(params[i])), "'''", "") // hack - strip the triple quotes more elegantly instead of doing this
}
return str
}
Expand Down
22 changes: 22 additions & 0 deletions cmd/pylonsd/cmd/gadgets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package cmd
import (
"fmt"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"

testutil "github.com/Pylons-tech/pylons/testutil/cli"
)

const rcpFilename = "test.rcp"

const pylonsGadgetsLiteral_duplicateName = `#foobar 1
"foo": "%0"
#foobar 1
Expand All @@ -30,6 +33,10 @@ const pylonsGadgetsLiteral_good = `#go_go_gadget_gadgets 3
"bar": "%1",
"%2": "true"`

const gadgetUserLiteral_tripleQuotes = `{
#go_go_gadget_gadgets '''a triple quoted string''' '''another one''' whatever
}`

func TestGadgets(t *testing.T) {
// builtins!

Expand Down Expand Up @@ -167,4 +174,19 @@ func TestGadgets(t *testing.T) {
assert.EqualValues(t, expected, ExpandGadget(gadget, []string{"a", "b", "is_a_gadget"}))
os.Remove(gadgetsFilename)
})

t.Run("Should remove triple quotes from raw string tokens", func(t *testing.T) {
testutil.WriteFixtureAtTestRuntime(gadgetsFilename, pylonsGadgetsLiteral_good)
testutil.WriteFixtureAtTestRuntime(rcpFilename, gadgetUserLiteral_tripleQuotes)
gadgets, err := LoadGadgetsForPath(rcpFilename)
if err != nil {
panic(err)
}
bytes, _ := os.ReadFile(rcpFilename)
info, _ := os.Stat(rcpFilename)
json := LoadModulesInline(bytes, rcpFilename, info, gadgets)
assert.False(t, strings.Contains(json, "'''"))
os.Remove(gadgetsFilename)
os.Remove(rcpFilename)
})
}
1 change: 1 addition & 0 deletions cmd/pylonsd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func main() {
rootCmd.AddCommand(pyloncmd.DevUpdate())
rootCmd.AddCommand(pyloncmd.Completion())
rootCmd.AddCommand(pyloncmd.DevCelCheck())
rootCmd.AddCommand(pyloncmd.DevParse())
removeLineBreaksInCobraArgs(rootCmd)

if err := svrcmd.Execute(rootCmd, "PYLONSD", app.DefaultNodeHome); err != nil {
Expand Down

0 comments on commit e86c828

Please sign in to comment.