Skip to content

Commit

Permalink
Merge pull request #24 from rightscale/fix_incorrect_printf_and_heredocs
Browse files Browse the repository at this point in the history
Fix incorrect fmt.Printf and HEREDOCS
  • Loading branch information
douglaswth authored Jun 26, 2020
2 parents 069a7f1 + 357062e commit 1286f3a
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* text=auto eol=lf
*.pt linguist-language=Ruby
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea/
.vscode/
out.json
vendor/
version.go
version.yml
Expand Down
7 changes: 7 additions & 0 deletions cmd/fpt/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
v1.1.3 / 2020-06-26
-------------------
* Fix an incorrect use of `fmt.Printf` without a format to use `fmt.Print` instead
* Handle the `'EOS'` and `"EOS"` forms of [HEREDOC](https://ruby-doc.org/core-2.2.7/doc/syntax/literals_rdoc.html#label-Here+Documents)s when detecting JavaScript `code` blocks in the `fpt script` subcommand
* Actually check if `--result`/`-r` is being passed when executing raw JavaScript with the `fpt script` subcommand
* Handle Policy Template (Ruby) comments correctly when parsing for the `fpt script` subcommand

v1.1.2 / 2020-05-21
-------------------
* Actually check for any updates if `update.check` is set to `true`
Expand Down
18 changes: 18 additions & 0 deletions cmd/fpt/fixtures/get_script1.pt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ RESVAR=otra[0]
EOF
end

script 'single_quote_heredoc', type: 'javascript' do # there can be a comment here,
result 'output' # here,
parameters 'a', 'b' # here,
code <<-'eos' # here,
var output = 'Single quote HEREDOCs\nare awesome!';
console.log(a, b, output);
eos
end # and here

script 'double_quote_heredoc', type: 'javascript' do
result 'output'
code <<"eos"
var output = 'Double quote HEREDOCs\\nare not that remarkable.';
console.log(output);
eos
end

policy "filter_check" do
validate $filtered_dcs do
Expand Down
2 changes: 1 addition & 1 deletion cmd/fpt/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func policyTemplateRun(ctx context.Context, cli policy.Client, file string, runO
lastEtag = *log.Etag
lastLog = *log.ResponseBody
if !noLog {
fmt.Printf(lastLog[lastSize:])
fmt.Print(lastLog[lastSize:])
}

//fmt.Printf("STATUS: %s\n", dump(status))
Expand Down
34 changes: 22 additions & 12 deletions cmd/fpt/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
Expand All @@ -24,6 +25,11 @@ var (
maxExecTime = 600 * time.Second
errHalt = fmt.Errorf("HALT")
debuglogDataSize = 10 * 1024
q = regexp.MustCompile(`\\(.)`)
qw = regexp.MustCompile(`"(.*?[^\\])"|'(.*?[^\\])'`)
scriptStartRe = regexp.MustCompile(`^\s*script\s+['"]([^'"]+)['"],.*do\s*(?:#.*)?$`)
scriptEndRe = regexp.MustCompile(`^\s*end\s*(?:#.*)?$`)
codeStartRe = regexp.MustCompile(`^\s*code\s+('.*|".*|<<-?(?:[A-Za-z_]+|'[A-Za-z_]+'|"[A-Za-z_]+")\s*(?:#.*)?)$`)
)

type script struct {
Expand Down Expand Up @@ -108,6 +114,10 @@ func runScript(ctx context.Context, file, outfile, result, name string, options
strings.Join(names, " or "))
}
}
} else if result == "" {
return fmt.Errorf("no result variable specified for raw JavaScript, pass --result with the name of the variable to extract")
} else {
name = strings.TrimSuffix(filepath.Base(file), filepath.Ext(file))
}
fmt.Printf("Running script %q from %s and writing %s to %s\n", name, file, result, outfile)

Expand Down Expand Up @@ -355,10 +365,7 @@ func getScripts(src string) []*script {
lines := strings.Split(src, "\n")
inScript := false
inCode := false
qw := regexp.MustCompile(`"(.*?[^\\])"|'(.*?[^\\])'`)
scriptStartRe := regexp.MustCompile(`^\s*script ['"]([^'"]+)['"],.*do\s*$`)
scriptEndRe := regexp.MustCompile(`^\s*end\s*$`)
codeStartRe := regexp.MustCompile(`^\s*code ('.*|".*|<<-?[A-Z_]+\s*)$`)
unescapeCode := true
var codeEndRe *regexp.Regexp
var codeLine int

Expand Down Expand Up @@ -395,9 +402,9 @@ func getScripts(src string) []*script {
}
scripts = append(scripts, &script{
name: name,
code: unquote(strings.Join(codeLines, "\n")),
code: unquote(strings.Join(codeLines, "\n"), unescapeCode),
line: codeLine,
result: unquote(result),
result: unquote(result, true),
params: params,
})
} else if matches := codeStartRe.FindStringSubmatch(line); len(matches) > 0 {
Expand All @@ -411,7 +418,9 @@ func getScripts(src string) []*script {
codeEndRe = regexp.MustCompile(`^(.*?[^\\]")`)
codeLines = append(codeLines, matches[1])
} else {
end := strings.TrimPrefix(strings.TrimPrefix(matches[1], "<<"), "-")
identifier := strings.TrimSpace(strings.SplitN(strings.TrimPrefix(strings.TrimPrefix(matches[1], "<<"), "-"), "#", 2)[0])
unescapeCode = identifier[:1] != "'"
end := strings.Trim(identifier, `'"`)
codeEndRe = regexp.MustCompile(`^\s*` + end)
codeLine++
}
Expand All @@ -431,23 +440,24 @@ func getScripts(src string) []*script {
}

func getKey(lines []string, key string) string {
keyRe := regexp.MustCompile(fmt.Sprintf(`\s*%s\s+(.*)\s*$`, key))
keyRe := regexp.MustCompile(fmt.Sprintf(`\s*%s\s+([^#]*)(?:#.*)?$`, key))
for _, line := range lines {
if matches := keyRe.FindStringSubmatch(line); len(matches) > 0 {
return matches[1]
return strings.TrimSpace(matches[1])
}
}
return ""
}

func unquote(s string) string {
q := regexp.MustCompile(`\\(.)`)
func unquote(s string, unescape bool) string {
if strings.HasPrefix(s, "'") {
s = s[1 : len(s)-1]
} else if strings.HasPrefix(s, `"`) {
s = s[1 : len(s)-1]
}
s = q.ReplaceAllString(s, "$1")
if unescape {
s = q.ReplaceAllString(s, "$1")
}
return s
}

Expand Down
14 changes: 14 additions & 0 deletions cmd/fpt/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ func TestGetScripts(t *testing.T) {
params: []string{"otra"},
result: "RESVAR",
},
{
name: "single_quote_heredoc",
code: " var output = 'Single quote HEREDOCs\\nare awesome!';\n\n console.log(a, b, output);",
line: 65,
params: []string{"a", "b"},
result: "output",
},
{
name: "double_quote_heredoc",
code: "var output = 'Double quote HEREDOCs\\nare not that remarkable.';\n\nconsole.log(output);",
line: 74,
params: []string{},
result: "output",
},
},
},
}
Expand Down

0 comments on commit 1286f3a

Please sign in to comment.