Skip to content

Commit

Permalink
fix file walking
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricedesaxe committed Jul 18, 2024
1 parent bb3e0fc commit fd2b81c
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,41 @@ func templCSSSort(flags Flags) {
// find all .templ files in directory and subdirectories
var files []string
var err error
if flags.file == "" {
files, err = filepath.Glob("./templates/*.templ")
if flags.file != "" {
// If the file flag is specified, only take in that file
if !strings.HasSuffix(flags.file, ".templ") {
log.Fatal("File must have .templ extension")
}
files = append(files, flags.file)
} else if flags.dir != "" {
files, err = filepath.Glob(flags.dir + "/*.templ")
// If the dir flag is specified, take in that dir and its subdirectories
err = filepath.Walk(flags.dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && strings.HasSuffix(info.Name(), ".templ") {
files = append(files, path)
}
return nil
})
} else {
files = []string{flags.file}
// If neither flag is specified, go through cwd and all subdirectories for any .templ file
err = filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && strings.HasSuffix(info.Name(), ".templ") {
files = append(files, path)
}
return nil
})
}
if err != nil {
log.Fatal(err)
}

log.Println("Found", len(files), "files")

// parse each file
for _, file := range files {
// read file
Expand Down

0 comments on commit fd2b81c

Please sign in to comment.