Skip to content

Commit

Permalink
Merge pull request #39 from hashicorp/fix-markdown-rendering
Browse files Browse the repository at this point in the history
Fix Markdown rendering
  • Loading branch information
radeksimko authored Feb 10, 2020
2 parents c6ae626 + 9f5681a commit dd27d0d
Show file tree
Hide file tree
Showing 18 changed files with 408 additions and 96 deletions.
97 changes: 1 addition & 96 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"encoding/json"
"fmt"
"os"
"strings"
"text/template"

"github.com/hashicorp/terraform-config-inspect/tfconfig"
flag "github.com/spf13/pflag"
Expand Down Expand Up @@ -47,102 +45,9 @@ func showModuleJSON(module *tfconfig.Module) {
}

func showModuleMarkdown(module *tfconfig.Module) {
tmpl := template.New("md")
tmpl.Funcs(template.FuncMap{
"tt": func(s string) string {
return "`" + s + "`"
},
"commas": func(s []string) string {
return strings.Join(s, ", ")
},
"json": func(v interface{}) (string, error) {
j, err := json.Marshal(v)
return string(j), err
},
"severity": func(s tfconfig.DiagSeverity) string {
switch s {
case tfconfig.DiagError:
return "Error: "
case tfconfig.DiagWarning:
return "Warning: "
default:
return ""
}
},
})
template.Must(tmpl.Parse(markdownTemplate))
err := tmpl.Execute(os.Stdout, module)
err := tfconfig.RenderMarkdown(os.Stdout, module)
if err != nil {
fmt.Fprintf(os.Stderr, "error rendering template: %s\n", err)
os.Exit(2)
}
}

const markdownTemplate = `
# Module {{ tt .Path }}
{{- if .RequiredCore}}
Core Version Constraints:
{{- range .RequiredCore }}
* {{ tt . }}
{{- end}}{{end}}
{{- if .RequiredProviders}}
Provider Requirements:
{{- range $name, $versions := .RequiredProviders }}
* **{{ $name }}:** {{ if $versions }}{{ commas $versions | tt }}{{ else }}(any version){{ end }}
{{- end}}{{end}}
{{- if .Variables}}
## Input Variables
{{- range .Variables }}
* {{ tt .Name }}{{ if .Default }} (default {{ json .Default | tt }}){{else}} (required){{end}}
{{- if .Description}}: {{ .Description }}{{ end }}
{{- end}}{{end}}
{{- if .Outputs}}
## Output Values
{{- range .Outputs }}
* {{ tt .Name }}{{ if .Description}}: {{ .Description }}{{ end }}
{{- end}}{{end}}
{{- if .ManagedResources}}
## Managed Resources
{{- range .ManagedResources }}
* {{ printf "%s.%s" .Type .Name | tt }} from {{ tt .Provider.Name }}
{{- end}}{{end}}
{{- if .DataResources}}
## Data Resources
{{- range .DataResources }}
* {{ printf "data.%s.%s" .Type .Name | tt }} from {{ tt .Provider.Name }}
{{- end}}{{end}}
{{- if .ModuleCalls}}
## Child Modules
{{- range .ModuleCalls }}
* {{ tt .Name }} from {{ tt .Source }}{{ if .Version }} ({{ tt .Version }}){{ end }}
{{- end}}{{end}}
{{- if .Diagnostics}}
## Problems
{{- range .Diagnostics }}
## {{ severity .Severity }}{{ .Summary }}{{ if .Pos }}
(at {{ tt .Pos.Filename }} line {{ .Pos.Line }}{{ end }})
{{ if .Detail }}
{{ .Detail }}
{{- end }}
{{- end}}{{end}}
`
105 changes: 105 additions & 0 deletions tfconfig/markdown.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package tfconfig

import (
"encoding/json"
"io"
"strings"
"text/template"
)

func RenderMarkdown(w io.Writer, module *Module) error {
tmpl := template.New("md")
tmpl.Funcs(template.FuncMap{
"tt": func(s string) string {
return "`" + s + "`"
},
"commas": func(s []string) string {
return strings.Join(s, ", ")
},
"json": func(v interface{}) (string, error) {
j, err := json.Marshal(v)
return string(j), err
},
"severity": func(s DiagSeverity) string {
switch s {
case DiagError:
return "Error: "
case DiagWarning:
return "Warning: "
default:
return ""
}
},
})
template.Must(tmpl.Parse(markdownTemplate))
return tmpl.Execute(w, module)
}

