Skip to content

Commit

Permalink
commands: when specifying -o with a path where its folder doesn't e…
Browse files Browse the repository at this point in the history
…xist yet, create it
  • Loading branch information
bvobart committed May 27, 2021
1 parent 4269be9 commit 55e74b6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 25 deletions.
10 changes: 1 addition & 9 deletions commands/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package commands

import (
"fmt"
"io/ioutil"
"strings"

"github.com/fatih/color"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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() {
Expand Down
10 changes: 1 addition & 9 deletions commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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() {
Expand Down
8 changes: 1 addition & 7 deletions commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package commands
import (
"errors"
"fmt"
"io/ioutil"

"github.com/fatih/color"
"github.com/hashicorp/go-multierror"
Expand Down Expand Up @@ -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))
}
Expand Down
19 changes: 19 additions & 0 deletions commands/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"fmt"
"io/ioutil"
"os"
"path"
"strings"
Expand Down Expand Up @@ -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!")
Expand Down

0 comments on commit 55e74b6

Please sign in to comment.