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) {
12981299func (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