Skip to content

Commit 2df0011

Browse files
authored
Merge pull request #107 from coryb/issue101
[#101] implement heredoc support
2 parents f3d79a5 + d2d5e9a commit 2df0011

13 files changed

Lines changed: 315 additions & 25 deletions

File tree

checker/checker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ func (c *checker) checkIdentArg(scope *parser.Scope, typ parser.ObjType, ident *
518518
func (c *checker) checkBasicLitArg(typ parser.ObjType, lit *parser.BasicLit) error {
519519
switch typ {
520520
case parser.Str:
521-
if lit.Str == nil {
521+
if lit.Str == nil && lit.HereDoc == nil {
522522
return ErrWrongArgType{lit.Pos, typ, lit.ObjType()}
523523
}
524524
case parser.Int:

codegen/codegen_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,36 @@ func TestCodeGen(t *testing.T) {
210210
Expect(t, llb.Image("busybox:stable")),
211211
)
212212
},
213+
}, {
214+
"here doc processing",
215+
[]string{"default"},
216+
`
217+
fs default() {
218+
image "busybox"
219+
220+
run <<~EOM
221+
echo
222+
hi
223+
EOM
224+
225+
run <<-EOM
226+
echo hi
227+
EOM
228+
229+
run <<EOM
230+
echo hi
231+
EOM
232+
}
233+
`,
234+
func(t *testing.T, cg *CodeGen) solver.Request {
235+
return Expect(t, llb.Image("busybox").Run(
236+
llb.Shlex("echo hi"),
237+
).Run(
238+
llb.Shlex("echo hi"),
239+
).Run(
240+
llb.Shlex("\techo hi"),
241+
).Root())
242+
},
213243
}} {
214244
tc := tc
215245
t.Run(tc.name, func(t *testing.T) {

codegen/expr.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ func (cg *CodeGen) EmitStringExpr(ctx context.Context, scope *parser.Scope, expr
3030
return "", errors.WithStack(ErrCodeGen{expr, errors.Errorf("unknown obj type")})
3131
}
3232
case expr.BasicLit != nil:
33-
return *expr.BasicLit.Str, nil
33+
if expr.BasicLit.Str != nil {
34+
return expr.BasicLit.Str.Unquoted(), nil
35+
}
36+
return expr.BasicLit.HereDoc.Value, nil
3437
case expr.FuncLit != nil:
3538
return cg.EmitStringBlock(ctx, scope, expr.FuncLit.Body.NonEmptyStmts(), nil)
3639
default:

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/docker/cli v1.14.0-0.20190523191156-ab688a9a79a1
99
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
1010
github.com/kr/pretty v0.2.0 // indirect
11+
github.com/lithammer/dedent v1.1.0
1112
github.com/logrusorgru/aurora v0.0.0-20191116043053-66b7ad493a23
1213
github.com/mattn/go-isatty v0.0.11
1314
github.com/mitchellh/go-homedir v1.1.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
264264
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
265265
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
266266
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
267+
github.com/lithammer/dedent v1.1.0 h1:VNzHMVCBNG1j0fh3OrsFRkVUwStdDArbgBWoPAffktY=
268+
github.com/lithammer/dedent v1.1.0/go.mod h1:jrXYCQtgg0nJiN+StA2KgR7w6CiQNv9Fd/Z9BP0jIOc=
267269
github.com/logrusorgru/aurora v0.0.0-20191116043053-66b7ad493a23 h1:Wp7NjqGKGN9te9N/rvXYRhlVcrulGdxnz8zadXWs7fc=
268270
github.com/logrusorgru/aurora v0.0.0-20191116043053-66b7ad493a23/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
269271
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=

module/resolve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ func ResolveGraph(ctx context.Context, resolver Resolver, res Resolved, mod *par
394394
filename = ModuleFilename
395395
case n.ImportPath != nil:
396396
importRes = res
397-
filename = n.ImportPath.Path
397+
filename = n.ImportPath.Path.Unquoted()
398398
}
399399

400400
rc, err := importRes.Open(filename)

module/tree.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ func NewTree(ctx context.Context, cln *client.Client, mw *progress.MultiWriter,
5353
value = filepath.Join(prefix, ModuleFilename)
5454
case decl.ImportPath != nil:
5555
if prefix == "" {
56-
value = decl.ImportPath.Path
56+
value = decl.ImportPath.Path.Unquoted()
5757
} else {
58-
value = filepath.Join(prefix, decl.ImportPath.Path)
58+
value = filepath.Join(prefix, decl.ImportPath.Path.Unquoted())
5959
}
6060
}
6161

module/vendor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func Vendor(ctx context.Context, cln *client.Client, mw *progress.MultiWriter, m
102102
case decl.ImportFunc != nil:
103103
filename = ModuleFilename
104104
case decl.ImportPath != nil:
105-
filename = decl.ImportPath.Path
105+
filename = decl.ImportPath.Path.Unquoted()
106106
}
107107

108108
f, err := os.Create(filepath.Join(vp, filename))

parse_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,24 @@ var (
8585
}
8686
} "src" "dst"
8787
}
88+
89+
string heredocTest() {
90+
value <<-EOM
91+
this
92+
should
93+
dedent
94+
EOM
95+
value <<~EOM
96+
this
97+
should
98+
fold
99+
EOM
100+
value <<EOM
101+
this
102+
is
103+
literal
104+
EOM
105+
}
88106
`
89107
)
90108

0 commit comments

Comments
 (0)