Skip to content

Commit e2e8575

Browse files
authored
feat: Add protocol v3 tables command (move doc generation to CLI) (#12758)
This seems to work, but marking as draft because without the change in cloudquery/plugin-sdk#1129 the docs will be missing some information. I also still need to update the code here to populate table relationship data based on the parent fields by walking the tree, so that this information will be correctly displayed in the docs.
1 parent b834ad3 commit e2e8575

21 files changed

+768
-46
lines changed

.github/workflows/lint_markdown.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- name: Vale
1818
uses: errata-ai/vale-action@v2
1919
with:
20-
vale_flags: "--glob=!{website/pages/blog/podcast-software-engineer-daily.md,*CHANGELOG.md,*/docs/tables/README.md,plugins/source/test/docs/tables/*.md,.github/styles/proselint/README.md,**/v1-migration.md,website/pages/docs/plugins/sources/*/tables.md,website/pages/docs/plugins/sources/*/policies.md,website/tables/**/*,website/pages/case-studies/*.mdx}"
20+
vale_flags: "--glob=!{website/pages/blog/podcast-software-engineer-daily.md,*CHANGELOG.md,*/docs/tables/README.md,plugins/source/test/docs/tables/*.md,.github/styles/proselint/README.md,**/v1-migration.md,website/pages/docs/plugins/sources/*/tables.md,website/pages/docs/plugins/sources/*/policies.md,website/tables/**/*,website/pages/case-studies/*.mdx,cli/internal/docs/testdata/*.md}"
2121
filter_mode: nofilter
2222
fail_on_error: true
2323
env:

cli/cmd/tables.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ func tables(cmd *cobra.Command, args []string) error {
9090
maxVersion := findMaxCommonVersion(versions, []int{2, 3})
9191
switch maxVersion {
9292
case 3:
93-
return fmt.Errorf("the CLI tables command is not supported for sources with protocol version 3. Please upvote https://github.com/cloudquery/cloudquery/issues/12270 if you need this functionality")
93+
if err := tablesV3(ctx, cl, outputPath, format); err != nil {
94+
return err
95+
}
9496
case 2:
9597
if err := tablesV2(ctx, cl, outputPath, format); err != nil {
9698
return err

cli/cmd/tables_v3.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
6+
"github.com/cloudquery/cloudquery/cli/internal/docs"
7+
"github.com/cloudquery/plugin-pb-go/managedplugin"
8+
pluginPb "github.com/cloudquery/plugin-pb-go/pb/plugin/v3"
9+
"github.com/cloudquery/plugin-sdk/v4/schema"
10+
"github.com/cloudquery/plugin-sdk/v4/types"
11+
)
12+
13+
func tablesV3(ctx context.Context, sourceClient *managedplugin.Client, path string, format string) error {
14+
err := types.RegisterAllExtensions()
15+
if err != nil {
16+
return err
17+
}
18+
sourcePbClient := pluginPb.NewPluginClient(sourceClient.Conn)
19+
if _, err := sourcePbClient.Init(ctx, &pluginPb.Init_Request{
20+
NoConnection: true,
21+
}); err != nil {
22+
return err
23+
}
24+
getTablesResp, err := sourcePbClient.GetTables(ctx, &pluginPb.GetTables_Request{
25+
Tables: []string{"*"},
26+
})
27+
if err != nil {
28+
return err
29+
}
30+
name, err := sourcePbClient.GetName(ctx, &pluginPb.GetName_Request{})
31+
if err != nil {
32+
return err
33+
}
34+
35+
schemas, err := pluginPb.NewSchemasFromBytes(getTablesResp.Tables)
36+
if err != nil {
37+
return err
38+
}
39+
tables, err := schema.NewTablesFromArrowSchemas(schemas)
40+
if err != nil {
41+
return err
42+
}
43+
topLevelTables, err := tables.UnflattenTables()
44+
if err != nil {
45+
return err
46+
}
47+
48+
g := docs.NewGenerator(name.Name, topLevelTables)
49+
f := docs.FormatMarkdown
50+
if format == "json" {
51+
f = docs.FormatJSON
52+
}
53+
return g.Generate(path, f)
54+
}

cli/docs/images/logo.png

-12.7 KB
Binary file not shown.

cli/docs/index.md

Lines changed: 0 additions & 17 deletions
This file was deleted.

cli/go.mod

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ go 1.20
44

55
require (
66
github.com/apache/arrow/go/v13 v13.0.0-20230731205701-112f94971882
7+
github.com/bradleyjkemp/cupaloy/v2 v2.8.0
78
github.com/cloudquery/plugin-pb-go v1.9.2
9+
github.com/cloudquery/plugin-sdk/v4 v4.4.0
810
github.com/getsentry/sentry-go v0.20.0
911
github.com/ghodss/yaml v1.0.0
1012
github.com/google/go-cmp v0.5.9
@@ -15,7 +17,7 @@ require (
1517
github.com/stretchr/testify v1.8.4
1618
github.com/thoas/go-funk v0.9.3
1719
golang.org/x/exp v0.0.0-20230728194245-b0cb94b80691
18-
google.golang.org/grpc v1.55.0
20+
google.golang.org/grpc v1.57.0
1921
google.golang.org/protobuf v1.31.0
2022
gopkg.in/yaml.v3 v3.0.1
2123
)
@@ -37,7 +39,6 @@ require (
3739
github.com/inconshreveable/mousetrap v1.1.0 // indirect
3840
github.com/klauspost/compress v1.16.7 // indirect
3941
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
40-
github.com/kr/pretty v0.3.0 // indirect
4142
github.com/mattn/go-colorable v0.1.13 // indirect
4243
github.com/mattn/go-isatty v0.0.19 // indirect
4344
github.com/mattn/go-runewidth v0.0.15 // indirect
@@ -53,14 +54,13 @@ require (
5354
github.com/spf13/pflag v1.0.5 // indirect
5455
github.com/zeebo/xxh3 v1.0.2 // indirect
5556
golang.org/x/mod v0.11.0 // indirect
56-
golang.org/x/net v0.9.0 // indirect
57-
golang.org/x/sys v0.7.0 // indirect
58-
golang.org/x/term v0.7.0 // indirect
57+
golang.org/x/net v0.10.0 // indirect
58+
golang.org/x/sys v0.8.0 // indirect
59+
golang.org/x/term v0.8.0 // indirect
5960
golang.org/x/text v0.9.0 // indirect
6061
golang.org/x/tools v0.6.0 // indirect
6162
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
6263
google.golang.org/genproto/googleapis/rpc v0.0.0-20230731193218-e0aa005b6bdf // indirect
63-
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
6464
gopkg.in/yaml.v2 v2.4.0 // indirect
6565
)
6666

cli/go.sum

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc
44
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
55
github.com/avast/retry-go/v4 v4.3.4 h1:pHLkL7jvCvP317I8Ge+Km2Yhntv3SdkJm7uekkqbKhM=
66
github.com/avast/retry-go/v4 v4.3.4/go.mod h1:rv+Nla6Vk3/ilU0H51VHddWHiwimzX66yZ0JT6T+UvE=
7+
github.com/bradleyjkemp/cupaloy/v2 v2.8.0 h1:any4BmKE+jGIaMpnU8YgH/I2LPiLBufr6oMMlVBbn9M=
8+
github.com/bradleyjkemp/cupaloy/v2 v2.8.0/go.mod h1:bm7JXdkRd4BHJk9HpwqAI8BoAY1lps46Enkdqw6aRX0=
79
github.com/cloudquery/arrow/go/v13 v13.0.0-20230805001301-f53878dc8a89 h1:8AahfL6H9XYnz8DgCYeb9ed7sviEObA+nGdsdUIyerM=
810
github.com/cloudquery/arrow/go/v13 v13.0.0-20230805001301-f53878dc8a89/go.mod h1:W69eByFNO0ZR30q1/7Sr9d83zcVZmF2MiP3fFYAWJOc=
911
github.com/cloudquery/plugin-pb-go v1.9.2 h1:jApELKSgtyj1dKQlD2hKPMTFs1GqOdSK8u+5rEluj4M=
1012
github.com/cloudquery/plugin-pb-go v1.9.2/go.mod h1:f00zd6V5mWD+8Qw9U0eb4HD8RnAobwV9byBexE7Qa+0=
13+
github.com/cloudquery/plugin-sdk/v4 v4.4.0 h1:5dG0u7/XBn5HJRob74JyhMO5VksXQmDh5nZ3CJtEksQ=
14+
github.com/cloudquery/plugin-sdk/v4 v4.4.0/go.mod h1:IIgXIl/cuu8+1Kh1zKyrTKulaRXNqc3hesjO6axb9pE=
1115
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
1216
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
1317
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
14-
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
1518
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1619
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1720
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -53,14 +56,8 @@ github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGC
5356
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
5457
github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg=
5558
github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
56-
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
57-
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
58-
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
59-
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
60-
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
61-
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
59+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
6260
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
63-
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
6461
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
6562
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
6663
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
@@ -90,8 +87,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
9087
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
9188
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
9289
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
93-
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
94-
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
90+
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
9591
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
9692
github.com/rs/zerolog v1.29.1 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc=
9793
github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU=
@@ -106,10 +102,12 @@ github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUq
106102
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
107103
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
108104
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
105+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
109106
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
110107
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
111108
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
112109
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
110+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
113111
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
114112
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
115113
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
@@ -136,8 +134,8 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
136134
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
137135
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
138136
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
139-
golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM=
140-
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
137+
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
138+
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
141139
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
142140
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
143141
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -152,11 +150,11 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc
152150
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
153151
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
154152
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
155-
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
156-
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
153+
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
154+
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
157155
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
158-
golang.org/x/term v0.7.0 h1:BEvjmm5fURWqcfbSKTdpkDXYBrUS1c0m8agp14W48vQ=
159-
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
156+
golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols=
157+
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
160158
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
161159
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
162160
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
@@ -177,17 +175,14 @@ golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNq
177175
gonum.org/v1/gonum v0.12.0 h1:xKuo6hzt+gMav00meVPUlXwSdoEJP46BR+wdxQEFK2o=
178176
google.golang.org/genproto/googleapis/rpc v0.0.0-20230731193218-e0aa005b6bdf h1:guOdSPaeFgN+jEJwTo1dQ71hdBm+yKSCCKuTRkJzcVo=
179177
google.golang.org/genproto/googleapis/rpc v0.0.0-20230731193218-e0aa005b6bdf/go.mod h1:zBEcrKX2ZOcEkHWxBPAIvYUWOKKMIhYcmNiUIu2ji3I=
180-
google.golang.org/grpc v1.55.0 h1:3Oj82/tFSCeUrRTg/5E/7d/W5A1tj6Ky1ABAuZuv5ag=
181-
google.golang.org/grpc v1.55.0/go.mod h1:iYEXKGkEBhg1PjZQvoYEVPTDkHo1/bjTnfwTeGONTY8=
178+
google.golang.org/grpc v1.57.0 h1:kfzNeI/klCGD2YPMUlaGNT3pxvYfga7smW3Vth8Zsiw=
179+
google.golang.org/grpc v1.57.0/go.mod h1:Sd+9RMTACXwmub0zcNY2c4arhtrbBYD1AUHI/dt16Mo=
182180
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
183181
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
184182
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
185183
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
186184
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
187-
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
188185
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
189-
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
190-
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
191186
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
192187
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
193188
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

cli/internal/docs/generator.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package docs
2+
3+
import (
4+
"embed"
5+
"fmt"
6+
"os"
7+
"regexp"
8+
"sort"
9+
10+
"github.com/cloudquery/plugin-sdk/v4/schema"
11+
)
12+
13+
//go:embed templates/*.go.tpl
14+
var templatesFS embed.FS
15+
16+
var reMatchNewlines = regexp.MustCompile(`\n{3,}`)
17+
var reMatchHeaders = regexp.MustCompile(`(#{1,6}.+)\n+`)
18+
19+
type Format int
20+
21+
const (
22+
FormatMarkdown Format = iota
23+
FormatJSON
24+
)
25+
26+
func (r Format) String() string {
27+
return [...]string{"markdown", "json"}[r]
28+
}
29+
30+
func FormatFromString(s string) (Format, error) {
31+
switch s {
32+
case "markdown":
33+
return FormatMarkdown, nil
34+
case "json":
35+
return FormatJSON, nil
36+
default:
37+
return FormatMarkdown, fmt.Errorf("unknown format %s", s)
38+
}
39+
}
40+
41+
type Generator struct {
42+
tables schema.Tables
43+
pluginName string
44+
}
45+
46+
func sortTables(tables schema.Tables) {
47+
sort.SliceStable(tables, func(i, j int) bool {
48+
return tables[i].Name < tables[j].Name
49+
})
50+
51+
for _, table := range tables {
52+
sortTables(table.Relations)
53+
}
54+
}
55+
56+
// NewGenerator creates a new generator for the given tables.
57+
// The tables are sorted by name. pluginName is optional and is used in markdown only
58+
func NewGenerator(pluginName string, tables schema.Tables) *Generator {
59+
sortedTables := make(schema.Tables, 0, len(tables))
60+
for _, t := range tables {
61+
sortedTables = append(sortedTables, t.Copy(nil))
62+
}
63+
sortTables(sortedTables)
64+
65+
return &Generator{
66+
tables: sortedTables,
67+
pluginName: pluginName,
68+
}
69+
}
70+
71+
func (g *Generator) Generate(dir string, format Format) error {
72+
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
73+
return err
74+
}
75+
76+
switch format {
77+
case FormatMarkdown:
78+
return g.renderTablesAsMarkdown(dir)
79+
case FormatJSON:
80+
return g.renderTablesAsJSON(dir)
81+
default:
82+
return fmt.Errorf("unsupported format: %v", format)
83+
}
84+
}

0 commit comments

Comments
 (0)