Skip to content

Commit fd2b81c

Browse files
committed
fix file walking
1 parent bb3e0fc commit fd2b81c

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

main.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,41 @@ func templCSSSort(flags Flags) {
4242
// find all .templ files in directory and subdirectories
4343
var files []string
4444
var err error
45-
if flags.file == "" {
46-
files, err = filepath.Glob("./templates/*.templ")
45+
if flags.file != "" {
46+
// If the file flag is specified, only take in that file
47+
if !strings.HasSuffix(flags.file, ".templ") {
48+
log.Fatal("File must have .templ extension")
49+
}
50+
files = append(files, flags.file)
4751
} else if flags.dir != "" {
48-
files, err = filepath.Glob(flags.dir + "/*.templ")
52+
// If the dir flag is specified, take in that dir and its subdirectories
53+
err = filepath.Walk(flags.dir, func(path string, info os.FileInfo, err error) error {
54+
if err != nil {
55+
return err
56+
}
57+
if !info.IsDir() && strings.HasSuffix(info.Name(), ".templ") {
58+
files = append(files, path)
59+
}
60+
return nil
61+
})
4962
} else {
50-
files = []string{flags.file}
63+
// If neither flag is specified, go through cwd and all subdirectories for any .templ file
64+
err = filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
65+
if err != nil {
66+
return err
67+
}
68+
if !info.IsDir() && strings.HasSuffix(info.Name(), ".templ") {
69+
files = append(files, path)
70+
}
71+
return nil
72+
})
5173
}
5274
if err != nil {
5375
log.Fatal(err)
5476
}
5577

78+
log.Println("Found", len(files), "files")
79+
5680
// parse each file
5781
for _, file := range files {
5882
// read file

0 commit comments

Comments
 (0)