Skip to content

Commit 6597df7

Browse files
feat: Make GenerateSourcePluginDocs struct method (#124)
* feat: Make GenerateSourcePluginDocs struct method Also, generate README.md with all tables in addition to file per table. * Update plugins/source_docs_test.go Co-authored-by: Erez Rokah <[email protected]> * Update plugins/source_docs_test.go Co-authored-by: Erez Rokah <[email protected]> * Update plugins/source_docs_test.go Co-authored-by: Erez Rokah <[email protected]> * fix formmating Co-authored-by: Erez Rokah <[email protected]>
1 parent b2287f8 commit 6597df7

File tree

7 files changed

+110
-97
lines changed

7 files changed

+110
-97
lines changed

docs/source.go

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
# Source Plugin: test
3+
## Tables
4+
| Name | Description |
5+
| ------------- | ------------- |
6+
|test_table|Description for test table|
7+

docs/.snapshots/TestGenerateSourcePluginDocs-relation_table.md renamed to plugins/.snapshots/TestGenerateSourcePluginDocs-relation_table.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Table: relation_table
33
Description for relational table
44
## Columns
5-
| Name | Type |
5+
| Name | Type |
66
| ------------- | ------------- |
77
|string_col|String|
88
|_cq_id|UUID|

docs/.snapshots/TestGenerateSourcePluginDocs-test_table.md renamed to plugins/.snapshots/TestGenerateSourcePluginDocs-test_table.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Table: test_table
33
Description for test table
44
## Columns
5-
| Name | Type |
5+
| Name | Type |
66
| ------------- | ------------- |
77
|int_col|Int|
88
|_cq_id|UUID|

plugins/source_docs.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,97 @@
11
package plugins
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
"text/template"
9+
10+
"github.com/cloudquery/plugin-sdk/schema"
11+
)
12+
13+
const tableTmpl = `
14+
# Table: {{.Name}}
15+
{{ $.Description }}
16+
## Columns
17+
| Name | Type |
18+
| ------------- | ------------- |
19+
{{- range $column := $.Columns }}
20+
|{{$column.Name}}|{{$column.Type | formatType}}|
21+
{{- end }}
22+
`
23+
24+
const allTablesTpml = `
25+
# Source Plugin: {{.Name}}
26+
## Tables
27+
| Name | Description |
28+
| ------------- | ------------- |
29+
{{- range $table := $.Tables }}
30+
|{{$table.Name}}|{{$table.Description }}|
31+
{{- end }}
32+
`
33+
34+
// GenerateSourcePluginDocs creates table documentation for the source plugin based on its list of tables
35+
func (p *SourcePlugin) GenerateSourcePluginDocs(dir string) error {
36+
for _, table := range p.Tables() {
37+
if err := renderAllTables(table, dir); err != nil {
38+
fmt.Printf("render table %s error: %s", table.Name, err)
39+
return err
40+
}
41+
}
42+
t, err := template.New("").Parse(allTablesTpml)
43+
if err != nil {
44+
return fmt.Errorf("failed to parse template for README.md: %v", err)
45+
}
46+
47+
outputPath := filepath.Join(dir, "README.md")
48+
f, err := os.Create(outputPath)
49+
if err != nil {
50+
return fmt.Errorf("failed to create file %v: %v", outputPath, err)
51+
}
52+
defer f.Close()
53+
if err := t.Execute(f, p); err != nil {
54+
return fmt.Errorf("failed to execute template: %v", err)
55+
}
56+
return nil
57+
}
58+
59+
func renderAllTables(t *schema.Table, dir string) error {
60+
if err := renderTable(t, dir); err != nil {
61+
return err
62+
}
63+
for _, r := range t.Relations {
64+
if err := renderAllTables(r, dir); err != nil {
65+
return err
66+
}
67+
}
68+
return nil
69+
}
70+
71+
func renderTable(table *schema.Table, dir string) error {
72+
t := template.New("").Funcs(map[string]interface{}{
73+
"formatType": formatType,
74+
"removeLineBreaks": func(text string) string {
75+
return strings.ReplaceAll(text, "\n", " ")
76+
},
77+
})
78+
t, err := t.New("").Parse(tableTmpl)
79+
if err != nil {
80+
return fmt.Errorf("failed to parse template: %v", err)
81+
}
82+
83+
outputPath := filepath.Join(dir, fmt.Sprintf("%s.md", table.Name))
84+
f, err := os.Create(outputPath)
85+
if err != nil {
86+
return fmt.Errorf("failed to create file %v: %v", outputPath, err)
87+
}
88+
defer f.Close()
89+
if err := t.Execute(f, table); err != nil {
90+
return fmt.Errorf("failed to execute template: %v", err)
91+
}
92+
return nil
93+
}
94+
95+
func formatType(v schema.ValueType) string {
96+
return strings.TrimPrefix(v.String(), "Type")
97+
}
Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
1-
package docs
1+
package plugins
22

