Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve error experience, linting, and more #174

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions .golangci.yml
@@ -0,0 +1,19 @@
run:
tests: False
timeout: 3m

severity:
default-severity: error
rules:
- linters:
- errcheck
- gocritic
severity: warning

linters:
enable:
- asciicheck
- exportloopref
- gci
- gofmt
- misspell
3 changes: 1 addition & 2 deletions cmd/tfplugindocs/main.go
Expand Up @@ -3,9 +3,8 @@ package main
import (
"os"

"github.com/mattn/go-colorable"

"github.com/hashicorp/terraform-plugin-docs/internal/cmd"
"github.com/mattn/go-colorable"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/generate.go
Expand Up @@ -42,7 +42,7 @@ func (cmd *generateCmd) Help() string {
}
})

strBuilder.WriteString(fmt.Sprintf("\nUsage: tfplugindocs generate [<args>]\n\n"))
strBuilder.WriteString("\nUsage: tfplugindocs generate [<args>]\n\n")
cmd.Flags().VisitAll(func(f *flag.Flag) {
if f.DefValue != "" {
strBuilder.WriteString(fmt.Sprintf(" --%s <ARG> %s%s%s (default: %q)\n",
Expand Down
7 changes: 3 additions & 4 deletions internal/cmd/run.go
Expand Up @@ -23,7 +23,6 @@ func (cmd *commonCmd) run(r func() error) int {
}

func initCommands(ui cli.Ui) map[string]cli.CommandFactory {

generateFactory := func() (cli.Command, error) {
return &generateCmd{
commonCmd: commonCmd{
Expand Down Expand Up @@ -55,7 +54,7 @@ func initCommands(ui cli.Ui) map[string]cli.CommandFactory {
"": defaultFactory,
"generate": generateFactory,
"validate": validateFactory,
//"serve": serveFactory,
// "serve": serveFactory,
}
}

Expand All @@ -82,7 +81,7 @@ func Run(name, version string, args []string, stdin io.Reader, stdout, stderr io

commands := initCommands(ui)

cli := cli.CLI{
cmd := cli.CLI{
Name: name,
Args: args,
Commands: commands,
Expand All @@ -91,7 +90,7 @@ func Run(name, version string, args []string, stdin io.Reader, stdout, stderr io
Version: version,
}

exitCode, err := cli.Run()
exitCode, err := cmd.Run()
if err != nil {
return 1
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/validate.go
Expand Up @@ -30,7 +30,7 @@ func (cmd *validateCmd) Help() string {
}
})

strBuilder.WriteString(fmt.Sprintf("\nUsage: tfplugindocs validate [<args>]\n\n"))
strBuilder.WriteString("\nUsage: tfplugindocs validate [<args>]\n\n")
cmd.Flags().VisitAll(func(f *flag.Flag) {
if f.DefValue != "" {
strBuilder.WriteString(fmt.Sprintf(" --%s <ARG> %s%s%s (default: %q)\n",
Expand Down
48 changes: 13 additions & 35 deletions internal/mdplain/renderer.go
Expand Up @@ -18,7 +18,7 @@ func (options *Text) GetFlags() int {

func (options *Text) TitleBlock(out *bytes.Buffer, text []byte) {
text = bytes.TrimPrefix(text, []byte("% "))
text = bytes.Replace(text, []byte("\n% "), []byte("\n"), -1)
text = bytes.ReplaceAll(text, []byte("\n% "), []byte("\n"))
out.Write(text)
out.WriteString("\n")
}
Expand All @@ -29,7 +29,6 @@ func (options *Text) Header(out *bytes.Buffer, text func() bool, level int, id s

if !text() {
out.Truncate(marker)
return
}
}

Expand Down Expand Up @@ -57,7 +56,7 @@ func (options *Text) BlockQuote(out *bytes.Buffer, text []byte) {
out.Write(text)
}

func (options *Text) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) {
func (options *Text) Table(out *bytes.Buffer, header, body []byte, columnData []int) {
doubleSpace(out)
out.Write(header)
out.Write(body)
Expand Down Expand Up @@ -93,7 +92,6 @@ func (options *Text) List(out *bytes.Buffer, text func() bool, flags int) {

if !text() {
out.Truncate(marker)
return
}
}

Expand All @@ -107,7 +105,6 @@ func (options *Text) Paragraph(out *bytes.Buffer, text func() bool) {

if !text() {
out.Truncate(marker)
return
}
}

Expand All @@ -130,26 +127,19 @@ func (options *Text) Emphasis(out *bytes.Buffer, text []byte) {
out.Write(text)
}

func (options *Text) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
return
}
func (options *Text) Image(out *bytes.Buffer, link, title, alt []byte) {}

func (options *Text) LineBreak(out *bytes.Buffer) {
return
}
func (options *Text) LineBreak(out *bytes.Buffer) {}

func (options *Text) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
func (options *Text) Link(out *bytes.Buffer, link, title, content []byte) {
out.Write(content)
if !isRelativeLink(link) {
out.WriteString(" ")
out.Write(link)
}
return
}

func (options *Text) RawHtmlTag(out *bytes.Buffer, text []byte) {
return
}
func (options *Text) RawHtmlTag(out *bytes.Buffer, text []byte) {}

func (options *Text) TripleEmphasis(out *bytes.Buffer, text []byte) {
out.Write(text)
Expand All @@ -159,9 +149,7 @@ func (options *Text) StrikeThrough(out *bytes.Buffer, text []byte) {
out.Write(text)
}

func (options *Text) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {
return
}
func (options *Text) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {}

func (options *Text) Entity(out *bytes.Buffer, entity []byte) {
out.Write(entity)
Expand All @@ -171,25 +159,15 @@ func (options *Text) NormalText(out *bytes.Buffer, text []byte) {
out.Write(text)
}

func (options *Text) Smartypants(out *bytes.Buffer, text []byte) {
return
}
func (options *Text) Smartypants(out *bytes.Buffer, text []byte) {}

func (options *Text) DocumentHeader(out *bytes.Buffer) {
return
}
func (options *Text) DocumentHeader(out *bytes.Buffer) {}

func (options *Text) DocumentFooter(out *bytes.Buffer) {
return
}
func (options *Text) DocumentFooter(out *bytes.Buffer) {}

func (options *Text) TocHeader(text []byte, level int) {
return
}
func (options *Text) TocHeader(text []byte, level int) {}

func (options *Text) TocFinalize() {
return
}
func (options *Text) TocFinalize() {}

func doubleSpace(out *bytes.Buffer) {
if out.Len() > 0 {
Expand All @@ -214,5 +192,5 @@ func isRelativeLink(link []byte) (yes bool) {
if len(link) == 1 && link[0] == '/' {
yes = true
}
return
return yes
}