-
Notifications
You must be signed in to change notification settings - Fork 9
/
fs.go
52 lines (42 loc) · 1.35 KB
/
fs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package llbutil
import (
"context"
"encoding/json"
"io/fs"
"path/filepath"
"github.com/moby/buildkit/client/llb"
"namespacelabs.dev/foundation/internal/bytestream"
"namespacelabs.dev/foundation/internal/fnfs"
)
func WriteFS(ctx context.Context, fsys fs.FS, base llb.State, target string) (llb.State, error) {
if err := fnfs.VisitFiles(ctx, fsys, func(path string, blob bytestream.ByteStream, de fs.DirEntry) error {
info, err := de.Info()
if err != nil {
return err
}
contents, err := bytestream.ReadAll(blob)
if err != nil {
return err
}
base = base.File(llb.Mkfile(filepath.Join(target, path), info.Mode(), contents))
return nil
}); err != nil {
return llb.State{}, err
}
return base, nil
}
func AddFile(base llb.State, path string, m fs.FileMode, content []byte) llb.State {
return base.
File(llb.Mkdir(filepath.Dir(path), 0755, llb.WithParents(true))).
File(llb.Mkfile(path, m, content))
}
func AddSerializedJsonAsFile(base llb.State, path string, content any) (llb.State, error) {
serialized, err := json.MarshalIndent(content, "", " ")
if err != nil {
return llb.State{}, err
}
return AddFile(base, path, 0644, serialized), nil
}