33
import (
4-
"context"
54
"os"
65
"path"
76
"testing"
87

98
"github.com/bradleyjkemp/cupaloy/v2"
10-
"github.com/cloudquery/plugin-sdk/plugins"
119
"github.com/cloudquery/plugin-sdk/schema"
12-
"github.com/cloudquery/plugin-sdk/specs"
13-
"github.com/rs/zerolog"
1410
"github.com/stretchr/testify/require"
1511
)
1612

17-
type testExecutionClient struct {
18-
logger zerolog.Logger
19-
}
20-
2113
var testTables = []*schema.Table{
2214
{
2315
Name: "test_table",
@@ -43,28 +35,20 @@ var testTables = []*schema.Table{
4335
},
4436
}
4537

46-
func (c *testExecutionClient) Logger() *zerolog.Logger {
47-
return &c.logger
48-
}
49-
50-
func newTestExecutionClient(context.Context, zerolog.Logger, specs.Source) (schema.ClientMeta, error) {
51-
return &testExecutionClient{}, nil
52-
}
53-
5438
func TestGenerateSourcePluginDocs(t *testing.T) {
5539
tmpdir, tmpErr := os.MkdirTemp("", "docs_test_*")
5640
if tmpErr != nil {
5741
t.Fatalf("failed to create temporary directory: %v", tmpErr)
5842
}
5943
defer os.RemoveAll(tmpdir)
6044

61-
p := plugins.NewSourcePlugin("test", "v1.0.0", testTables, newTestExecutionClient)
62-
err := GenerateSourcePluginDocs(p, tmpdir)
45+
p := NewSourcePlugin("test", "v1.0.0", testTables, newTestExecutionClient)
46+
err := p.GenerateSourcePluginDocs(tmpdir)
6347
if err != nil {
6448
t.Fatalf("unexpected error calling GenerateSourcePluginDocs: %v", err)
6549
}
6650

67-
expectFiles := []string{"test_table.md", "relation_table.md"}
51+
expectFiles := []string{"test_table.md", "relation_table.md", "README.md"}
6852
for _, exp := range expectFiles {
6953
t.Run(exp, func(t *testing.T) {
7054
output := path.Join(tmpdir, exp)

serve/doc.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package serve
44
import (
55
"fmt"
66

7-
"github.com/cloudquery/plugin-sdk/docs"
87
"github.com/spf13/cobra"
98
)
109

@@ -22,8 +21,7 @@ func newCmdDoc(opts Options) *cobra.Command {
2221
if opts.SourcePlugin == nil {
2322
return fmt.Errorf("doc generation is only supported for source plugins")
2423
}
25-
26-
return docs.GenerateSourcePluginDocs(opts.SourcePlugin, args[0])
24+
return opts.SourcePlugin.GenerateSourcePluginDocs(args[0])
2725
},
2826
}
2927
}

0 commit comments

Comments
 (0)