Skip to content

Commit

Permalink
✨ Format multiple files asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
UltiRequiem committed Sep 9, 2021
1 parent 3bd5137 commit 14ef6de
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
13 changes: 11 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ func Init() {
overwrite, indent, logFiles, multipleArgs, files := getParams()

if multipleArgs {
channel := make(chan string)

for _, file := range files {
formatFile(file, *indent, *overwrite)
go formatFile(file, *indent, *overwrite, channel)
}

for i := 0; i < len(files); i++ {
logFile := <-channel

if logFiles {
log.Printf("%s formatted!\n", file)
log.Print(logFile)
}

}

} else {
formatStream(os.Stdin, os.Stdout, *indent)
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func getParams() (*bool, *int, bool, bool, []string) {

}

func formatFile(file string, indent int, overwrite bool) {
func formatFile(file string, indent int, overwrite bool, channel chan string) {
r, err := os.Open(file)

defer r.Close()
Expand All @@ -40,6 +40,8 @@ func formatFile(file string, indent int, overwrite bool) {
if e := dumpStream(&out, file, overwrite); e != nil {
log.Fatalf("Cannot overwrite %s: '%s'", file, e)
}

channel <- fmt.Sprintf("%s formatted!", file)
}

func formatStream(r io.Reader, out io.Writer, indent int) error {
Expand Down

0 comments on commit 14ef6de

Please sign in to comment.