Skip to content

Commit

Permalink
snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Apr 9, 2020
1 parent 30176dd commit 105b208
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 39 deletions.
7 changes: 4 additions & 3 deletions v2/chezmoi/destdir.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package chezmoi

// FIXME do we need Stat?
// FIXME do we need a more specific FileReader interface with just ReadFile?

import (
"os"
"os/exec"
)

// A DestDirReader reads from a destination directory.
type DestDirReader interface {
// A DirReader reads from a directory.
type DirReader interface {
Glob(pattern string) ([]string, error)
Lstat(filename string) (os.FileInfo, error)
ReadDir(dirname string) ([]os.FileInfo, error)
Expand All @@ -19,7 +20,7 @@ type DestDirReader interface {

// A DestDir makes changes to a destination directory.
type DestDir interface {
DestDirReader
DirReader
Chmod(name string, mode os.FileMode) error
IdempotentCmdOutput(cmd *exec.Cmd) ([]byte, error)
Mkdir(name string, perm os.FileMode) error
Expand Down
5 changes: 3 additions & 2 deletions v2/chezmoi/deststateentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ type DestStateSymlink struct {
*lazyLinkname
}

// NewDestStateEntry returns a new DestStateEntry populated with path from fs.
func NewDestStateEntry(destDirReader DestDirReader, path string) (DestStateEntry, error) {
// NewDestStateEntry returns a new DestStateEntry populated with path from
// destDirReader.
func NewDestStateEntry(destDirReader DirReader, path string) (DestStateEntry, error) {
info, err := destDirReader.Lstat(path)
switch {
case os.IsNotExist(err):
Expand Down
2 changes: 1 addition & 1 deletion v2/chezmoi/emptydestdir_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package chezmoi

var _ DestDirReader = &EmptyDestDir{}
var _ DirReader = &EmptyDestDir{}
8 changes: 8 additions & 0 deletions v2/chezmoi/private_posix.go → v2/chezmoi/private.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
package chezmoi

import (
"runtime"

vfs "github.com/twpayne/go-vfs"
)

// IsPrivate returns whether path should be considered private.
func IsPrivate(fs vfs.Stater, path string, want bool) (bool, error) {
// Private has no real equivalent on Windows, so always return what the
// caller wants.
if runtime.GOOS == "windows" {
return want, nil
}

info, err := fs.Stat(path)
if err != nil {
return false, err
Expand Down
12 changes: 0 additions & 12 deletions v2/chezmoi/private_windows.go

This file was deleted.

30 changes: 15 additions & 15 deletions v2/chezmoi/sourcestate.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package chezmoi

// FIXME use vfs.PathFS instead of targetDir arg
// FIXME accumulate all source state warnings/errors

import (
Expand Down Expand Up @@ -68,7 +69,7 @@ func NewSourceState(options ...SourceStateOption) *SourceState {
return s
}

// ApplyAll updates targetDir in fs to match s using destDir. FIXME comment
// ApplyAll updates targetDir in destDir to match s.
func (s *SourceState) ApplyAll(destDir DestDir, umask os.FileMode, targetDir string) error {
for _, targetName := range s.sortedTargetNames() {
if err := s.ApplyOne(destDir, umask, targetDir, targetName); err != nil {
Expand All @@ -78,7 +79,7 @@ func (s *SourceState) ApplyAll(destDir DestDir, umask os.FileMode, targetDir str
return nil
}

// ApplyOne updates targetName in targetDir on fs to match s using destDir. FIXME comment
// ApplyOne updates targetName in targetDir on fs to match s using destDir.
func (s *SourceState) ApplyOne(destDir DestDir, umask os.FileMode, targetDir, targetName string) error {
targetPath := path.Join(targetDir, targetName)
destStateEntry, err := NewDestStateEntry(destDir, targetPath)
Expand Down Expand Up @@ -138,9 +139,9 @@ func (s *SourceState) ExecuteTemplateData(name string, data []byte) ([]byte, err
}

// Read reads a source state from sourcePath in fs.
func (s *SourceState) Read(fs vfs.FS, sourceDir string) error {
func (s *SourceState) Read(dirReader DirReader, sourceDir string) error {
sourceDirPrefix := filepath.ToSlash(sourceDir) + pathSeparator
return vfs.Walk(fs, sourceDir, func(sourcePath string, info os.FileInfo, err error) error {
return vfs.Walk(dirReader, sourceDir, func(sourcePath string, info os.FileInfo, err error) error {
sourcePath = filepath.ToSlash(sourcePath)
if err != nil {
return err
Expand All @@ -153,16 +154,16 @@ func (s *SourceState) Read(fs vfs.FS, sourceDir string) error {
targetDirName := getTargetDirName(dir)
switch {
case info.Name() == ignoreName:
return s.addPatterns(fs, s.ignore, sourcePath, dir)
return s.addPatterns(dirReader, s.ignore, sourcePath, dir)
case info.Name() == removeName:
return s.addPatterns(fs, s.remove, sourcePath, targetDirName)
return s.addPatterns(dirReader, s.remove, sourcePath, targetDirName)
case info.Name() == templatesDirName:
if err := s.addTemplatesDir(fs, sourcePath); err != nil {
if err := s.addTemplatesDir(dirReader, sourcePath); err != nil {
return err
}
return filepath.SkipDir
case info.Name() == versionName:
data, err := fs.ReadFile(sourcePath)
data, err := dirReader.ReadFile(sourcePath)
if err != nil {
return err
}
Expand Down Expand Up @@ -215,12 +216,12 @@ func (s *SourceState) Read(fs vfs.FS, sourceDir string) error {
}
}
s.entries[targetName] = &SourceStateFile{
fs: fs,
dirReader: dirReader,
path: sourcePath,
attributes: fileAttributes,
lazyContents: &lazyContents{
contentsFunc: func() ([]byte, error) {
return fs.ReadFile(sourcePath)
return dirReader.ReadFile(sourcePath)
},
},
}
Expand Down Expand Up @@ -260,7 +261,6 @@ func (s *SourceState) Remove(destDir DestDir, umask os.FileMode, targetDir strin
return err
}
}

return nil
}

Expand All @@ -274,7 +274,7 @@ func (s *SourceState) Evaluate(umask os.FileMode) error {
return nil
}

func (s *SourceState) addPatterns(fs vfs.FS, ps *PatternSet, path, relPath string) error {
func (s *SourceState) addPatterns(fs DirReader, ps *PatternSet, path, relPath string) error {
data, err := s.executeTemplate(fs, path)
if err != nil {
return err
Expand Down Expand Up @@ -306,7 +306,7 @@ func (s *SourceState) addPatterns(fs vfs.FS, ps *PatternSet, path, relPath strin
return nil
}

func (s *SourceState) addTemplatesDir(fs vfs.FS, templateDir string) error {
func (s *SourceState) addTemplatesDir(fs DirReader, templateDir string) error {
templateDirPrefix := filepath.ToSlash(templateDir) + pathSeparator
return vfs.Walk(fs, templateDir, func(templatePath string, info os.FileInfo, err error) error {
templatePath = filepath.ToSlash(templatePath)
Expand Down Expand Up @@ -340,8 +340,8 @@ func (s *SourceState) addTemplatesDir(fs vfs.FS, templateDir string) error {
})
}

func (s *SourceState) executeTemplate(fs vfs.FS, path string) ([]byte, error) {
data, err := fs.ReadFile(path)
func (s *SourceState) executeTemplate(dirReader DirReader, path string) ([]byte, error) {
data, err := dirReader.ReadFile(path)
if err != nil {
return nil, err
}
Expand Down
10 changes: 4 additions & 6 deletions v2/chezmoi/sourcestateentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package chezmoi

import (
"os"

vfs "github.com/twpayne/go-vfs"
)

// A SourceStateEntry represents the state of an entry in the source state.
Expand All @@ -20,7 +18,7 @@ type SourceStateDir struct {

// A SourceStateFile represents the state of a file in the source state.
type SourceStateFile struct {
fs vfs.FS
dirReader DirReader
path string
attributes FileAttributes
*lazyContents
Expand Down Expand Up @@ -63,7 +61,7 @@ func (s *SourceStateFile) TargetStateEntry(umask os.FileMode) TargetStateEntry {
perm: perm &^ umask,
lazyContents: &lazyContents{
contentsFunc: func() ([]byte, error) {
return s.fs.ReadFile(s.path)
return s.dirReader.ReadFile(s.path)
},
},
}
Expand All @@ -72,15 +70,15 @@ func (s *SourceStateFile) TargetStateEntry(umask os.FileMode) TargetStateEntry {
name: s.attributes.Name,
lazyContents: &lazyContents{
contentsFunc: func() ([]byte, error) {
return s.fs.ReadFile(s.path)
return s.dirReader.ReadFile(s.path)
},
},
}
case SourceFileTypeSymlink:
return &TargetStateSymlink{
lazyLinkname: &lazyLinkname{
linknameFunc: func() (string, error) {
linknameBytes, err := s.fs.ReadFile(s.path)
linknameBytes, err := s.dirReader.ReadFile(s.path)
if err != nil {
return "", err
}
Expand Down

0 comments on commit 105b208

Please sign in to comment.