const markdownTemplate = `
# Module {{ tt .Path }}
{{- if .RequiredCore}}
Core Version Constraints:
{{- range .RequiredCore }}
* {{ tt . }}
{{- end}}{{end}}
{{- if .RequiredProviders}}
Provider Requirements:
{{- range $name, $req := .RequiredProviders }}
* **{{ $name }}{{ if $req.Source }} ({{ $req.Source | tt }}){{ end }}:** {{ if $req.VersionConstraints }}{{ commas $req.VersionConstraints | tt }}{{ else }}(any version){{ end }}
{{- end}}{{end}}
{{- if .Variables}}
## Input Variables
{{- range .Variables }}
* {{ tt .Name }}{{ if .Default }} (default {{ json .Default | tt }}){{else}} (required){{end}}
{{- if .Description}}: {{ .Description }}{{ end }}
{{- end}}{{end}}
{{- if .Outputs}}
## Output Values
{{- range .Outputs }}
* {{ tt .Name }}{{ if .Description}}: {{ .Description }}{{ end }}
{{- end}}{{end}}
{{- if .ManagedResources}}
## Managed Resources
{{- range .ManagedResources }}
* {{ printf "%s.%s" .Type .Name | tt }} from {{ tt .Provider.Name }}
{{- end}}{{end}}
{{- if .DataResources}}
## Data Resources
{{- range .DataResources }}
* {{ printf "data.%s.%s" .Type .Name | tt }} from {{ tt .Provider.Name }}
{{- end}}{{end}}
{{- if .ModuleCalls}}
## Child Modules
{{- range .ModuleCalls }}
* {{ tt .Name }} from {{ tt .Source }}{{ if .Version }} ({{ tt .Version }}){{ end }}
{{- end}}{{end}}
{{- if .Diagnostics}}
## Problems
{{- range .Diagnostics }}
## {{ severity .Severity }}{{ .Summary }}{{ if .Pos }}
(at {{ tt .Pos.Filename }} line {{ .Pos.Line }}{{ end }})
{{ if .Detail }}
{{ .Detail }}
{{- end }}
{{- end}}{{end}}
`
53 changes: 53 additions & 0 deletions tfconfig/markdown_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package tfconfig

import (
"bytes"
"io/ioutil"
"path/filepath"
"testing"

"github.com/go-test/deep"
)

func TestRenderMarkdown(t *testing.T) {
fixturesDir := "test-fixtures"
testDirs, err := ioutil.ReadDir(fixturesDir)
if err != nil {
t.Fatal(err)
}

for _, info := range testDirs {
if !info.IsDir() {
continue
}

t.Run(info.Name(), func(t *testing.T) {
name := info.Name()
path := filepath.Join(fixturesDir, name)

fullPath := filepath.Join(path, name+".out.md")
expected, err := ioutil.ReadFile(fullPath)
if err != nil {
t.Skipf("%q not found, skipping test", fullPath)
}

module, _ := LoadModule(path)
if module == nil {
t.Fatalf("result object is nil; want a real object")
}

var b bytes.Buffer
buf := &b
err = RenderMarkdown(buf, module)
if err != nil {
t.Fatal(err)
}

if diff := deep.Equal(buf.String(), string(expected)); diff != nil {
for _, problem := range diff {
t.Errorf("%s", problem)
}
}
})
}
}
18 changes: 18 additions & 0 deletions tfconfig/test-fixtures/basics/basics.out.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

# Module `test-fixtures/basics`

Provider Requirements:
* **null:** (any version)

## Input Variables
* `A` (default `"A default"`)
* `B` (required): The B variable

## Output Values
* `A`
* `B`: I am B

## Managed Resources
* `null_resource.A` from `null`
* `null_resource.B` from `null`

11 changes: 11 additions & 0 deletions tfconfig/test-fixtures/data-resources/data-resources.out.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

# Module `test-fixtures/data-resources`

Provider Requirements:
* **external:** (any version)
* **notexternal:** (any version)

## Data Resources
* `data.external.bar` from `notexternal`
* `data.external.foo` from `external`

3 changes: 3 additions & 0 deletions tfconfig/test-fixtures/empty/empty.out.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

# Module `test-fixtures/empty`

8 changes: 8 additions & 0 deletions tfconfig/test-fixtures/for-expression/for-expression.out.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

# Module `test-fixtures/for-expression`

## Input Variables
* `enabled` (default `true`)
* `log_categories` (default `["one","two","three"]`)
* `retention_days` (default `7`)

6 changes: 6 additions & 0 deletions tfconfig/test-fixtures/invalid-braces/invalid-braces.out.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

# Module `test-fixtures/invalid-braces`

## Input Variables
* `foo` (default `"123"`)

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

# Module `test-fixtures/legacy-block-labels`

Core Version Constraints:
* `>= 0.11.0`

Provider Requirements:
* **aws:** `1.0.0`
* **external:** (any version)
* **notnull:** (any version)
* **noversion:** (any version)

## Input Variables
* `foo` (default `"foo default"`): foo description

## Output Values
* `foo`: foo description

## Managed Resources
* `null_resource.foo` from `notnull`

## Data Resources
* `data.external.foo` from `external`

## Child Modules
* `foo` from `foo/bar/baz` (`1.2.3`)

8 changes: 8 additions & 0 deletions tfconfig/test-fixtures/module-calls/module-calls.out.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

# Module `test-fixtures/module-calls`

## Child Modules
* `bar` from `./child`
* `baz` from `../elsewhere`
* `foo` from `foo/bar/baz` (`1.0.2`)

22 changes: 22 additions & 0 deletions tfconfig/test-fixtures/overrides/overrides.out.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

# Module `test-fixtures/overrides`

Provider Requirements:
* **null:** (any version)

## Input Variables
* `A` (required): The A variable OVERRIDDEN
* `B` (required): The B variable
* `C` (required): An entirely new variable C

## Output Values
* `A`: I am an overridden output!
* `B`: I am B

## Managed Resources
* `null_resource.A` from `null`
* `null_resource.B` from `null`

## Child Modules
* `foo` from `foo/bar/baz` (`1.0.2_override`)

11 changes: 11 additions & 0 deletions tfconfig/test-fixtures/provider-configs/provider-configs.out.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

# Module `test-fixtures/provider-configs`

Provider Requirements:
* **bar:** `1.0.0, 1.1.0`
* **baz:** `2.0.0`
* **foo:** (any version)

## Managed Resources
* `bar_bar.bar` from `bar`

Loading

0 comments on commit dd27d0d

Please sign in to comment.