Skip to content

Commit

Permalink
Expand args on the HOST command so you can have dynamic hostnames (#3…
Browse files Browse the repository at this point in the history
…957)

fixes #1743

---------

Co-authored-by: Alex Couture-Beil <[email protected]>
Co-authored-by: Alex Couture-Beil <[email protected]>
  • Loading branch information
3 people authored Apr 2, 2024
1 parent 9801a49 commit 195a318
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
46 changes: 46 additions & 0 deletions ast/tests/host.ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,52 @@
}
]
},
{
"name": "expand-args",
"recipe": [
{
"command": {
"args": [
"fake_host",
"=",
"example.com"
],
"name": "ARG"
}
},
{
"command": {
"args": [
"target_ip",
"=",
"1.2"
],
"name": "ARG"
}
},
{
"command": {
"args": [
"$fake_host",
"$target_ip.3.4"
],
"name": "HOST"
}
},
{
"command": {
"args": [
"cat",
"/etc/hosts",
"|",
"acbgrep",
"\"1.2.3.4\\W*example.com\""
],
"name": "RUN"
}
}
]
},
{
"name": "invalid-ip",
"recipe": [
Expand Down
12 changes: 8 additions & 4 deletions earthfile2llb/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2024,10 +2024,14 @@ func (i *Interpreter) handleHost(ctx context.Context, cmd spec.Command) error {
if i.local {
return i.errorf(cmd.SourceLocation, "HOST command not supported with LOCALLY")
}

host := cmd.Args[0]
ipStr := cmd.Args[1]

host, err := i.expandArgs(ctx, cmd.Args[0], true, false)
if err != nil {
return i.errorf(cmd.SourceLocation, "unable to expand host name for HOST: %s", cmd.Args)
}
ipStr, err := i.expandArgs(ctx, cmd.Args[1], true, false)
if err != nil {
return i.errorf(cmd.SourceLocation, "unable to expand IP addr for HOST: %s", cmd.Args)
}
ip := net.ParseIP(ipStr)
if ip == nil {
return i.errorf(cmd.SourceLocation, "invalid HOST ip %s", ipStr)
Expand Down
1 change: 1 addition & 0 deletions tests/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,7 @@ host:
DO +RUN_EARTHLY --earthfile=host.earth --target=+add-single-host
DO +RUN_EARTHLY --earthfile=host.earth --target=+add-multiple-host
DO +RUN_EARTHLY --earthfile=host.earth --target=+add-ipv6
DO +RUN_EARTHLY --earthfile=host.earth --target=+expand-args

host-invalid:
DO +RUN_EARTHLY --earthfile=host.earth --should_fail=true --target=+invalid-ip --output_contains="invalid HOST ip"
Expand Down
6 changes: 6 additions & 0 deletions tests/host.earth
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ add-ipv6:
HOST cool.website dead:beef::cafe
RUN cat /etc/hosts | grep "dead:beef::cafe\W*cool.website"

expand-args:
ARG fake_host=example.com
ARG target_ip=1.2
HOST $fake_host $target_ip.3.4
RUN cat /etc/hosts | acbgrep "1.2.3.4\W*example.com"

invalid-ip:
HOST example.com 1.2.3.4.5.6.7.8.9.10

Expand Down

0 comments on commit 195a318

Please sign in to comment.