-
Notifications
You must be signed in to change notification settings - Fork 9
/
snapshot.go
165 lines (137 loc) · 3.66 KB
/
snapshot.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// 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 memfs
import (
"context"
"io/fs"
"sort"
"github.com/moby/patternmatcher"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/fnfs"
"namespacelabs.dev/foundation/std/tasks"
)
type SnapshotOpts struct {
RequireIncludeFiles bool
IncludeFiles []string
ExcludePatterns []string
}
type SnapshotFS interface {
Snapshot(SnapshotOpts) (*FS, error)
}
type matcher struct {
requiredFileMap map[string]struct{}
observedFileMap map[string]struct{}
excludeMatcher *patternmatcher.PatternMatcher
}
func newMatcher(opts SnapshotOpts) (*matcher, error) {
m := &matcher{}
if len(opts.IncludeFiles) > 0 {
m.requiredFileMap = map[string]struct{}{}
for _, f := range opts.IncludeFiles {
m.requiredFileMap[f] = struct{}{}
}
}
if len(opts.ExcludePatterns) > 0 {
excludeMatcher, err := patternmatcher.New(opts.ExcludePatterns)
if err != nil {
return nil, err
}
m.excludeMatcher = excludeMatcher
}
return m, nil
}
type matchAny struct {
matches []WithMatch
}
func (m matchAny) Match(str string) bool {
for _, x := range m.matches {
if x.Match(str) {
return true
}
}
return false
}
func (m *matcher) includes(file string) (bool, error) {
if m.requiredFileMap != nil {
if _, ok := m.requiredFileMap[file]; ok {
if m.observedFileMap == nil {
m.observedFileMap = map[string]struct{}{}
}
m.observedFileMap[file] = struct{}{}
return true, nil
}
return false, nil
}
if m.excludeMatcher != nil {
excluded, err := m.excludeMatcher.MatchesOrParentMatches(file)
return !excluded, err
}
return true, nil
}
func DeferSnapshot(fsys fs.FS, opts SnapshotOpts) compute.Computable[fs.FS] {
return compute.Inline(tasks.Action("fs.snapshot"), func(ctx context.Context) (fs.FS, error) {
return Snapshot(fsys, opts)
})
}
func Snapshot(fsys fs.FS, opts SnapshotOpts) (*FS, error) {
if snapfs, ok := fsys.(SnapshotFS); ok {
return snapfs.Snapshot(opts)
}
return snapshotWith(fsys, opts, ".", true, func(path string) ([]byte, error) {
return fs.ReadFile(fsys, path)
})
}
func SnapshotDir(fsys fs.FS, dir string, opts SnapshotOpts) (*FS, error) {
return snapshotWith(fsys, opts, dir, false, func(path string) ([]byte, error) {
return fs.ReadFile(fsys, path)
})
}
func snapshotWith(fsys fs.FS, opts SnapshotOpts, dir string, godeep bool, readFile func(string) ([]byte, error)) (*FS, error) {
m, err := newMatcher(opts)
if err != nil {
return nil, err
}
var snapshot FS
if err := fnfs.WalkDir(fsys, dir, func(path string, d fs.DirEntry) error {
if d.IsDir() {
if godeep || path == dir {
return nil
}
return fs.SkipDir
}
if !d.Type().IsRegular() {
return nil
}
// Only check files for inclusion.
// A directory might be excluded by default, but files in it may be included.
if included, err := m.includes(path); err != nil {
return err
} else if !included {
return nil
}
contents, err := readFile(path)
if err != nil {
return err
}
snapshot.Add(path, contents)
return nil
}); err != nil {
return nil, err
}
if opts.RequireIncludeFiles && len(m.requiredFileMap) != len(m.observedFileMap) {
var left []string
for k := range m.requiredFileMap {
if _, ok := m.observedFileMap[k]; !ok {
left = append(left, k)
}
}
sort.Strings(left)
return nil, fnerrors.BadInputError("failed to load required files: %v", left)
}
return &snapshot, nil
}
type WithMatch interface {
Match(name string) bool
}