-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds
mllint render
to pretty-print Markdown reports to terminal
- Loading branch information
Showing
3 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"path" | ||
|
||
"github.com/bvobart/mllint/utils" | ||
"github.com/bvobart/mllint/utils/markdown" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewRenderCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "render FILE", | ||
Short: "Render an " + formatInlineCode("mllint") + " report to your terminal.", | ||
Long: fmt.Sprintf(`Renders an %s report to your terminal in a pretty way.`, formatInlineCode("mllint")), | ||
RunE: render, | ||
Args: cobra.ExactArgs(1), | ||
SilenceErrors: true, | ||
SilenceUsage: true, | ||
} | ||
return cmd | ||
} | ||
|
||
func render(cmd *cobra.Command, args []string) error { | ||
filename := args[0] | ||
if !utils.FileExists(filename) { | ||
return fmt.Errorf("cannot find file: %s", filename) | ||
} | ||
|
||
if path.Ext(filename) != ".md" { | ||
return fmt.Errorf("mllint can only render Markdown files, but the provided filename does not end with '.md': %s", filename) | ||
} | ||
|
||
contents, err := ioutil.ReadFile(filename) | ||
if err != nil { | ||
return fmt.Errorf("cannot read from the given file: %w", err) | ||
} | ||
|
||
fmt.Println(markdown.Render(string(contents))) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters