Skip to content

Commit 034b8d3

Browse files
committed
Use io/fs package instead of os
1 parent df0ea51 commit 034b8d3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+496
-499
lines changed

.golangci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ linters-settings:
8080
forbidigo:
8181
forbid:
8282
- ^fmt\.Print.*$
83-
- ^ioutil\.
83+
- ^ioutil\..*$
84+
- ^os\.(DirEntry|FileInfo|FileMode|Is.*|Mode.*)$
8485
gofumpt:
8586
extra-rules: true
8687
goimports:

cmd/addcmd_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/require"
7-
"github.com/twpayne/go-vfs/v2"
8-
"github.com/twpayne/go-vfs/v2/vfst"
7+
"github.com/twpayne/go-vfs/v3"
8+
"github.com/twpayne/go-vfs/v3/vfst"
99

1010
"github.com/twpayne/chezmoi/v2/internal/chezmoitest"
1111
)
@@ -235,9 +235,9 @@ func TestAddCmd(t *testing.T) {
235235
} {
236236
t.Run(tc.name, func(t *testing.T) {
237237
chezmoitest.SkipUnlessGOOS(t, tc.name)
238-
chezmoitest.WithTestFS(t, tc.root, func(fs vfs.FS) {
239-
require.NoError(t, newTestConfig(t, fs).execute(append([]string{"add"}, tc.args...)))
240-
vfst.RunTests(t, fs, "", tc.tests...)
238+
chezmoitest.WithTestFS(t, tc.root, func(fileSystem vfs.FS) {
239+
require.NoError(t, newTestConfig(t, fileSystem).execute(append([]string{"add"}, tc.args...)))
240+
vfst.RunTests(t, fileSystem, "", tc.tests...)
241241
})
242242
})
243243
}

cmd/applycmd_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package cmd
22

33
import (
4-
"os"
4+
"io/fs"
55
"path/filepath"
66
"testing"
77

88
"github.com/stretchr/testify/require"
9-
"github.com/twpayne/go-vfs/v2"
10-
"github.com/twpayne/go-vfs/v2/vfst"
9+
"github.com/twpayne/go-vfs/v3"
10+
"github.com/twpayne/go-vfs/v3/vfst"
1111

1212
"github.com/twpayne/chezmoi/v2/internal/chezmoitest"
1313
)
@@ -69,7 +69,7 @@ func TestApplyCmd(t *testing.T) {
6969
vfst.TestDoesNotExist,
7070
),
7171
vfst.TestPath("/home/user/.symlink",
72-
vfst.TestModeType(os.ModeSymlink),
72+
vfst.TestModeType(fs.ModeSymlink),
7373
vfst.TestSymlinkTarget(filepath.FromSlash(".dir/subdir/file")),
7474
),
7575
vfst.TestPath("/home/user/.template",
@@ -201,12 +201,12 @@ func TestApplyCmd(t *testing.T) {
201201
},
202202
},
203203
},
204-
}, func(fs vfs.FS) {
204+
}, func(fileSystem vfs.FS) {
205205
if tc.extraRoot != nil {
206-
require.NoError(t, vfst.NewBuilder().Build(fs, tc.extraRoot))
206+
require.NoError(t, vfst.NewBuilder().Build(fileSystem, tc.extraRoot))
207207
}
208-
require.NoError(t, newTestConfig(t, fs).execute(append([]string{"apply"}, tc.args...)))
209-
vfst.RunTests(t, fs, "", tc.tests)
208+
require.NoError(t, newTestConfig(t, fileSystem).execute(append([]string{"apply"}, tc.args...)))
209+
vfst.RunTests(t, fileSystem, "", tc.tests)
210210
})
211211
})
212212
}

