From 55e74b62ee58dd052524755595084fea437c61ec Mon Sep 17 00:00:00 2001 From: Bart van Oort Date: Thu, 27 May 2021 15:43:53 +0200 Subject: [PATCH] commands: when specifying `-o` with a path where its folder doesn't exist yet, create it --- commands/describe.go | 10 +--------- commands/list.go | 10 +--------- commands/run.go | 8 +------- commands/util.go | 19 +++++++++++++++++++ 4 files changed, 22 insertions(+), 25 deletions(-) diff --git a/commands/describe.go b/commands/describe.go index b94cafe..778c220 100644 --- a/commands/describe.go +++ b/commands/describe.go @@ -2,7 +2,6 @@ package commands import ( "fmt" - "io/ioutil" "strings" "github.com/fatih/color" @@ -11,7 +10,6 @@ import ( "github.com/bvobart/mllint/api" "github.com/bvobart/mllint/categories" "github.com/bvobart/mllint/linters" - "github.com/bvobart/mllint/utils" "github.com/bvobart/mllint/utils/markdown" "github.com/bvobart/mllint/utils/markdowngen" ) @@ -64,13 +62,7 @@ func describe(cmd *cobra.Command, args []string) error { } if outputToFile() { - if err := ioutil.WriteFile(outputFile, []byte(output.String()), 0644); err != nil { - return fmt.Errorf("failed to write output file: %w", err) - } - bold := color.New(color.Bold) - shush(func() { bold.Println("Your report is complete, see", formatInlineCode(utils.AbsolutePath(outputFile))) }) - shush(func() { bold.Println() }) - return nil + return writeToOutputFile(output.String()) } if outputToStdout() { diff --git a/commands/list.go b/commands/list.go index 1019583..3e886c6 100644 --- a/commands/list.go +++ b/commands/list.go @@ -2,14 +2,12 @@ package commands import ( "fmt" - "io/ioutil" "github.com/fatih/color" "github.com/spf13/cobra" "github.com/bvobart/mllint/api" "github.com/bvobart/mllint/linters" - "github.com/bvobart/mllint/utils" "github.com/bvobart/mllint/utils/markdowngen" ) @@ -100,13 +98,7 @@ func listLinters(linters map[api.Category]api.Linter) error { md := markdowngen.LintersOverview(linters) if outputToFile() { - if err := ioutil.WriteFile(outputFile, []byte(md), 0644); err != nil { - return fmt.Errorf("failed to write output file: %w", err) - } - bold := color.New(color.Bold) - shush(func() { bold.Println("Your report is complete, see", formatInlineCode(utils.AbsolutePath(outputFile))) }) - shush(func() { bold.Println() }) - return nil + return writeToOutputFile(md) } if outputToStdout() { diff --git a/commands/run.go b/commands/run.go index 85ed95f..65db0bd 100644 --- a/commands/run.go +++ b/commands/run.go @@ -3,7 +3,6 @@ package commands import ( "errors" "fmt" - "io/ioutil" "github.com/fatih/color" "github.com/hashicorp/go-multierror" @@ -115,12 +114,7 @@ func (rc *runCommand) RunLint(cmd *cobra.Command, args []string) error { } if outputToFile() { - if err := ioutil.WriteFile(outputFile, []byte(output), 0644); err != nil { - return fmt.Errorf("failed to write output file: %w", err) - } - bold := color.New(color.Bold) - shush(func() { bold.Println("Your report is complete, see", formatInlineCode(utils.AbsolutePath(outputFile))) }) - shush(func() { bold.Println() }) + return writeToOutputFile(output) } else { fmt.Println(markdown.Render(output)) } diff --git a/commands/util.go b/commands/util.go index 6d28948..fe29fe2 100644 --- a/commands/util.go +++ b/commands/util.go @@ -2,6 +2,7 @@ package commands import ( "fmt" + "io/ioutil" "os" "path" "strings" @@ -36,6 +37,24 @@ func formatInlineCode(text string) string { return color.New(color.Reset, color.Italic, color.FgYellow).Sprint(text) } +func writeToOutputFile(output string) error { + // create output folder if it doesn't exist + if !utils.FolderExists(path.Dir(outputFile)) { + if err := os.MkdirAll(path.Dir(outputFile), 0755); err != nil { + return fmt.Errorf("failed to create output directory: %w", err) + } + } + + if err := ioutil.WriteFile(outputFile, []byte(output), 0644); err != nil { + return fmt.Errorf("failed to write output file: %w", err) + } + + bold := color.New(color.Bold) + shush(func() { bold.Println("Your report is complete, see", formatInlineCode(utils.AbsolutePath(outputFile))) }) + shush(func() { bold.Println() }) + return nil +} + func prettyPrintLinters(linters map[api.Category]api.Linter) { if len(linters) == 0 { color.Red("Oh no! Your mllint configuration has disabled ALL rules!")