Skip to content

Commit d8f8445

Browse files
committed
Sort functions alphabetically
1 parent 1c8d357 commit d8f8445

File tree

8 files changed

+214
-214
lines changed

8 files changed

+214
-214
lines changed

cmd/add_test.go

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,47 @@ import (
77
"github.com/twpayne/go-vfs/vfst"
88
)
99

10+
func TestAddAfterModification(t *testing.T) {
11+
c := &Config{
12+
SourceDir: "/home/user/.chezmoi",
13+
TargetDir: "/home/user",
14+
Umask: 022,
15+
DryRun: false,
16+
Verbose: true,
17+
}
18+
fs, cleanup, err := vfst.NewTestFS(map[string]interface{}{
19+
"/home/user": &vfst.Dir{Perm: 0755},
20+
"/home/user/.chezmoi": &vfst.Dir{Perm: 0700},
21+
"/home/user/.bashrc": "# contents of .bashrc\n",
22+
})
23+
defer cleanup()
24+
if err != nil {
25+
t.Fatalf("vfst.NewTestFS(_) == _, _, %v, want _, _, <nil>", err)
26+
}
27+
args := []string{"/home/user/.bashrc"}
28+
if err := c.runAddCommand(fs, args); err != nil {
29+
t.Errorf("c.runAddCommand(fs, nil, %+v) == %v, want <nil>", args, err)
30+
}
31+
vfst.RunTests(t, fs, "",
32+
vfst.TestPath("/home/user/.chezmoi/dot_bashrc",
33+
vfst.TestModeIsRegular,
34+
vfst.TestContentsString("# contents of .bashrc\n"),
35+
),
36+
)
37+
if err := fs.WriteFile("/home/user/.bashrc", []byte("# new contents of .bashrc\n"), 0644); err != nil {
38+
t.Errorf("fs.WriteFile(...) == %v, want <nil>", err)
39+
}
40+
if err := c.runAddCommand(fs, args); err != nil {
41+
t.Errorf("c.runAddCommand(fs, nil, %+v) == %v, want <nil>", args, err)
42+
}
43+
vfst.RunTests(t, fs, "",
44+
vfst.TestPath("/home/user/.chezmoi/dot_bashrc",
45+
vfst.TestModeIsRegular,
46+
vfst.TestContentsString("# new contents of .bashrc\n"),
47+
),
48+
)
49+
}
50+
1051
func TestAddCommand(t *testing.T) {
1152
for _, tc := range []struct {
1253
name string
@@ -234,44 +275,3 @@ func TestAddCommand(t *testing.T) {
234275
})
235276
}
236277
}
237-
238-
func TestAddAfterModification(t *testing.T) {
239-
c := &Config{
240-
SourceDir: "/home/user/.chezmoi",
241-
TargetDir: "/home/user",
242-
Umask: 022,
243-
DryRun: false,
244-
Verbose: true,
245-
}
246-
fs, cleanup, err := vfst.NewTestFS(map[string]interface{}{
247-
"/home/user": &vfst.Dir{Perm: 0755},
248-
"/home/user/.chezmoi": &vfst.Dir{Perm: 0700},
249-
"/home/user/.bashrc": "# contents of .bashrc\n",
250-
})
251-
defer cleanup()
252-
if err != nil {
253-
t.Fatalf("vfst.NewTestFS(_) == _, _, %v, want _, _, <nil>", err)
254-
}
255-
args := []string{"/home/user/.bashrc"}
256-
if err := c.runAddCommand(fs, args); err != nil {
257-
t.Errorf("c.runAddCommand(fs, nil, %+v) == %v, want <nil>", args, err)
258-
}
259-
vfst.RunTests(t, fs, "",
260-
vfst.TestPath("/home/user/.chezmoi/dot_bashrc",
261-
vfst.TestModeIsRegular,
262-
vfst.TestContentsString("# contents of .bashrc\n"),
263-
),
264-
)
265-
if err := fs.WriteFile("/home/user/.bashrc", []byte("# new contents of .bashrc\n"), 0644); err != nil {
266-
t.Errorf("fs.WriteFile(...) == %v, want <nil>", err)
267-
}
268-
if err := c.runAddCommand(fs, args); err != nil {
269-
t.Errorf("c.runAddCommand(fs, nil, %+v) == %v, want <nil>", args, err)
270-
}
271-
vfst.RunTests(t, fs, "",
272-
vfst.TestPath("/home/user/.chezmoi/dot_bashrc",
273-
vfst.TestModeIsRegular,
274-
vfst.TestContentsString("# new contents of .bashrc\n"),
275-
),
276-
)
277-
}

cmd/octal_int_value.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ import "strconv"
66
// pflag.Value interface for use as a command line flag.
77
type octalIntValue int
88

9-
func (o *octalIntValue) String() string {
10-
return "0" + strconv.FormatInt(int64(*o), 8)
11-
}
12-
139
func (o *octalIntValue) Set(s string) error {
1410
v, err := strconv.ParseInt(s, 0, 64)
1511
*o = octalIntValue(v)
1612
return err
1713
}
1814

