Skip to content

Commit

Permalink
Merge pull request #54 from suborbital/connor/relative-files
Browse files Browse the repository at this point in the history
Allow for complex directory structures within the bundle static/
  • Loading branch information
cohix committed Feb 10, 2021
2 parents b9eafd8 + 358474d commit a6edd20
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
11 changes: 6 additions & 5 deletions bundle/bundle.go
Expand Up @@ -67,7 +67,8 @@ func (b *Bundle) StaticFile(filePath string) ([]byte, error) {

// Write writes a runnable bundle
// based loosely on https://golang.org/src/archive/zip/example_test.go
func Write(directive *directive.Directive, files []os.File, staticFiles []os.File, targetPath string) error {
// staticFiles should be a map of *relative* filepaths to their associated files, with or without the `static/` prefix.
func Write(directive *directive.Directive, modules []os.File, staticFiles map[string]os.File, targetPath string) error {
if directive == nil {
return errors.New("directive must be provided")
}
Expand All @@ -83,8 +84,8 @@ func Write(directive *directive.Directive, files []os.File, staticFiles []os.Fil
return errors.Wrap(err, "failed to writeDirective")
}

// Add some files to the archive.
for _, file := range files {
// Add the Wasm modules to the archive.
for _, file := range modules {
if file.Name() == "Directive.yaml" || file.Name() == "Directive.yml" {
// only allow the canonical directive that's passed in
continue
Expand All @@ -101,13 +102,13 @@ func Write(directive *directive.Directive, files []os.File, staticFiles []os.Fil
}

// Add static files to the archive.
for _, file := range staticFiles {
for path, file := range staticFiles {
contents, err := ioutil.ReadAll(&file)
if err != nil {
return errors.Wrapf(err, "failed to read file %s", file.Name())
}

fileName := fmt.Sprintf("static/%s", filepath.Base(file.Name()))
fileName := ensurePrefix(path, "static/")
if err := writeFile(w, fileName, contents); err != nil {
return errors.Wrap(err, "failed to writeFile into bundle")
}
Expand Down
6 changes: 3 additions & 3 deletions bundle/bundlewritetester/main.go
Expand Up @@ -23,16 +23,16 @@ func main() {
files = append(files, *file)
}

staticFiles := []os.File{}
staticFiles := map[string]os.File{}
for _, filename := range []string{"go.mod", "go.sum", "Makefile"} {
path := filepath.Join("./", filename)
path := filepath.Join("/", filename)

file, err := os.Open(path)
if err != nil {
log.Fatal("failed to open file", err)
}

staticFiles = append(staticFiles, *file)
staticFiles[path] = *file
}

directive := &directive.Directive{
Expand Down

0 comments on commit a6edd20

Please sign in to comment.