Skip to content

Commit

Permalink
chore: Use io/fs.Skip{All,Dir} sentinel errors
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Aug 13, 2023
1 parent dbc1b4b commit 424189b
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 42 deletions.
8 changes: 4 additions & 4 deletions internal/chezmoi/archive.go
Expand Up @@ -200,7 +200,7 @@ HEADER:
switch err := processHeader(implicitDirHeader(dir+"/", header.ModTime), dir+"/"); {
case errors.Is(err, fs.SkipDir):
continue HEADER
case errors.Is(err, Break):
case errors.Is(err, fs.SkipAll):
return nil
case err != nil:
return err
Expand All @@ -211,7 +211,7 @@ HEADER:
switch err := processHeader(header, header.Name); {
case errors.Is(err, fs.SkipDir):
continue HEADER
case errors.Is(err, Break):
case errors.Is(err, fs.SkipAll):
return nil
case err != nil:
return err
Expand Down Expand Up @@ -281,7 +281,7 @@ FILE:
switch err := processHeader(fileInfo, dir+"/"); {
case errors.Is(err, fs.SkipDir):
continue FILE
case errors.Is(err, Break):
case errors.Is(err, fs.SkipAll):
return nil
case err != nil:
return err
Expand All @@ -306,7 +306,7 @@ FILE:
switch {
case errors.Is(err, fs.SkipDir):
skippedDirPrefixes = append(skippedDirPrefixes, zipFile.Name+"/")
case errors.Is(err, Break):
case errors.Is(err, fs.SkipAll):
return nil
case err != nil:
return err
Expand Down
2 changes: 1 addition & 1 deletion internal/chezmoi/archive_test.go
Expand Up @@ -107,7 +107,7 @@ func TestWalkArchive(t *testing.T) {
case "dir2":
return fs.SkipDir
case "symlink1":
return Break
return fs.SkipAll
default:
return nil
}
Expand Down
8 changes: 0 additions & 8 deletions internal/chezmoi/chezmoi.go
Expand Up @@ -9,11 +9,9 @@ import (
"crypto/sha256"
"crypto/sha512"
"fmt"
"io"
"io/fs"
"net"
"os"
"path/filepath"
"regexp"
"runtime"
"strconv"
Expand All @@ -31,12 +29,6 @@ var (
// DefaultTemplateOptions are the default template options.
DefaultTemplateOptions = []string{"missingkey=error"}

// Break indicates that a walk should be stopped.
Break = io.EOF

// Skip indicates that entry should be skipped.
Skip = filepath.SkipDir

// Umask is the process's umask.
Umask = fs.FileMode(0)
)
Expand Down
29 changes: 14 additions & 15 deletions internal/chezmoi/sourcestate.go
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/mitchellh/copystructure"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
vfs "github.com/twpayne/go-vfs/v4"
"go.uber.org/multierr"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -405,7 +404,7 @@ DEST_ABS_PATH:

if options.PreAddFunc != nil {
switch err := options.PreAddFunc(targetRelPath); {
case errors.Is(err, Skip):
case errors.Is(err, fs.SkipDir):
continue DEST_ABS_PATH
case err != nil:
return err
Expand All @@ -429,7 +428,7 @@ DEST_ABS_PATH:
if !oldSourceEntryRelPath.Empty() && oldSourceEntryRelPath != sourceEntryRelPath {
if options.ReplaceFunc != nil {
switch err := options.ReplaceFunc(targetRelPath, newSourceStateEntry, oldSourceStateEntry); {
case errors.Is(err, Skip):
case errors.Is(err, fs.SkipDir):
continue DEST_ABS_PATH
case err != nil:
return err
Expand Down Expand Up @@ -927,7 +926,7 @@ func (s *SourceState) Read(ctx context.Context, options *ReadOptions) error {
if err := s.addTemplateDataDir(sourceAbsPath, fileInfo); err != nil {
return err
}
return vfs.SkipDir
return fs.SkipDir
case isPrefixDotFormat(fileInfo.Name(), dataName):
if !s.readTemplateData {
return nil
Expand All @@ -937,7 +936,7 @@ func (s *SourceState) Read(ctx context.Context, options *ReadOptions) error {
if err := s.addTemplatesDir(ctx, sourceAbsPath); err != nil {
return err
}
return vfs.SkipDir
return fs.SkipDir
case s.templateDataOnly:
return nil
case isPrefixDotFormat(fileInfo.Name(), externalName) || isPrefixDotFormatDotTmpl(fileInfo.Name(), externalName):
Expand All @@ -947,7 +946,7 @@ func (s *SourceState) Read(ctx context.Context, options *ReadOptions) error {
if err := s.addExternalDir(ctx, sourceAbsPath); err != nil {
return err
}
return vfs.SkipDir
return fs.SkipDir
case fileInfo.Name() == ignoreName || fileInfo.Name() == ignoreName+TemplateSuffix:
return s.addPatterns(s.ignore, sourceAbsPath, parentSourceRelPath)
case fileInfo.Name() == removeName || fileInfo.Name() == removeName+TemplateSuffix:
Expand All @@ -960,14 +959,14 @@ func (s *SourceState) Read(ctx context.Context, options *ReadOptions) error {
for relPath, scriptSourceStateEntries := range scriptsDirSourceStateEntries {
addSourceStateEntries(relPath, scriptSourceStateEntries...)
}
return vfs.SkipDir
return fs.SkipDir
case fileInfo.Name() == VersionName:
return s.readVersionFile(sourceAbsPath)
case strings.HasPrefix(fileInfo.Name(), Prefix):
fallthrough
case strings.HasPrefix(fileInfo.Name(), ignorePrefix):
if fileInfo.IsDir() {
return vfs.SkipDir
return fs.SkipDir
}
return nil
case fileInfo.IsDir():
Expand All @@ -976,7 +975,7 @@ func (s *SourceState) Read(ctx context.Context, options *ReadOptions) error {
TargetRelPath(s.encryption.EncryptedSuffix()).
JoinString(da.TargetName)
if s.Ignore(targetRelPath) {
return vfs.SkipDir
return fs.SkipDir
}
sourceStateDir := s.newSourceStateDir(sourceAbsPath, sourceRelPath, da)
addSourceStateEntries(targetRelPath, sourceStateDir)
Expand Down Expand Up @@ -1353,7 +1352,7 @@ func (s *SourceState) addExternalDir(ctx context.Context, externalsDirAbsPath Ab
return fmt.Errorf("%s: not allowed in %s directory", externalAbsPath, externalsDirName)
case strings.HasPrefix(fileInfo.Name(), ignorePrefix):
if fileInfo.IsDir() {
return vfs.SkipDir
return fs.SkipDir
}
return nil
case fileInfo.Mode().IsRegular():
Expand Down Expand Up @@ -1449,7 +1448,7 @@ func (s *SourceState) addTemplateDataDir(sourceAbsPath AbsPath, fileInfo fs.File
return fmt.Errorf("%s: not allowed in %s directory", dataAbsPath, dataName)
case strings.HasPrefix(fileInfo.Name(), ignorePrefix):
if fileInfo.IsDir() {
return vfs.SkipDir
return fs.SkipDir
}
return nil
case fileInfo.Mode().IsRegular():
Expand Down Expand Up @@ -1482,7 +1481,7 @@ func (s *SourceState) addTemplatesDir(ctx context.Context, templatesDirAbsPath A
return fmt.Errorf("%s: not allowed in %s directory", templateAbsPath, TemplatesDirName)
case strings.HasPrefix(fileInfo.Name(), ignorePrefix):
if fileInfo.IsDir() {
return vfs.SkipDir
return fs.SkipDir
}
return nil
case fileInfo.Mode().IsRegular():
Expand Down Expand Up @@ -2481,7 +2480,7 @@ func (s *SourceState) readExternalArchiveFile(
sourceRelPath: sourceRelPath,
targetStateEntry: targetStateEntry,
}
return Break
return fs.SkipAll
case fileInfo.Mode()&fs.ModeType == fs.ModeSymlink:
fileAttr := FileAttr{
TargetName: fileInfo.Name(),
Expand All @@ -2500,7 +2499,7 @@ func (s *SourceState) readExternalArchiveFile(
sourceRelPath: sourceRelPath,
targetStateEntry: targetStateEntry,
}
return Break
return fs.SkipAll
default:
return fmt.Errorf("%s: unsupported mode %o", name, fileInfo.Mode()&fs.ModeType)
}
Expand Down Expand Up @@ -2694,7 +2693,7 @@ func (s *SourceState) readScriptsDir(
return fmt.Errorf("%s: not allowed in %s directory", sourceAbsPath, scriptsDirName)
case strings.HasPrefix(fileInfo.Name(), ignorePrefix):
if fileInfo.IsDir() {
return vfs.SkipDir
return fs.SkipDir
}
return nil
case fileInfo.IsDir():
Expand Down
2 changes: 1 addition & 1 deletion internal/chezmoi/sourcestate_test.go
Expand Up @@ -1946,7 +1946,7 @@ func (s *SourceState) applyAll(
) error {
for _, targetRelPath := range s.TargetRelPaths() {
switch err := s.Apply(targetSystem, destSystem, persistentState, targetDirAbsPath, targetRelPath, options); {
case errors.Is(err, Skip):
case errors.Is(err, fs.SkipDir):
continue
case err != nil:
return err
Expand Down
2 changes: 1 addition & 1 deletion internal/chezmoi/sourcestatetreenode.go
Expand Up @@ -67,7 +67,7 @@ func (n *sourceStateEntryTreeNode) ForEachNode(
targetRelPath RelPath, f func(RelPath, *sourceStateEntryTreeNode) error,
) error {
switch err := f(targetRelPath, n); {
case errors.Is(err, Skip):
case errors.Is(err, fs.SkipDir):
return nil
case err != nil:
return err
Expand Down
5 changes: 3 additions & 2 deletions internal/cmd/addcmd.go
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"io/fs"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -106,7 +107,7 @@ func (c *Config) defaultPreAddFunc(targetRelPath chezmoi.RelPath) error {
c.Add.prompt = false
return nil
case choice == "no":
return chezmoi.Skip
return fs.SkipDir
case choice == "quit":
return chezmoi.ExitCodeError(0)
case choice == "yes":
Expand Down Expand Up @@ -161,7 +162,7 @@ func (c *Config) defaultReplaceFunc(
c.force = true
return nil
case choice == "no":
return chezmoi.Skip
return fs.SkipDir
case choice == "quit":
return chezmoi.ExitCodeError(0)
case choice == "yes":
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/config.go
Expand Up @@ -613,7 +613,7 @@ func (c *Config) applyArgs(
switch err := sourceState.Apply(
targetSystem, c.destSystem, c.persistentState, targetDirAbsPath, targetRelPath, applyOptions,
); {
case errors.Is(err, chezmoi.Skip):
case errors.Is(err, fs.SkipDir):
continue
case err != nil && c.keepGoing:
c.errorf("%v\n", err)
Expand Down Expand Up @@ -954,7 +954,7 @@ func (c *Config) defaultPreApplyFunc(
case choice == "yes":
return nil
case choice == "no":
return chezmoi.Skip
return fs.SkipDir
case choice == "all":
c.interactive = false
return nil
Expand Down Expand Up @@ -1003,7 +1003,7 @@ func (c *Config) defaultPreApplyFunc(
c.force = true
return nil
case choice == "skip":
return chezmoi.Skip
return fs.SkipDir
case choice == "quit":
return chezmoi.ExitCodeError(0)
default:
Expand Down
4 changes: 3 additions & 1 deletion internal/cmd/mergeallcmd.go
@@ -1,6 +1,8 @@
package cmd

import (
"io/fs"

"github.com/spf13/cobra"

"github.com/twpayne/chezmoi/v2/internal/chezmoi"
Expand Down Expand Up @@ -47,7 +49,7 @@ func (c *Config) runMergeAllCmd(cmd *cobra.Command, args []string) error {
!targetEntryState.Equivalent(actualEntryState) {
targetRelPaths = append(targetRelPaths, targetRelPath)
}
return chezmoi.Skip
return fs.SkipDir
}
if err := c.applyArgs(cmd.Context(), dryRunSystem, c.DestDirAbsPath, args, applyArgsOptions{
cmd: cmd,
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/statuscmd.go
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"io/fs"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -75,7 +76,7 @@ func (c *Config) runStatusCmd(cmd *cobra.Command, args []string) error {
if x != ' ' || y != ' ' {
fmt.Fprintf(&builder, "%c%c %s\n", x, y, targetRelPath)
}
return chezmoi.Skip
return fs.SkipDir
}
if err := c.applyArgs(cmd.Context(), dryRunSystem, c.DestDirAbsPath, args, applyArgsOptions{
cmd: cmd,
Expand Down
7 changes: 3 additions & 4 deletions internal/cmd/unmanagedcmd.go
Expand Up @@ -7,7 +7,6 @@ import (
"strings"

"github.com/spf13/cobra"
vfs "github.com/twpayne/go-vfs/v4"
"golang.org/x/exp/maps"

"github.com/twpayne/chezmoi/v2/internal/chezmoi"
Expand Down Expand Up @@ -81,13 +80,13 @@ func (c *Config) runUnmanagedCmd(
if fileInfo.IsDir() {
switch {
case !managed:
return vfs.SkipDir
return fs.SkipDir
case ignored:
return vfs.SkipDir
return fs.SkipDir
case sourceStateEntry != nil:
if external, ok := sourceStateEntry.Origin().(*chezmoi.External); ok {
if external.Type == chezmoi.ExternalTypeGitRepo {
return vfs.SkipDir
return fs.SkipDir
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/upgradecmd.go
Expand Up @@ -398,7 +398,7 @@ func (c *Config) replaceExecutable(
if err != nil {
return err
}
return chezmoi.Break
return fs.SkipAll
default:
return nil
}
Expand Down

0 comments on commit 424189b

Please sign in to comment.