-
Notifications
You must be signed in to change notification settings - Fork 1
/
builder.go
56 lines (49 loc) · 1.2 KB
/
builder.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
53
54
55
56
package filetree
import (
"fmt"
"github.com/nextlinux/stereoscope/pkg/file"
)
// Builder is a helper for building a filetree and accompanying index in a coordinated fashion.
type Builder struct {
tree Writer
index IndexWriter
}
func NewBuilder(tree Writer, index IndexWriter) *Builder {
return &Builder{
tree: tree,
index: index,
}
}
func (b *Builder) Add(metadata file.Metadata) (*file.Reference, error) {
var (
ref *file.Reference
err error
)
switch metadata.Type {
case file.TypeSymLink:
ref, err = b.tree.AddSymLink(file.Path(metadata.Path), file.Path(metadata.LinkDestination))
if err != nil {
return nil, err
}
case file.TypeHardLink:
ref, err = b.tree.AddHardLink(file.Path(metadata.Path), file.Path(metadata.LinkDestination))
if err != nil {
return nil, err
}
case file.TypeDirectory:
ref, err = b.tree.AddDir(file.Path(metadata.Path))
if err != nil {
return nil, err
}
default:
ref, err = b.tree.AddFile(file.Path(metadata.Path))
if err != nil {
return nil, err
}
}
if ref == nil {
return nil, fmt.Errorf("could not add path=%q link=%q during tar iteration", metadata.Path, metadata.LinkDestination)
}
b.index.Add(*ref, metadata)
return ref, nil
}