15+
func (o *octalIntValue) String() string {
16+
return "0" + strconv.FormatInt(int64(*o), 8)
17+
}
18+
1919
func (o *octalIntValue) Type() string {
2020
return "int"
2121
}

lib/chezmoi/autotemplate.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,6 @@ func (b byValueLength) Less(i, j int) bool {
2626
}
2727
func (b byValueLength) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
2828

29-
func extractVariables(variables []templateVariable, parent []string, data map[string]interface{}) []templateVariable {
30-
for name, value := range data {
31-
switch value := value.(type) {
32-
case string:
33-
variables = append(variables, templateVariable{
34-
name: strings.Join(append(parent, name), "."),
35-
value: value,
36-
})
37-
case map[string]interface{}:
38-
variables = extractVariables(variables, append(parent, name), value)
39-
}
40-
}
41-
return variables
42-
}
43-
4429
func autoTemplate(contents []byte, data map[string]interface{}) ([]byte, error) {
4530
// FIXME this naive approach will generate incorrect templates if the
4631
// variable names match variable values
@@ -79,12 +64,27 @@ func autoTemplate(contents []byte, data map[string]interface{}) ([]byte, error)
7964
return []byte(contentsStr), nil
8065
}
8166

82-
// isWord returns true if b is a word byte.
83-
func isWord(b byte) bool {
84-
return '0' <= b && b <= '9' || 'A' <= b && b <= 'Z' || 'a' <= b && b <= 'z'
67+
func extractVariables(variables []templateVariable, parent []string, data map[string]interface{}) []templateVariable {
68+
for name, value := range data {
69+
switch value := value.(type) {
70+
case string:
71+
variables = append(variables, templateVariable{
72+
name: strings.Join(append(parent, name), "."),
73+
value: value,
74+
})
75+
case map[string]interface{}:
76+
variables = extractVariables(variables, append(parent, name), value)
77+
}
78+
}
79+
return variables
8580
}
8681

8782
// inWord returns true if splitting s at position i would split a word.
8883
func inWord(s string, i int) bool {
8984
return i > 0 && i < len(s) && isWord(s[i-1]) && isWord(s[i])
9085
}
86+
87+
// isWord returns true if b is a word byte.
88+
func isWord(b byte) bool {
89+
return '0' <= b && b <= '9' || 'A' <= b && b <= 'Z' || 'a' <= b && b <= 'z'
90+
}

lib/chezmoi/chezmoi.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ func ReturnTemplateFuncError(err error) {
4848
})
4949
}
5050