cmd/config.go

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"io"
9+
"io/fs"
910
"os"
1011
"os/exec"
1112
"os/user"
@@ -25,12 +26,12 @@ import (
2526
"github.com/mitchellh/mapstructure"
2627
"github.com/rs/zerolog"
2728
"github.com/rs/zerolog/log"
29+
"github.com/spf13/afero"
2830
"github.com/spf13/cobra"
2931
"github.com/spf13/viper"
3032
"github.com/twpayne/go-shell"
31-
"github.com/twpayne/go-vfs/v2"
32-
vfsafero "github.com/twpayne/go-vfsafero/v2"
33-
"github.com/twpayne/go-xdg/v4"
33+
"github.com/twpayne/go-vfs/v3"
34+
"github.com/twpayne/go-xdg/v6"
3435
"golang.org/x/term"
3536

3637
"github.com/twpayne/chezmoi/v2/assets/templates"
@@ -51,7 +52,7 @@ type Config struct {
5152
// Global configuration, settable in the config file.
5253
SourceDirAbsPath chezmoi.AbsPath `mapstructure:"sourceDir"`
5354
DestDirAbsPath chezmoi.AbsPath `mapstructure:"destDir"`
54-
Umask os.FileMode `mapstructure:"umask"`
55+
Umask fs.FileMode `mapstructure:"umask"`
5556
Remove bool `mapstructure:"remove"`
5657
Color *autoBool `mapstructure:"color"`
5758
Data map[string]interface{} `mapstructure:"data"`
@@ -123,7 +124,7 @@ type Config struct {
123124
versionStr string
124125

125126
// Configuration.
126-
fs vfs.FS
127+
fileSystem vfs.FS
127128
bds *xdg.BaseDirectorySpecification
128129
configFileAbsPath chezmoi.AbsPath
129130
baseSystem chezmoi.System
@@ -195,10 +196,10 @@ func newConfig(options ...configOption) (*Config, error) {
195196
}
196197

197198
c := &Config{
198-
bds: bds,
199-
fs: vfs.OSFS,
200-
homeDir: userHomeDir,
201-
Umask: chezmoi.Umask,
199+
bds: bds,
200+
fileSystem: vfs.OSFS,
201+
homeDir: userHomeDir,
202+
Umask: chezmoi.Umask,
202203
Add: addCmdConfig{
203204
exclude: chezmoi.NewEntryTypeSet(chezmoi.EntryTypesNone),
204205
include: chezmoi.NewEntryTypeSet(chezmoi.EntryTypesAll),
@@ -357,11 +358,11 @@ func newConfig(options ...configOption) (*Config, error) {
357358
if err != nil {
358359
return nil, err
359360
}
360-
c.configFileAbsPath, err = c.defaultConfigFile(c.fs, c.bds)
361+
c.configFileAbsPath, err = c.defaultConfigFile(c.fileSystem, c.bds)
361362
if err != nil {
362363
return nil, err
363364
}
364-
c.SourceDirAbsPath, err = c.defaultSourceDir(c.fs, c.bds)
365+
c.SourceDirAbsPath, err = c.defaultSourceDir(c.fileSystem, c.bds)
365366
if err != nil {
366367
return nil, err
367368
}
@@ -384,7 +385,7 @@ type applyArgsOptions struct {
384385
include *chezmoi.EntryTypeSet
385386
exclude *chezmoi.EntryTypeSet
386387
recursive bool
387-
umask os.FileMode
388+
umask fs.FileMode
388389
preApplyFunc chezmoi.PreApplyFunc
389390
}
390391

@@ -505,7 +506,7 @@ func (c *Config) colorAutoFunc() (bool, error) {
505506

506507
// defaultConfigFile returns the default config file according to the XDG Base
507508
// Directory Specification.
508-
func (c *Config) defaultConfigFile(fs vfs.Stater, bds *xdg.BaseDirectorySpecification) (chezmoi.AbsPath, error) {
509+
func (c *Config) defaultConfigFile(fileSystem vfs.Stater, bds *xdg.BaseDirectorySpecification) (chezmoi.AbsPath, error) {
509510
// Search XDG Base Directory Specification config directories first.
510511
for _, configDir := range bds.ConfigDirs {
511512
configDirAbsPath, err := chezmoi.NewAbsPathFromExtPath(configDir, c.homeDirAbsPath)
@@ -514,7 +515,7 @@ func (c *Config) defaultConfigFile(fs vfs.Stater, bds *xdg.BaseDirectorySpecific
514515
}
515516
for _, extension := range viper.SupportedExts {
516517
configFileAbsPath := configDirAbsPath.Join("chezmoi", chezmoi.RelPath("chezmoi."+extension))
517-
if _, err := fs.Stat(string(configFileAbsPath)); err == nil {
518+
if _, err := fileSystem.Stat(string(configFileAbsPath)); err == nil {
518519
return configFileAbsPath, nil
519520
}
520521
}
@@ -572,15 +573,15 @@ func (c *Config) defaultPreApplyFunc(targetRelPath chezmoi.RelPath, targetEntryS
572573

573574
// defaultSourceDir returns the default source directory according to the XDG
574575
// Base Directory Specification.
575-
func (c *Config) defaultSourceDir(fs vfs.Stater, bds *xdg.BaseDirectorySpecification) (chezmoi.AbsPath, error) {
576+
func (c *Config) defaultSourceDir(fileSystem vfs.Stater, bds *xdg.BaseDirectorySpecification) (chezmoi.AbsPath, error) {
576577
// Check for XDG Base Directory Specification data directories first.
577578
for _, dataDir := range bds.DataDirs {
578579
dataDirAbsPath, err := chezmoi.NewAbsPathFromExtPath(dataDir, c.homeDirAbsPath)
579580
if err != nil {
580581
return "", err
581582
}
582583
sourceDirAbsPath := dataDirAbsPath.Join("chezmoi")
583-
if _, err := fs.Stat(string(sourceDirAbsPath)); err == nil {
584+
if _, err := fileSystem.Stat(string(sourceDirAbsPath)); err == nil {
584585
return sourceDirAbsPath, nil
585586
}
586587
}
@@ -659,7 +660,7 @@ func (c *Config) defaultTemplateData() map[string]interface{} {
659660
}
660661
}
661662

662-
if fqdnHostname := chezmoi.FQDNHostname(c.fs); fqdnHostname != "" {
663+
if fqdnHostname := chezmoi.FQDNHostname(c.fileSystem); fqdnHostname != "" {
663664
data["fqdnHostname"] = fqdnHostname
664665
}
665666

@@ -671,15 +672,15 @@ func (c *Config) defaultTemplateData() map[string]interface{} {
671672
Msg("os.Hostname")
672673
}
673674

674-
if kernelInfo, err := chezmoi.KernelInfo(c.fs); err == nil {
675+
if kernelInfo, err := chezmoi.KernelInfo(c.fileSystem); err == nil {
675676
data["kernel"] = kernelInfo
676677
} else {
677678
log.Debug().
678679
Err(err).
679680
Msg("chezmoi.KernelInfo")
680681
}
681682

682-
if osRelease, err := chezmoi.OSRelease(c.fs); err == nil {
683+
if osRelease, err := chezmoi.OSRelease(c.fileSystem); err == nil {
683684
data["osRelease"] = upperSnakeCaseToCamelCaseMap(osRelease)
684685
} else {
685686
log.Debug().
@@ -692,8 +693,8 @@ func (c *Config) defaultTemplateData() map[string]interface{} {
692693
}
693694
}
694695

695-
func (c *Config) destAbsPathInfos(sourceState *chezmoi.SourceState, args []string, recursive, follow bool) (map[chezmoi.AbsPath]os.FileInfo, error) {
696-
destAbsPathInfos := make(map[chezmoi.AbsPath]os.FileInfo)
696+
func (c *Config) destAbsPathInfos(sourceState *chezmoi.SourceState, args []string, recursive, follow bool) (map[chezmoi.AbsPath]fs.FileInfo, error) {
697+
destAbsPathInfos := make(map[chezmoi.AbsPath]fs.FileInfo)
697698
for _, arg := range args {
698699
destAbsPath, err := chezmoi.NewAbsPathFromExtPath(arg, c.homeDirAbsPath)
699700
if err != nil {
@@ -703,11 +704,11 @@ func (c *Config) destAbsPathInfos(sourceState *chezmoi.SourceState, args []strin
703704
return nil, err
704705
}
705706
if recursive {
706-
if err := chezmoi.Walk(c.destSystem, destAbsPath, func(destAbsPath chezmoi.AbsPath, info os.FileInfo, err error) error {
707+
if err := chezmoi.Walk(c.destSystem, destAbsPath, func(destAbsPath chezmoi.AbsPath, info fs.FileInfo, err error) error {
707708
if err != nil {
708709
return err
709710
}
710-
if follow && info.Mode()&os.ModeType == os.ModeSymlink {
711+
if follow && info.Mode().Type() == fs.ModeSymlink {
711712
info, err = c.destSystem.Stat(destAbsPath)
712713
if err != nil {
713714
return err
@@ -718,7 +719,7 @@ func (c *Config) destAbsPathInfos(sourceState *chezmoi.SourceState, args []strin
718719
return nil, err
719720
}
720721
} else {
721-
var info os.FileInfo
722+
var info fs.FileInfo
722723
if follow {
723724
info, err = c.destSystem.Stat(destAbsPath)
724725
} else {
@@ -735,7 +736,7 @@ func (c *Config) destAbsPathInfos(sourceState *chezmoi.SourceState, args []strin
735736
return destAbsPathInfos, nil
736737
}
737738

738-
func (c *Config) diffFile(path chezmoi.RelPath, fromData []byte, fromMode os.FileMode, toData []byte, toMode os.FileMode) error {
739+
func (c *Config) diffFile(path chezmoi.RelPath, fromData []byte, fromMode fs.FileMode, toData []byte, toMode fs.FileMode) error {
739740
var sb strings.Builder
740741
unifiedEncoder := diff.NewUnifiedEncoder(&sb, diff.DefaultContextLines)
741742
color, err := c.Color.Value()
@@ -806,7 +807,7 @@ func (c *Config) doPurge(purgeOptions *purgeOptions) error {
806807
// Remove all paths that exist.
807808
for _, absPath := range absPaths {
808809
switch _, err := c.destSystem.Stat(absPath); {
809-
case os.IsNotExist(err):
810+
case errors.Is(err, fs.ErrNotExist):
810811
continue
811812
case err != nil:
812813
return err
@@ -827,7 +828,7 @@ func (c *Config) doPurge(purgeOptions *purgeOptions) error {
827828
}
828829

829830
switch err := c.destSystem.RemoveAll(absPath); {
830-
case os.IsPermission(err):
831+
case errors.Is(err, fs.ErrPermission):
831832
continue
832833
case err != nil:
833834
return err
@@ -887,7 +888,7 @@ func (c *Config) findConfigTemplate() (chezmoi.RelPath, string, []byte, error) {
887888
filename := chezmoi.RelPath(chezmoi.Prefix + "." + ext + chezmoi.TemplateSuffix)
888889
contents, err := c.baseSystem.ReadFile(c.SourceDirAbsPath.Join(filename))
889890
switch {
890-
case os.IsNotExist(err):
891+
case errors.Is(err, fs.ErrNotExist):
891892
continue
892893
case err != nil:
893894
return "", "", nil, err
@@ -1063,7 +1064,7 @@ func (c *Config) persistentPostRunRootE(cmd *cobra.Command, args []string) error
10631064
if boolAnnotation(cmd, modifiesConfigFile) {
10641065
// Warn the user of any errors reading the config file.
10651066
v := viper.New()
1066-
v.SetFs(vfsafero.NewAferoFS(c.fs))
1067+
v.SetFs(afero.FromIOFS{FS: c.fileSystem})
10671068
v.SetConfigFile(string(c.configFileAbsPath))
10681069
err := v.ReadInConfig()
10691070
if err == nil {
@@ -1139,7 +1140,7 @@ func (c *Config) persistentPreRunRootE(cmd *cobra.Command, args []string) error
11391140
zerolog.SetGlobalLevel(zerolog.Disabled)
11401141
}
11411142

1142-
c.baseSystem = chezmoi.NewRealSystem(c.fs)
1143+
c.baseSystem = chezmoi.NewRealSystem(c.fileSystem)
11431144
if c.debug {
11441145
c.baseSystem = chezmoi.NewDebugSystem(c.baseSystem)
11451146
}
@@ -1274,7 +1275,7 @@ func (c *Config) persistentStateFile() (chezmoi.AbsPath, error) {
12741275
return persistentStateFile, nil
12751276
}
12761277
}
1277-
defaultConfigFileAbsPath, err := c.defaultConfigFile(c.fs, c.bds)
1278+
defaultConfigFileAbsPath, err := c.defaultConfigFile(c.fileSystem, c.bds)
12781279
if err != nil {
12791280
return "", err
12801281
}
@@ -1298,9 +1299,9 @@ func (c *Config) promptChoice(prompt string, choices []string) (string, error) {
12981299
func (c *Config) readConfig() error {
12991300
v := viper.New()
13001301
v.SetConfigFile(string(c.configFileAbsPath))
1301-
v.SetFs(vfsafero.NewAferoFS(c.fs))
1302+
v.SetFs(afero.FromIOFS{FS: c.fileSystem})
13021303
switch err := v.ReadInConfig(); {
1303-
case os.IsNotExist(err):
1304+
case errors.Is(err, fs.ErrNotExist):
13041305
return nil
13051306
case err != nil:
13061307
return err

0 commit comments

Comments
 (0)