From f06cf10bbe83e305b40da3e0ec249cc34c0cc977 Mon Sep 17 00:00:00 2001 From: esimov Date: Thu, 29 Apr 2021 15:36:08 +0300 Subject: [PATCH] perf: create output dir in case it's missing --- cmd/triangle/main.go | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/cmd/triangle/main.go b/cmd/triangle/main.go index 0e84056..ca25d3a 100644 --- a/cmd/triangle/main.go +++ b/cmd/triangle/main.go @@ -28,8 +28,6 @@ const helperBanner = ` // The default http address used for accessing the generated SVG file in case of -web flag is used. const httpAddress = "http://localhost:8080" -type MessageType int - type result struct { path string triangles []triangle.Triangle @@ -37,6 +35,8 @@ type result struct { err error } +type MessageType int + // The message types used accross the CLI application. const ( DefaultMessage MessageType = iota @@ -113,19 +113,15 @@ func main() { var wg sync.WaitGroup // Read destination file or directory. - dst, err := os.Stat(*destination) + _, err := os.Stat(*destination) if err != nil { - log.Fatalf( - decorateText("Unable to get dir stats: %v", ErrorMessage), - decorateText(err.Error(), DefaultMessage), - ) - } - - //@TODO create destination directory in case it does not exists. - - // Check if the image destination is a directory or a file. - if dst.Mode().IsRegular() { - log.Fatalf(decorateText("Please specify a directory as destination!.", ErrorMessage)) + err = os.Mkdir(*destination, 0755) + if err != nil { + log.Fatalf( + decorateText("Unable to get dir stats: %v\n", ErrorMessage), + decorateText(err.Error(), DefaultMessage), + ) + } } // Process image files from directory concurrently. @@ -148,6 +144,7 @@ func main() { wg.Wait() }() + // Consume the channel values for res := range ch { showProcessStatus(res.path, res.triangles, res.points, res.err) } @@ -190,7 +187,7 @@ func main() { procTime := time.Since(start) s.Stop() - fmt.Printf("Generated in: %s\n", decorateText(fmt.Sprintf("%.2fs", procTime.Seconds()), SuccessMessage)) + fmt.Printf("Finished in: %s\n", decorateText(fmt.Sprintf("%.2fs", procTime.Seconds()), SuccessMessage)) } // walkDir starts a goroutine to walk the specified directory tree