Skip to content

Commit

Permalink
impr: support binary files
Browse files Browse the repository at this point in the history
  • Loading branch information
nalgeon committed Dec 11, 2023
1 parent 81240dd commit 0385ead
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/engine/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (e *Docker) writeFiles(dir string, files Files) error {
name = e.cmd.Entry
}
path := filepath.Join(dir, name)
err = os.WriteFile(path, []byte(content), 0444)
err = fileio.WriteFile(path, content, 0444)
return err == nil
})
return err
Expand Down
26 changes: 26 additions & 0 deletions internal/fileio/fileio.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
package fileio

import (
"encoding/base64"
"encoding/json"
"errors"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
)

// CopyFile copies all files matching the pattern
Expand Down Expand Up @@ -52,3 +56,25 @@ func ReadJson[T any](path string) (T, error) {
}
return obj, err
}

// WriteFile writes the file to disk.
// The content can be text or binary (encoded as a data URL),
// e.g. data:application/octet-stream;base64,MTIz
func WriteFile(path, content string, perm fs.FileMode) (err error) {
var data []byte
if strings.HasPrefix(content, "data:") {
// data-url encoded file
_, encoded, found := strings.Cut(content, ",")
if !found {
return errors.New("invalid data-url encoding")
}
data, err = base64.StdEncoding.DecodeString(encoded)
if err != nil {
return err
}
} else {
// text file
data = []byte(content)
}
return os.WriteFile(path, data, perm)
}
56 changes: 56 additions & 0 deletions internal/fileio/fileio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,59 @@ func TestReadJson(t *testing.T) {
}
})
}

func TestWriteFile(t *testing.T) {
dir, err := os.MkdirTemp("", "files")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

t.Run("text", func(t *testing.T) {
path := filepath.Join(dir, "data.txt")
err = WriteFile(path, "hello", 0444)
if err != nil {
t.Fatalf("expected nil err, got %v", err)
}
got, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read file: expected nil err, got %v", err)
}
want := []byte("hello")
if !reflect.DeepEqual(got, want) {
t.Errorf("read file: expected %v, got %v", want, got)
}
})

t.Run("binary", func(t *testing.T) {
path := filepath.Join(dir, "data.bin")
err = WriteFile(path, "data:application/octet-stream;base64,MTIz", 0444)
if err != nil {
t.Fatalf("expected nil err, got %v", err)
}
got, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read file: expected nil err, got %v", err)
}
want := []byte("123")
if !reflect.DeepEqual(got, want) {
t.Errorf("read file: expected %v, got %v", want, got)
}
})

t.Run("missing data-url separator", func(t *testing.T) {
path := filepath.Join(dir, "data.bin")
err = WriteFile(path, "data:application/octet-stream:MTIz", 0444)
if err == nil {
t.Fatal("expected error, got nil")
}
})

t.Run("invalid binary value", func(t *testing.T) {
path := filepath.Join(dir, "data.bin")
err = WriteFile(path, "data:application/octet-stream;base64,12345", 0444)
if err == nil {
t.Fatal("expected error, got nil")
}
})
}

0 comments on commit 0385ead

Please sign in to comment.