Skip to content

Commit

Permalink
Merge pull request #187 from suborbital/issue-123
Browse files Browse the repository at this point in the history
feat(builder): Introduce templating for prereq commands
  • Loading branch information
javorszky committed Feb 7, 2022
2 parents 84b7b4d + 64cb16d commit 9249bb7
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ subo/docker/publish:
mod/replace/atmo:
go mod edit -replace github.com/suborbital/atmo=$(HOME)/Workspaces/suborbital/atmo

tidy:
go mod tidy && go mod download && go mod vendor

.PHONY: subo subo/docker
16 changes: 11 additions & 5 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
"strings"

"github.com/pkg/errors"
"golang.org/x/mod/semver"

"github.com/suborbital/atmo/bundle"
"github.com/suborbital/atmo/directive"
"github.com/suborbital/subo/builder/context"
"github.com/suborbital/subo/subo/release"
"github.com/suborbital/subo/subo/util"
"golang.org/x/mod/semver"
)

// Builder is capable of building Wasm modules from source
Expand Down Expand Up @@ -259,20 +260,25 @@ func (b *Builder) checkAndRunPreReqs(runnable context.RunnableDir, result *Build
}

for _, p := range preReqs {

filepath := filepath.Join(runnable.Fullpath, p.File)

if _, err := os.Stat(filepath); err != nil {
if errors.Is(err, os.ErrNotExist) {
b.log.LogStart(fmt.Sprintf("missing %s, fixing...", p.File))

outputLog, err := util.RunInDir(p.Command, runnable.Fullpath)

result.OutputLog += outputLog + "\n"
fullCmd, err := p.GetCommand(runnable)
if err != nil {
return errors.Wrap(err, "prereq.GetCommand")
}

outputLog, err := util.RunInDir(fullCmd, runnable.Fullpath)
if err != nil {
return errors.Wrapf(err, "failed to Run prerequisite: %s", p.Command)
return errors.Wrapf(err, "util.RunInDir: %s", fullCmd)
}

result.OutputLog += outputLog + "\n"

b.log.LogDone("fixed!")
}
}
Expand Down
29 changes: 26 additions & 3 deletions builder/context/prereq.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package context

// PreReq is a pre-requisite file paired with the native command needed to acquire that file (if it's missing)
import (
"strings"
"text/template"

"github.com/pkg/errors"
)

// Prereq is a pre-requisite file paired with the native command needed to acquire that file (if it's missing).
type Prereq struct {
File string
Command string
Expand All @@ -18,7 +25,7 @@ var PreRequisiteCommands = map[string]map[string][]Prereq{
},
Prereq{
File: "_lib/_lib.tar.gz",
Command: "curl -L https://github.com/suborbital/reactr/archive/v0.13.0.tar.gz -o _lib/_lib.tar.gz",
Command: "curl -L https://github.com/suborbital/reactr/archive/v{{ .Runnable.APIVersion }}.tar.gz -o _lib/_lib.tar.gz",
},
Prereq{
File: "_lib/suborbital",
Expand Down Expand Up @@ -49,7 +56,7 @@ var PreRequisiteCommands = map[string]map[string][]Prereq{
},
Prereq{
File: "_lib/_lib.tar.gz",
Command: "curl -L https://github.com/suborbital/reactr/archive/v0.13.0.tar.gz -o _lib/_lib.tar.gz",
Command: "curl -L https://github.com/suborbital/reactr/archive/v{{ .Runnable.APIVersion }}.tar.gz -o _lib/_lib.tar.gz",
},
Prereq{
File: "_lib/suborbital",
Expand All @@ -71,3 +78,19 @@ var PreRequisiteCommands = map[string]map[string][]Prereq{
},
},
}

// GetCommand takes a RunnableDir, and returns an executed template command string.
func (p Prereq) GetCommand(r RunnableDir) (string, error) {
cmdTmpl, err := template.New("cmd").Parse(p.Command)
if err != nil {
return "", errors.Wrapf(err, "failed to parse prerequisite Command string into template: %s", p.Command)
}

var fullCmd strings.Builder
err = cmdTmpl.Execute(&fullCmd, r)
if err != nil {
return "", errors.Wrap(err, "failed to execute prerequisite Command string with runnableDir")
}

return fullCmd.String(), nil
}
68 changes: 68 additions & 0 deletions builder/context/prereq_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package context

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/suborbital/atmo/directive"
)

func TestPrereq_GetCommand(t *testing.T) {
tests := []struct {
name string
prereq Prereq
r RunnableDir
want string
wantErr assert.ErrorAssertionFunc
}{
{
name: "successfully expands template",
prereq: Prereq{
File: "_lib/_lib.tar.gz",
Command: "curl -L https://github.com/suborbital/reactr/archive/v{{ .Runnable.APIVersion }}.tar.gz -o _lib/_lib.tar.gz",
},
r: RunnableDir{
Runnable: &directive.Runnable{
APIVersion: "0.33.75",
},
},
want: "curl -L https://github.com/suborbital/reactr/archive/v0.33.75.tar.gz -o _lib/_lib.tar.gz",
wantErr: assert.NoError,
},
{
name: "errors due to missing data to expand with",
prereq: Prereq{
File: "_lib/_lib.tar.gz",
Command: "curl -L https://github.com/suborbital/reactr/archive/v{{ .Runnable.APIVersion }}.tar.gz -o _lib/_lib.tar.gz",
},
r: RunnableDir{
Runnable: nil,
},
want: "",
wantErr: assert.Error,
},
{
name: "successfully expands command with no template tag in it",
prereq: Prereq{
File: "_lib/_lib.tar.gz",
Command: "curl -L https://github.com/suborbital/reactr/archive/v2.tar.gz -o _lib/_lib.tar.gz",
},
r: RunnableDir{
Runnable: &directive.Runnable{
APIVersion: "0.33.75",
},
},
want: "curl -L https://github.com/suborbital/reactr/archive/v2.tar.gz -o _lib/_lib.tar.gz",
wantErr: assert.NoError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.prereq.GetCommand(tt.r)

tt.wantErr(t, err)
assert.Equal(t, tt.want, got)
})
}
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ require (
github.com/pelletier/go-toml v1.9.4
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.2.1
github.com/stretchr/testify v1.7.0
github.com/suborbital/atmo v0.4.0
golang.org/x/mod v0.5.1
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/go-redis/redis/v8 v8.11.4 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
Expand All @@ -32,6 +34,7 @@ require (
github.com/jackc/pgx/v4 v4.13.0 // indirect
github.com/jmoiron/sqlx v1.3.4 // indirect
github.com/julienschmidt/httprouter v1.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sethvargo/go-envconfig v0.4.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/suborbital/reactr v0.13.0 // indirect
Expand All @@ -41,4 +44,5 @@ require (
golang.org/x/sys v0.0.0-20211113001501-0c823b97ae02 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

0 comments on commit 9249bb7

Please sign in to comment.