impr: support binary files
This commit is contained in:
@@ -139,7 +139,7 @@ func (e *Docker) writeFiles(dir string, files Files) error {
|
|||||||
name = e.cmd.Entry
|
name = e.cmd.Entry
|
||||||
}
|
}
|
||||||
path := filepath.Join(dir, name)
|
path := filepath.Join(dir, name)
|
||||||
err = os.WriteFile(path, []byte(content), 0444)
|
err = fileio.WriteFile(path, content, 0444)
|
||||||
return err == nil
|
return err == nil
|
||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -2,10 +2,14 @@
|
|||||||
package fileio
|
package fileio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CopyFile copies all files matching the pattern
|
// CopyFile copies all files matching the pattern
|
||||||
@@ -52,3 +56,25 @@ func ReadJson[T any](path string) (T, error) {
|
|||||||
}
|
}
|
||||||
return obj, err
|
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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -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")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user