-
Notifications
You must be signed in to change notification settings - Fork 8
/
workspacesize.go
86 lines (69 loc) · 2.18 KB
/
workspacesize.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
// 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 buildkit
import (
"context"
"fmt"
"io/fs"
"strings"
"github.com/dustin/go-humanize"
"github.com/moby/patternmatcher"
"golang.org/x/exp/slices"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/fnfs"
)
type fileAndSize struct {
Name string
Size uint64
}
type workspaceSizeReport struct {
Files []fileAndSize
TotalSize uint64
}
func reportWorkspaceSize(ctx context.Context, fsys fs.FS, matcher *patternmatcher.PatternMatcher) (workspaceSizeReport, error) {
var w workspaceSizeReport
err := fnfs.WalkDir(fsys, ".", func(path string, d fs.DirEntry) error {
if matches, err := matcher.MatchesOrParentMatches(path); err != nil {
return err
} else if matches {
if d.IsDir() {
return fs.SkipDir
}
return nil
}
if !d.IsDir() {
fi, err := d.Info()
if err == nil {
w.Files = append(w.Files, fileAndSize{path, uint64(fi.Size())})
w.TotalSize += uint64(fi.Size())
}
}
return nil
})
slices.SortFunc(w.Files, func(a, b fileAndSize) bool {
return a.Size > b.Size
})
return w, err
}
func makeSizeError(w workspaceSizeReport) error {
fileList := w.Files
var description string
if len(fileList) > 0 {
if len(fileList) > 10 {
fileList = fileList[:10]
}
fileLabel := make([]string, len(fileList))
for k, l := range fileList {
fileLabel[k] = fmt.Sprintf(" %s (%s)", l.Name, humanize.Bytes(l.Size))
}
description = fmt.Sprintf(" The top %d largest files in the workspace are:\n\n%s", len(fileLabel), strings.Join(fileLabel, "\n"))
} else {
description = "Wasn't able to compute the largest files in the workspace."
}
return fnerrors.New(`the workspace snapshot is unexpectedly large (%s vs max expected %s);
this is likely a problem with the way that foundation is filtering workspace contents.
%s
If you don't think this is an actual issue, please re-run with --skip_buildkit_workspace_size_check=true.`,
humanize.Bytes(w.TotalSize), humanize.Bytes(maxExpectedWorkspaceSize), description)
}