Skip to content

Commit 25a84a3

Browse files
authored
Use os.DirFS over apkofs.DirFS (#2079)
* Use os.DirFS over apkofs.DirFS We don't need this anymore, and the signature changed to require a context.Context, so I'm just dropping it rather than dealing with that. Signed-off-by: Jon Johnson <[email protected]> * Actually return errors if missing SourceDir Signed-off-by: Jon Johnson <[email protected]> --------- Signed-off-by: Jon Johnson <[email protected]>
1 parent 07d5134 commit 25a84a3

File tree

5 files changed

+21
-16
lines changed

5 files changed

+21
-16
lines changed

pkg/build/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ func (b *Build) BuildPackage(ctx context.Context) error {
687687
return fmt.Errorf("mkdir -p %s: %w", b.WorkspaceDir, err)
688688
}
689689

690-
fs := apkofs.DirFS(b.SourceDir)
690+
fs := os.DirFS(b.SourceDir)
691691
if fs != nil {
692692
log.Infof("populating workspace %s from %s", b.WorkspaceDir, b.SourceDir)
693693
if err := b.populateWorkspace(ctx, fs); err != nil {

pkg/build/test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,20 @@ func (t *Test) PopulateWorkspace(ctx context.Context, src fs.FS) error {
209209

210210
log.Infof("populating workspace %s from %s", t.WorkspaceDir, t.SourceDir)
211211

212-
fsys := apkofs.DirFS(t.SourceDir, apkofs.WithCreateDir())
213-
214-
if fsys == nil {
215-
return fmt.Errorf("unable to create/use directory %s", t.SourceDir)
212+
fi, err := os.Stat(t.SourceDir)
213+
switch {
214+
case err != nil && !os.IsNotExist(err):
215+
return fmt.Errorf("stating dir %s: %w", t.SourceDir, err)
216+
case err != nil && os.IsNotExist(err):
217+
if err := os.MkdirAll(t.SourceDir, 0o700); err != nil {
218+
return fmt.Errorf("creating dir %s: %w", t.SourceDir, err)
219+
}
220+
case !fi.IsDir():
221+
return fmt.Errorf("not a directory: %s", t.SourceDir)
216222
}
217223

224+
fsys := os.DirFS(t.SourceDir)
225+
218226
return fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
219227
if err != nil {
220228
return err
@@ -296,7 +304,7 @@ func (t *Test) TestPackage(ctx context.Context) error {
296304
return fmt.Errorf("mkdir -p %s: %w", t.WorkspaceDir, err)
297305
}
298306

299-
if err := t.PopulateWorkspace(ctx, apkofs.DirFS(t.SourceDir)); err != nil {
307+
if err := t.PopulateWorkspace(ctx, os.DirFS(t.SourceDir)); err != nil {
300308
return fmt.Errorf("unable to populate workspace: %w", err)
301309
}
302310
}

pkg/cli/license_check.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
"github.com/spf13/cobra"
2323

24-
apkofs "chainguard.dev/apko/pkg/apk/fs"
2524
"chainguard.dev/melange/pkg/config"
2625
"chainguard.dev/melange/pkg/license"
2726
"chainguard.dev/melange/pkg/renovate"
@@ -69,7 +68,7 @@ func licenseCheck() *cobra.Command {
6968
if err != nil {
7069
return fmt.Errorf("failed to get absolute path for source directory: %w", err)
7170
}
72-
sourceFS := apkofs.DirFS(sourceDir)
71+
sourceFS := os.DirFS(sourceDir)
7372
detectedLicenses, diffs, err := license.LicenseCheck(ctx, cfg, sourceFS)
7473
if err != nil {
7574
return err

pkg/license/license_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"strings"
2424
"testing"
2525

26-
apkofs "chainguard.dev/apko/pkg/apk/fs"
2726
"chainguard.dev/melange/pkg/config"
2827
"github.com/chainguard-dev/clog"
2928
)
@@ -59,7 +58,7 @@ func TestFindLicenseFiles(t *testing.T) {
5958
fp.Close()
6059
}
6160

62-
tmpFS := apkofs.DirFS(tmpDir)
61+
tmpFS := os.DirFS(tmpDir)
6362

6463
// Call function under test
6564
licenseFiles, err := FindLicenseFiles(tmpFS)
@@ -135,7 +134,7 @@ func TestFindLicenseFiles(t *testing.T) {
135134
fp.Close()
136135
}
137136

138-
tmpFS = apkofs.DirFS(tmpDir)
137+
tmpFS = os.DirFS(tmpDir)
139138

140139
// Call function under test
141140
licenseFiles, err = FindLicenseFiles(tmpFS)
@@ -165,7 +164,7 @@ func TestIdentify(t *testing.T) {
165164
}
166165

167166
testDataDir := "testdata"
168-
dataFS := apkofs.DirFS(testDataDir)
167+
dataFS := os.DirFS(testDataDir)
169168
err = fs.WalkDir(dataFS, ".", func(path string, info fs.DirEntry, err error) error {
170169
if err != nil {
171170
t.Errorf("Error walking through testdata directory: %v", err)
@@ -217,7 +216,7 @@ func TestLicenseCheck(t *testing.T) {
217216
}
218217

219218
testDataDir := "testdata"
220-
dataFS := apkofs.DirFS(testDataDir)
219+
dataFS := os.DirFS(testDataDir)
221220

222221
// Create a buffer to capture log output
223222
var logBuf strings.Builder
@@ -293,7 +292,7 @@ func TestLicenseCheck_withOverrides(t *testing.T) {
293292
}
294293

295294
testDataDir := "testdata"
296-
dataFS := apkofs.DirFS(testDataDir)
295+
dataFS := os.DirFS(testDataDir)
297296

298297
// Call function under test
299298
_, diffs, err := LicenseCheck(context.Background(), cfg, dataFS)

pkg/linter/linter.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"slices"
3030
"strings"
3131

32-
apkofs "chainguard.dev/apko/pkg/apk/fs"
3332
"github.com/chainguard-dev/clog"
3433
"github.com/dustin/go-humanize"
3534
"golang.org/x/exp/maps"
@@ -684,7 +683,7 @@ func LintBuild(ctx context.Context, cfg *config.Configuration, packageName strin
684683
}
685684

686685
log := clog.FromContext(ctx)
687-
fsys := apkofs.DirFS(path)
686+
fsys := os.DirFS(path)
688687

689688
if err := lintPackageFS(ctx, cfg, packageName, fsys, warn); err != nil {
690689
log.Warn(err.Error())

0 commit comments

Comments
 (0)