Skip to content

Commit

Permalink
feat(cmd): automatically create parent folders for output file (#36)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Masson <mmasson@stingray.com>
  • Loading branch information
michaelmass and Michael Masson committed May 20, 2021
1 parent c7a829b commit cbfa3fe
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion cmd/gomarkdoc/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ func writeOutput(specs []*PackageSpec, opts commandOptions) error {
return err
}
default:
if err := ioutil.WriteFile(fileName, []byte(text), 0755); err != nil {
if err := writeFile(fileName, text); err != nil {
return fmt.Errorf("failed to write output file %s: %w", fileName, err)
}
}
Expand All @@ -450,6 +450,22 @@ func writeOutput(specs []*PackageSpec, opts commandOptions) error {
return nil
}

func writeFile(fileName string, text string) error {
folder := filepath.Dir(fileName)

if folder != "" {
if err := os.MkdirAll(folder, 0755); err != nil {
return fmt.Errorf("failed to create folder %s: %w", folder, err)
}
}

if err := ioutil.WriteFile(fileName, []byte(text), 0755); err != nil {
return fmt.Errorf("failed to write file %s: %w", fileName, err)
}

return nil
}

func checkFile(b *bytes.Buffer, path string) error {
checkErr := errors.New("output does not match current files. Did you forget to run gomarkdoc?")

Expand Down

0 comments on commit cbfa3fe

Please sign in to comment.