Skip to content

Commit

Permalink
Rename ParsedSourceFileName to FileAttributes
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Dec 17, 2018
1 parent 9264001 commit c2da0c0
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 83 deletions.
16 changes: 8 additions & 8 deletions cmd/chattr.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,22 @@ func (c *Config) runChattrCommand(fs vfs.FS, cmd *cobra.Command, args []string)
}
newSourceName = da.SourceName()
case *chezmoi.File:
psfn := chezmoi.ParseSourceFileName(oldSourceName)
fa := chezmoi.ParseFileAttributes(oldSourceName)
mode := os.FileMode(0666)
if executable := ams.executable.modify(entry.Executable()); executable {
mode |= 0111
}
if private := ams.private.modify(entry.Private()); private {
mode &= 0700
}
psfn.Mode = mode
psfn.Empty = ams.empty.modify(entry.Empty)
psfn.Template = ams.template.modify(entry.Template)
newSourceName = psfn.SourceFileName()
fa.Mode = mode
fa.Empty = ams.empty.modify(entry.Empty)
fa.Template = ams.template.modify(entry.Template)
newSourceName = fa.SourceName()
case *chezmoi.Symlink:
psfn := chezmoi.ParseSourceFileName(oldSourceName)
psfn.Template = ams.template.modify(entry.Template)
newSourceName = psfn.SourceFileName()
fa := chezmoi.ParseFileAttributes(oldSourceName)
fa.Template = ams.template.modify(entry.Template)
newSourceName = fa.SourceName()
}
if newSourceName != oldSourceName {
renames[filepath.Join(ts.SourceDir, oldSourceName)] = filepath.Join(ts.SourceDir, newSourceName)
Expand Down
81 changes: 41 additions & 40 deletions lib/chezmoi/chezmoi.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ type DirAttributes struct {
Perm os.FileMode
}

// A ParsedSourceFileName is a parsed source file name.
type ParsedSourceFileName struct {
// A FileAttributes is a parsed source file name.
type FileAttributes struct {
FileName string
Mode os.FileMode
Empty bool
Template bool
}

type parsedSourceFilePath struct {
ParsedSourceFileName
FileAttributes
dirNames []string
}

Expand Down Expand Up @@ -93,75 +93,76 @@ func (da DirAttributes) SourceName() string {
return sourceName
}

// ParseSourceFileName parses a source file name.
func ParseSourceFileName(fileName string) ParsedSourceFileName {
// ParseFileAttributes parses a source file name.
func ParseFileAttributes(sourceName string) FileAttributes {
name := sourceName
mode := os.FileMode(0666)
empty := false
template := false
if strings.HasPrefix(fileName, symlinkPrefix) {
fileName = strings.TrimPrefix(fileName, symlinkPrefix)
if strings.HasPrefix(name, symlinkPrefix) {
name = strings.TrimPrefix(name, symlinkPrefix)
mode |= os.ModeSymlink
} else {
private := false
if strings.HasPrefix(fileName, privatePrefix) {
fileName = strings.TrimPrefix(fileName, privatePrefix)
if strings.HasPrefix(name, privatePrefix) {
name = strings.TrimPrefix(name, privatePrefix)
private = true
}
if strings.HasPrefix(fileName, emptyPrefix) {
fileName = strings.TrimPrefix(fileName, emptyPrefix)
if strings.HasPrefix(name, emptyPrefix) {
name = strings.TrimPrefix(name, emptyPrefix)
empty = true
}
if strings.HasPrefix(fileName, executablePrefix) {
fileName = strings.TrimPrefix(fileName, executablePrefix)
if strings.HasPrefix(name, executablePrefix) {
name = strings.TrimPrefix(name, executablePrefix)
mode |= 0111
}
if private {
mode &= 0700
}
}
if strings.HasPrefix(fileName, dotPrefix) {
fileName = "." + strings.TrimPrefix(fileName, dotPrefix)
if strings.HasPrefix(name, dotPrefix) {
name = "." + strings.TrimPrefix(name, dotPrefix)
}
if strings.HasSuffix(fileName, templateSuffix) {
fileName = strings.TrimSuffix(fileName, templateSuffix)
if strings.HasSuffix(name, templateSuffix) {
name = strings.TrimSuffix(name, templateSuffix)
template = true
}
return ParsedSourceFileName{
FileName: fileName,
return FileAttributes{
FileName: name,
Mode: mode,
Empty: empty,
Template: template,
}
}

// SourceFileName returns psfn's source file name.
func (psfn ParsedSourceFileName) SourceFileName() string {
fileName := ""
switch psfn.Mode & os.ModeType {
// SourceName returns fa's source name.
func (fa FileAttributes) SourceName() string {
sourceName := ""
switch fa.Mode & os.ModeType {
case 0:
if psfn.Mode.Perm()&os.FileMode(077) == os.FileMode(0) {
fileName = privatePrefix
if fa.Mode.Perm()&os.FileMode(077) == os.FileMode(0) {
sourceName = privatePrefix
}
if psfn.Empty {
fileName += emptyPrefix
if fa.Empty {
sourceName += emptyPrefix
}
if psfn.Mode.Perm()&os.FileMode(0111) != os.FileMode(0) {
fileName += executablePrefix
if fa.Mode.Perm()&os.FileMode(0111) != os.FileMode(0) {
sourceName += executablePrefix
}
case os.ModeSymlink:
fileName = symlinkPrefix
sourceName = symlinkPrefix
default:
panic(fmt.Sprintf("%+v: unsupported type", psfn)) // FIXME return error instead of panicing
panic(fmt.Sprintf("%+v: unsupported type", fa)) // FIXME return error instead of panicing
}
if strings.HasPrefix(psfn.FileName, ".") {
fileName += dotPrefix + strings.TrimPrefix(psfn.FileName, ".")
if strings.HasPrefix(fa.FileName, ".") {
sourceName += dotPrefix + strings.TrimPrefix(fa.FileName, ".")
} else {
fileName += psfn.FileName
sourceName += fa.FileName
}
if psfn.Template {
fileName += templateSuffix
if fa.Template {
sourceName += templateSuffix
}
return fileName
return sourceName
}

// parseDirNameComponents parses multiple directory name components. It returns
Expand All @@ -181,10 +182,10 @@ func parseDirNameComponents(components []string) ([]string, []os.FileMode) {
func parseSourceFilePath(path string) parsedSourceFilePath {
components := splitPathList(path)
dirNames, _ := parseDirNameComponents(components[0 : len(components)-1])
psfn := ParseSourceFileName(components[len(components)-1])
fa := ParseFileAttributes(components[len(components)-1])
return parsedSourceFilePath{
ParsedSourceFileName: psfn,
dirNames: dirNames,
FileAttributes: fa,
dirNames: dirNames,
}
}

Expand Down
62 changes: 31 additions & 31 deletions lib/chezmoi/chezmoi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,113 +56,113 @@ func TestDirAttributes(t *testing.T) {
}
}

func TestSourceFileName(t *testing.T) {
func TestFileAttributes(t *testing.T) {
for _, tc := range []struct {
sourceFileName string
psfn ParsedSourceFileName
sourceName string
fa FileAttributes
}{
{
sourceFileName: "foo",
psfn: ParsedSourceFileName{
sourceName: "foo",
fa: FileAttributes{
FileName: "foo",
Mode: 0666,
Empty: false,
Template: false,
},
},
{
sourceFileName: "dot_foo",
psfn: ParsedSourceFileName{
sourceName: "dot_foo",
fa: FileAttributes{
FileName: ".foo",
Mode: 0666,
Empty: false,
Template: false,
},
},
{
sourceFileName: "private_foo",
psfn: ParsedSourceFileName{
sourceName: "private_foo",
fa: FileAttributes{
FileName: "foo",
Mode: 0600,
Empty: false,
Template: false,
},
},
{
sourceFileName: "private_dot_foo",
psfn: ParsedSourceFileName{
sourceName: "private_dot_foo",
fa: FileAttributes{
FileName: ".foo",
Mode: 0600,
Empty: false,
Template: false,
},
},
{
sourceFileName: "empty_foo",
psfn: ParsedSourceFileName{
sourceName: "empty_foo",
fa: FileAttributes{
FileName: "foo",
Mode: 0666,
Empty: true,
Template: false,
},
},
{
sourceFileName: "executable_foo",
psfn: ParsedSourceFileName{
sourceName: "executable_foo",
fa: FileAttributes{
FileName: "foo",
Mode: 0777,
Empty: false,
Template: false,
},
},
{
sourceFileName: "foo.tmpl",
psfn: ParsedSourceFileName{
sourceName: "foo.tmpl",
fa: FileAttributes{
FileName: "foo",
Mode: 0666,
Empty: false,
Template: true,
},
},
{
sourceFileName: "private_executable_dot_foo.tmpl",
psfn: ParsedSourceFileName{
sourceName: "private_executable_dot_foo.tmpl",
fa: FileAttributes{
FileName: ".foo",
Mode: 0700,
Empty: false,
Template: true,
},
},
{
sourceFileName: "symlink_foo",
psfn: ParsedSourceFileName{
sourceName: "symlink_foo",
fa: FileAttributes{
FileName: "foo",
Mode: os.ModeSymlink | 0666,
},
},
{
sourceFileName: "symlink_dot_foo",
psfn: ParsedSourceFileName{
sourceName: "symlink_dot_foo",
fa: FileAttributes{
FileName: ".foo",
Mode: os.ModeSymlink | 0666,
},
},
{
sourceFileName: "symlink_foo.tmpl",
psfn: ParsedSourceFileName{
sourceName: "symlink_foo.tmpl",
fa: FileAttributes{
FileName: "foo",
Mode: os.ModeSymlink | 0666,
Template: true,
},
},
} {
t.Run(tc.sourceFileName, func(t *testing.T) {
gotPSFN := ParseSourceFileName(tc.sourceFileName)
if diff, equal := messagediff.PrettyDiff(tc.psfn, gotPSFN); !equal {
t.Errorf("ParseSourceFileName(%q) == %+v, want %+v, diff:\n%s", tc.sourceFileName, gotPSFN, tc.psfn, diff)
t.Run(tc.sourceName, func(t *testing.T) {
gotFA := ParseFileAttributes(tc.sourceName)
if diff, equal := messagediff.PrettyDiff(tc.fa, gotFA); !equal {
t.Errorf("ParseFileAttributes(%q) == %+v, want %+v, diff:\n%s", tc.sourceName, gotFA, tc.fa, diff)
}
if gotSourceFileName := tc.psfn.SourceFileName(); gotSourceFileName != tc.sourceFileName {
t.Errorf("%+v.SourceFileName() == %q, want %q", tc.psfn, gotSourceFileName, tc.sourceFileName)
if gotSourceName := tc.fa.SourceName(); gotSourceName != tc.sourceName {
t.Errorf("%+v.SourceName() == %q, want %q", tc.fa, gotSourceName, tc.sourceName)
}
})
}
Expand Down
8 changes: 4 additions & 4 deletions lib/chezmoi/target_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,12 @@ func (ts *TargetState) addFile(targetName string, entries map[string]Entry, pare
}
perm := info.Mode().Perm()
empty := info.Size() == 0
sourceName := ParsedSourceFileName{
sourceName := FileAttributes{
FileName: name,
Mode: perm,
Empty: empty,
Template: template,
}.SourceFileName()
}.SourceName()
if parentDirSourceName != "" {
sourceName = filepath.Join(parentDirSourceName, sourceName)
}
Expand Down Expand Up @@ -398,10 +398,10 @@ func (ts *TargetState) addSymlink(targetName string, entries map[string]Entry, p
return err
}
}
sourceName := ParsedSourceFileName{
sourceName := FileAttributes{
FileName: name,
Mode: os.ModeSymlink,
}.SourceFileName()
}.SourceName()
if parentDirSourceName != "" {
sourceName = filepath.Join(parentDirSourceName, sourceName)
}
Expand Down

0 comments on commit c2da0c0

Please sign in to comment.