51+
// dirNames returns the dir names from dirAttributes.
52+
func dirNames(dirAttributes []DirAttributes) []string {
53+
dns := []string{}
54+
for _, da := range dirAttributes {
55+
dns = append(dns, da.Name)
56+
}
57+
return dns
58+
}
59+
5160
// parseDirNameComponents parses multiple directory name components.
5261
func parseDirNameComponents(components []string) []DirAttributes {
5362
das := []DirAttributes{}
@@ -58,15 +67,6 @@ func parseDirNameComponents(components []string) []DirAttributes {
5867
return das
5968
}
6069

61-
// dirNames returns the dir names from dirAttributes.
62-
func dirNames(dirAttributes []DirAttributes) []string {
63-
dns := []string{}
64-
for _, da := range dirAttributes {
65-
dns = append(dns, da.Name)
66-
}
67-
return dns
68-
}
69-
7070
// parseSourceFilePath parses a single source file path.
7171
func parseSourceFilePath(path string) parsedSourceFilePath {
7272
components := splitPathList(path)

lib/chezmoi/file.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,6 @@ func (f *File) ConcreteValue(targetDir string, ignore func(string) bool, sourceD
177177
}, nil
178178
}
179179

180-
// Evaluate evaluates f's contents.
181-
func (f *File) Evaluate(ignore func(string) bool) error {
182-
if ignore(f.targetName) {
183-
return nil
184-
}
185-
_, err := f.Contents()
186-
return err
187-
}
188-
189180
// Contents returns f's contents.
190181
func (f *File) Contents() ([]byte, error) {
191182
if f.evaluateContents != nil {
@@ -195,6 +186,15 @@ func (f *File) Contents() ([]byte, error) {
195186
return f.contents, f.contentsErr
196187
}
197188

189+
// Evaluate evaluates f's contents.
190+
func (f *File) Evaluate(ignore func(string) bool) error {
191+
if ignore(f.targetName) {
192+
return nil
193+
}
194+
_, err := f.Contents()
195+
return err
196+
}
197+
198198
// Executable returns true is f is executable.
199199
func (f *File) Executable() bool {
200200
return f.Perm&0111 != 0

lib/chezmoi/symlink.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,6 @@ func (s *Symlink) Evaluate(ignore func(string) bool) error {
8181
return err
8282
}
8383

84-
// SourceName implements Entry.SourceName.
85-
func (s *Symlink) SourceName() string {
86-
return s.sourceName
87-
}
88-
8984
// Linkname returns s's link name.
9085
func (s *Symlink) Linkname() (string, error) {
9186
if s.evaluateLinkname != nil {
@@ -95,6 +90,11 @@ func (s *Symlink) Linkname() (string, error) {
9590
return s.linkname, s.linknameErr
9691
}
9792

93+
// SourceName implements Entry.SourceName.
94+
func (s *Symlink) SourceName() string {
95+
return s.sourceName
96+
}
97+
9898
// TargetName implements Entry.TargetName.
9999
func (s *Symlink) TargetName() string {
100100
return s.targetName

lib/chezmoi/target_state.go

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ func (ts *TargetState) Add(fs vfs.FS, addOptions AddOptions, targetPath string,
132132
}
133133
}
134134

135+
// Apply ensures that ts.TargetDir in fs matches ts.
136+
func (ts *TargetState) Apply(fs vfs.FS, mutator Mutator) error {
137+
for _, entryName := range sortedEntryNames(ts.Entries) {
138+
if err := ts.Entries[entryName].Apply(fs, ts.TargetDir, ts.TargetIgnore.Match, ts.Umask, mutator); err != nil {
139+
return err
140+
}
141+
}
142+
return nil
143+
}
144+
135145
// Archive writes ts to w.
136146
func (ts *TargetState) Archive(w *tar.Writer, umask os.FileMode) error {
137147
currentUser, err := user.Current()
@@ -168,16 +178,6 @@ func (ts *TargetState) Archive(w *tar.Writer, umask os.FileMode) error {
168178
return nil
169179
}
170180

171-
// Apply ensures that ts.TargetDir in fs matches ts.
172-
func (ts *TargetState) Apply(fs vfs.FS, mutator Mutator) error {
173-
for _, entryName := range sortedEntryNames(ts.Entries) {
174-
if err := ts.Entries[entryName].Apply(fs, ts.TargetDir, ts.TargetIgnore.Match, ts.Umask, mutator); err != nil {
175-
return err
176-
}
177-
}
178-
return nil
179-
}
180-
181181
// ConcreteValue returns a value suitable for serialization.
182182
func (ts *TargetState) ConcreteValue(recursive bool) (interface{}, error) {
183183
var entryConcreteValues []interface{}
@@ -414,6 +414,33 @@ func (ts *TargetState) addFile(targetName string, entries map[string]Entry, pare
414414
return mutator.WriteFile(filepath.Join(ts.SourceDir, sourceName), contents, 0666&^ts.Umask, existingContents)
415415
}
416416

417+
func (ts *TargetState) addSourceIgnore(fs vfs.FS, path, relPath string) error {
418+
data, err := ts.executeTemplate(fs, path)
419+
if err != nil {
420+
return err
421+
}
422+
dir := filepath.Dir(relPath)
423+
s := bufio.NewScanner(bytes.NewReader(data))
424+
for s.Scan() {
425+
text := s.Text()
426+
if index := strings.IndexRune(text, '#'); index != -1 {
427+
text = text[:index]
428+
}
429+
text = strings.TrimSpace(text)
430+
if text == "" {
431+
continue
432+
}
433+
pattern := filepath.Join(dir, text)
434+
if err := ts.TargetIgnore.Add(pattern); err != nil {
435+
return fmt.Errorf("%s: %v", path, err)
436+
}
437+
}
438+
if err := s.Err(); err != nil {
439+
return fmt.Errorf("%s: %v", path, err)
440+
}
441+
return nil
442+
}
443+
417444
func (ts *TargetState) addSymlink(targetName string, entries map[string]Entry, parentDirSourceName string, linkname string, mutator Mutator) error {
418445
name := filepath.Base(targetName)
419446
var existingSymlink *Symlink
@@ -555,30 +582,3 @@ func (ts *TargetState) importHeader(r io.Reader, importTAROptions ImportTAROptio
555582
return fmt.Errorf("%s: unspported typeflag '%c'", header.Name, header.Typeflag)
556583
}
557584
}
558-
559-
func (ts *TargetState) addSourceIgnore(fs vfs.FS, path, relPath string) error {
560-
data, err := ts.executeTemplate(fs, path)
561-
if err != nil {
562-
return err
563-
}
564-
dir := filepath.Dir(relPath)
565-
s := bufio.NewScanner(bytes.NewReader(data))
566-
for s.Scan() {
567-
text := s.Text()
568-
if index := strings.IndexRune(text, '#'); index != -1 {
569-
text = text[:index]
570-
}
571-
text = strings.TrimSpace(text)
572-
if text == "" {
573-
continue
574-
}
575-
pattern := filepath.Join(dir, text)
576-
if err := ts.TargetIgnore.Add(pattern); err != nil {
577-
return fmt.Errorf("%s: %v", path, err)
578-
}
579-
}
580-
if err := s.Err(); err != nil {
581-
return fmt.Errorf("%s: %v", path, err)
582-
}
583-
return nil
584-
}

0 commit comments

Comments
 (0)