Skip to content

Commit

Permalink
Async write to files
Browse files Browse the repository at this point in the history
  • Loading branch information
juanvillacortac committed Jun 14, 2022
1 parent acb0588 commit 7118cca
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
34 changes: 25 additions & 9 deletions cmd/ditto.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"path"
"path/filepath"

"golang.org/x/sync/errgroup"

"github.com/juanvillacortac/ditto/pkg/generators"
"github.com/juanvillacortac/ditto/pkg/program"
)

Expand All @@ -22,6 +25,17 @@ var (
verbose = flag.Bool("V", false, "Verbose output")
)

func writeFile(file generators.OutputFile) error {
path := filepath.Dir(file.Filename)
if err := os.MkdirAll(path, os.ModePerm); err != nil {
return err
}
if err := os.WriteFile(file.Filename, []byte(file.Body), os.ModePerm); err != nil {
return err
}
return nil
}

func run() int {
flag.Parse()

Expand Down Expand Up @@ -82,16 +96,18 @@ func run() int {
}
}

errg := new(errgroup.Group)
errg.SetLimit(len(files))

for _, f := range files {
path := filepath.Dir(f.Filename)
if err := os.MkdirAll(path, os.ModePerm); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return 1
}
if err := os.WriteFile(f.Filename, []byte(f.Body), os.ModePerm); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return 1
}
f := f
errg.Go(func() error {
return writeFile(f)
})
}

if err := errg.Wait(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
fmt.Println("Done!")
return 0
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ require (
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 // indirect
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ github.com/yoheimuta/go-protoparser/v4 v4.5.4 h1:0/uEpkzBeSmC591cHIm+M7WIfHaP5FO
github.com/yoheimuta/go-protoparser/v4 v4.5.4/go.mod h1:AHNNnSWnb0UoL4QgHPiOAg2BniQceFscPI5X/BZNHl8=
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 h1:S25/rfnfsMVgORT4/J61MJ7rdyseOZOyvLIrZEZ7s6s=
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f h1:Ax0t5p6N38Ga0dThY21weqDEyz2oklo4IvDkpigvkD8=
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down

0 comments on commit 7118cca

Please sign in to comment.