Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
129 commits
Select commit Hold shift + click to select a range
8cdb4f7
move to one plugin wip
yevgenypats May 28, 2023
1dd18d5
wip
yevgenypats May 30, 2023
eea010c
more wip
yevgenypats May 30, 2023
89aa95b
wip
yevgenypats May 31, 2023
b8ab1a1
more wip
yevgenypats May 31, 2023
f5fa791
more wip
yevgenypats Jun 1, 2023
50bd8f1
wip
yevgenypats Jun 2, 2023
87ff461
more wip
yevgenypats Jun 4, 2023
04f8c28
more wip
yevgenypats Jun 4, 2023
645842c
more work
yevgenypats Jun 4, 2023
e34b7f7
more wip
yevgenypats Jun 5, 2023
93dfff6
more wip
yevgenypats Jun 5, 2023
e239857
wip
yevgenypats Jun 5, 2023
71a960f
more wip
yevgenypats Jun 5, 2023
b66e12a
more wip
yevgenypats Jun 10, 2023
2287642
more wip
yevgenypats Jun 12, 2023
da742a7
fix
yevgenypats Jun 12, 2023
e6b8c41
Add MixedBatchWriter (WIP)
hermanschaaf Jun 13, 2023
d2de7f1
workers, but probably won't use this
hermanschaaf Jun 14, 2023
1b79d01
Simplified mixed batch writer
hermanschaaf Jun 14, 2023
b0aa429
Update New
hermanschaaf Jun 15, 2023
69525b4
Add GetTables() to Message interface
hermanschaaf Jun 15, 2023
4b31116
fix batchwriter
yevgenypats Jun 15, 2023
86e6f68
Fix tests
hermanschaaf Jun 15, 2023
61b8fab
make migrate_force part of write options
yevgenypats Jun 15, 2023
d75995f
Rename
hermanschaaf Jun 15, 2023
b158831
Add EnablePrimaryKeys
hermanschaaf Jun 15, 2023
b9b2316
Undo table change
hermanschaaf Jun 15, 2023
bcbf81c
use read method instead of sync for write testing
yevgenypats Jun 15, 2023
31464e9
rebase complete
yevgenypats Jun 15, 2023
934cfcd
nits
yevgenypats Jun 15, 2023
20342b4
fix some more tests
yevgenypats Jun 16, 2023
7a59ca7
Rename interfaces to Source/Destination, couple of testing bugfixes
hermanschaaf Jun 16, 2023
b0dff85
Use only 8 chars of uuid
hermanschaaf Jun 16, 2023
2ce96f0
Rename to MigrateTable
hermanschaaf Jun 16, 2023
072a48e
Move messages package and many other changes
hermanschaaf Jun 16, 2023
491f2c2
move to []byte
yevgenypats Jun 16, 2023
0b9625d
fix data race
yevgenypats Jun 16, 2023
5df2175
tests passing
yevgenypats Jun 16, 2023
ca4de2c
Remove GetSpec
yevgenypats Jun 16, 2023
6dd7795
fix destination v0
yevgenypats Jun 16, 2023
16753fd
more wip
yevgenypats Jun 17, 2023
72f0029
discovery versions: int32 instead of uint64 as is such in the proto
disq Jun 20, 2023
cae94ee
Add test for NewRecordFromBytes
hermanschaaf Jun 21, 2023
3167a56
Add better migration support
hermanschaaf Jun 22, 2023
1969467
commit fixes
yevgenypats Jun 22, 2023
07d79cd
migrate flattend tables
yevgenypats Jun 22, 2023
dbd7142
Remove upserts
hermanschaaf Jun 23, 2023
701338b
fix tests
yevgenypats Jun 23, 2023
75af249
rebase complete
yevgenypats Jun 23, 2023
a245984
move to one plugin wip
yevgenypats May 28, 2023
1af9d0e
wip
yevgenypats May 30, 2023
85ef171
more wip
yevgenypats May 30, 2023
d3da366
wip
yevgenypats May 31, 2023
2fbd64d
more wip
yevgenypats May 31, 2023
3e76a4a
more wip
yevgenypats Jun 1, 2023
3d2b0f4
wip
yevgenypats Jun 2, 2023
ff8d458
more wip
yevgenypats Jun 4, 2023
6f85bf7
more wip
yevgenypats Jun 4, 2023
88ccb18
more work
yevgenypats Jun 4, 2023
6846b97
more wip
yevgenypats Jun 5, 2023
101b5ac
more wip
yevgenypats Jun 5, 2023
6ed42c0
wip
yevgenypats Jun 5, 2023
ade2629
more wip
yevgenypats Jun 5, 2023
540a99e
more wip
yevgenypats Jun 10, 2023
a10fd2c
more wip
yevgenypats Jun 12, 2023
8a4fe3e
fix
yevgenypats Jun 12, 2023
5c78241
Add MixedBatchWriter (WIP)
hermanschaaf Jun 13, 2023
85e6043
workers, but probably won't use this
hermanschaaf Jun 14, 2023
1e31a3a
Simplified mixed batch writer
hermanschaaf Jun 14, 2023
6528c5e
Update New
hermanschaaf Jun 15, 2023
a787638
Add GetTables() to Message interface
hermanschaaf Jun 15, 2023
5b8ed22
fix batchwriter
yevgenypats Jun 15, 2023
1f8480c
Fix tests
hermanschaaf Jun 15, 2023
13958e6
make migrate_force part of write options
yevgenypats Jun 15, 2023
69b76d9
Rename
hermanschaaf Jun 15, 2023
6174649
Add EnablePrimaryKeys
hermanschaaf Jun 15, 2023
bff2523
Undo table change
hermanschaaf Jun 15, 2023
8d450c3
use read method instead of sync for write testing
yevgenypats Jun 15, 2023
188a8e5
rebase complete
yevgenypats Jun 15, 2023
76b8233
nits
yevgenypats Jun 15, 2023
af9e518
fix some more tests
yevgenypats Jun 16, 2023
89c32ef
Rename interfaces to Source/Destination, couple of testing bugfixes
hermanschaaf Jun 16, 2023
01213f0
Use only 8 chars of uuid
hermanschaaf Jun 16, 2023
ba4dd22
Rename to MigrateTable
hermanschaaf Jun 16, 2023
71e56da
Move messages package and many other changes
hermanschaaf Jun 16, 2023
3d612fd
move to []byte
yevgenypats Jun 16, 2023
fd33dde
fix data race
yevgenypats Jun 16, 2023
5c1bf00
tests passing
yevgenypats Jun 16, 2023
29473df
Remove GetSpec
yevgenypats Jun 16, 2023
8c6e325
fix destination v0
yevgenypats Jun 16, 2023
7ee3599
more wip
yevgenypats Jun 17, 2023
54c8eaa
discovery versions: int32 instead of uint64 as is such in the proto
disq Jun 20, 2023
cb2955d
Add test for NewRecordFromBytes
hermanschaaf Jun 21, 2023
49b01e6
Add better migration support
hermanschaaf Jun 22, 2023
c6440a5
commit fixes
yevgenypats Jun 22, 2023
a8b629e
migrate flattend tables
yevgenypats Jun 22, 2023
10ce6db
Remove upserts
hermanschaaf Jun 23, 2023
e6a9949
fix tests
yevgenypats Jun 23, 2023
338e57a
rebase complete
yevgenypats Jun 23, 2023
9133c2b
Merge branch 'feat/sdkv4' of github.com:cloudquery/plugin-sdk into fe…
hermanschaaf Jun 23, 2023
b556091
remove cover
yevgenypats Jun 23, 2023
919770a
Converting between bytes is the responsibility of plugin-pb-go
hermanschaaf Jun 23, 2023
bccf74e
Merge branch 'feat/sdkv4' of github.com:cloudquery/plugin-sdk into fe…
hermanschaaf Jun 23, 2023
9c050a1
fix tests
yevgenypats Jun 23, 2023
203e13f
fmt
yevgenypats Jun 23, 2023
a55b2ab
Remove upsert
disq Jun 23, 2023
cb3a6ee
fix some lints
yevgenypats Jun 23, 2023
97110aa
lint
yevgenypats Jun 23, 2023
1df923d
tests passing
yevgenypats Jun 23, 2023
b0cfbbf
lints
yevgenypats Jun 23, 2023
45b18fd
more lint stuff
yevgenypats Jun 23, 2023
0d2fcf9
add lint
yevgenypats Jun 23, 2023
550f315
lint
yevgenypats Jun 23, 2023
e88cb9d
fix lint
yevgenypats Jun 23, 2023
20befbe
merge
yevgenypats Jun 23, 2023
1a744ce
merge with main
yevgenypats Jun 23, 2023
5807368
fix tests
yevgenypats Jun 24, 2023
64995a0
add more tests
yevgenypats Jun 24, 2023
f522ea0
more tests
yevgenypats Jun 24, 2023
908fce1
remove buf.yaml
yevgenypats Jun 24, 2023
12f41ff
more tests
yevgenypats Jun 24, 2023
9486b50
remove empty files
yevgenypats Jun 24, 2023
0c21711
nits
yevgenypats Jun 24, 2023
d3cd5aa
make state it's own package
yevgenypats Jun 25, 2023
7dc094b
batchWriter worker: Fix for batch size overflow check
disq Jun 26, 2023
104819f
Merge branch 'main' into feat/sdkv4
erezrokah Jun 26, 2023
b7ede75
mod tidy
yevgenypats Jun 26, 2023
4402453
Update lint_markdown.yml
erezrokah Jun 26, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/lint_markdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Vale
uses: errata-ai/vale-action@v2
with:
vale_flags: "--glob=!{plugins/source/testdata/*,CHANGELOG.md,.github/styles/proselint/README.md}"
vale_flags: "--glob=!{docs/testdata/*,CHANGELOG.md,.github/styles/proselint/README.md}"
filter_mode: nofilter
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
Expand All @@ -31,4 +31,4 @@ jobs:
with:
files: .
config_file: .markdownlint.yaml
ignore_files: "{plugins/source/testdata/*,CHANGELOG.md}"
ignore_files: "{docs/testdata/*,CHANGELOG.md}"
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ config.hcl
vendor
cover.out
.delta.*
bench.json
bench.json
serve/^TestPluginDocs$/
cover
12 changes: 0 additions & 12 deletions backend/backend.go

This file was deleted.

12 changes: 0 additions & 12 deletions buf.yaml

This file was deleted.

137 changes: 137 additions & 0 deletions docs/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package docs

import (
"embed"
"fmt"
"os"
"regexp"
"sort"

"github.com/cloudquery/plugin-sdk/v4/caser"
"github.com/cloudquery/plugin-sdk/v4/schema"
)

//go:embed templates/*.go.tpl
var templatesFS embed.FS

var reMatchNewlines = regexp.MustCompile(`\n{3,}`)
var reMatchHeaders = regexp.MustCompile(`(#{1,6}.+)\n+`)

var DefaultTitleExceptions = map[string]string{
// common abbreviations
"acl": "ACL",
"acls": "ACLs",
"api": "API",
"apis": "APIs",
"ca": "CA",
"cidr": "CIDR",
"cidrs": "CIDRs",
"db": "DB",
"dbs": "DBs",
"dhcp": "DHCP",
"iam": "IAM",
"iot": "IOT",
"ip": "IP",
"ips": "IPs",
"ipv4": "IPv4",
"ipv6": "IPv6",
"mfa": "MFA",
"ml": "ML",
"oauth": "OAuth",
"vpc": "VPC",
"vpcs": "VPCs",
"vpn": "VPN",
"vpns": "VPNs",
"waf": "WAF",
"wafs": "WAFs",

// cloud providers
"aws": "AWS",
"gcp": "GCP",
}

type Format int

const (
FormatMarkdown Format = iota
FormatJSON
)

func (r Format) String() string {
return [...]string{"markdown", "json"}[r]
}

func FormatFromString(s string) (Format, error) {
switch s {
case "markdown":
return FormatMarkdown, nil
case "json":
return FormatJSON, nil
default:
return FormatMarkdown, fmt.Errorf("unknown format %s", s)
}
}

type Generator struct {
tables schema.Tables
titleTransformer func(*schema.Table) string
pluginName string
}

func DefaultTitleTransformer(table *schema.Table) string {
if table.Title != "" {
return table.Title
}
csr := caser.New(caser.WithCustomExceptions(DefaultTitleExceptions))
return csr.ToTitle(table.Name)
}

func sortTables(tables schema.Tables) {
sort.SliceStable(tables, func(i, j int) bool {
return tables[i].Name < tables[j].Name
})

for _, table := range tables {
sortTables(table.Relations)
}
}

// NewGenerator creates a new generator for the given tables.
// The tables are sorted by name. pluginName is optional and is used in markdown only
func NewGenerator(pluginName string, tables schema.Tables) *Generator {
sortedTables := make(schema.Tables, 0, len(tables))
for _, t := range tables {
sortedTables = append(sortedTables, t.Copy(nil))
}
sortTables(sortedTables)

return &Generator{
tables: sortedTables,
titleTransformer: DefaultTitleTransformer,
pluginName: pluginName,
}
}

func (g *Generator) Generate(dir string, format Format) error {
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return err
}

switch format {
case FormatMarkdown:
return g.renderTablesAsMarkdown(dir)
case FormatJSON:
return g.renderTablesAsJSON(dir)
default:
return fmt.Errorf("unsupported format: %v", format)
}
}

// setDestinationManagedCqColumns overwrites or adds the CQ columns that are managed by the destination plugins (_cq_sync_time, _cq_source_name).
// func setDestinationManagedCqColumns(tables []*schema.Table) {
// for _, table := range tables {
// table.OverwriteOrAddColumn(&schema.CqSyncTimeColumn)
// table.OverwriteOrAddColumn(&schema.CqSourceNameColumn)
// setDestinationManagedCqColumns(table.Relations)
// }
// }
13 changes: 6 additions & 7 deletions plugins/source/docs_test.go → docs/generator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build !windows

package source
package docs

import (
"os"
Expand All @@ -9,8 +9,8 @@ import (

"github.com/apache/arrow/go/v13/arrow"
"github.com/bradleyjkemp/cupaloy/v2"
"github.com/cloudquery/plugin-sdk/v3/schema"
"github.com/cloudquery/plugin-sdk/v3/types"
"github.com/cloudquery/plugin-sdk/v4/schema"
"github.com/cloudquery/plugin-sdk/v4/types"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -120,14 +120,13 @@ var testTables = []*schema.Table{
}

func TestGeneratePluginDocs(t *testing.T) {
p := NewPlugin("test", "v1.0.0", testTables, newTestExecutionClient)

g := NewGenerator("test", testTables)
cup := cupaloy.New(cupaloy.SnapshotSubdirectory("testdata"))

t.Run("Markdown", func(t *testing.T) {
tmpdir := t.TempDir()

err := p.GeneratePluginDocs(tmpdir, "markdown")
err := g.Generate(tmpdir, FormatMarkdown)
if err != nil {
t.Fatalf("unexpected error calling GeneratePluginDocs: %v", err)
}
Expand All @@ -146,7 +145,7 @@ func TestGeneratePluginDocs(t *testing.T) {
t.Run("JSON", func(t *testing.T) {
tmpdir := t.TempDir()

err := p.GeneratePluginDocs(tmpdir, "json")
err := g.Generate(tmpdir, FormatJSON)
if err != nil {
t.Fatalf("unexpected error calling GeneratePluginDocs: %v", err)
}
Expand Down
62 changes: 62 additions & 0 deletions docs/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package docs

import (
"bytes"
"encoding/json"
"os"
"path/filepath"

"github.com/cloudquery/plugin-sdk/v4/schema"
)

type jsonTable struct {
Name string `json:"name"`
Title string `json:"title"`
Description string `json:"description"`
Columns []jsonColumn `json:"columns"`
Relations []jsonTable `json:"relations"`
}

type jsonColumn struct {
Name string `json:"name"`
Type string `json:"type"`
IsPrimaryKey bool `json:"is_primary_key,omitempty"`
IsIncrementalKey bool `json:"is_incremental_key,omitempty"`
}

func (g *Generator) renderTablesAsJSON(dir string) error {
jsonTables := g.jsonifyTables(g.tables)
buffer := &bytes.Buffer{}
m := json.NewEncoder(buffer)
m.SetIndent("", " ")
m.SetEscapeHTML(false)
err := m.Encode(jsonTables)
if err != nil {
return err
}
outputPath := filepath.Join(dir, "__tables.json")
return os.WriteFile(outputPath, buffer.Bytes(), 0644)
}

func (g *Generator) jsonifyTables(tables schema.Tables) []jsonTable {
jsonTables := make([]jsonTable, len(tables))
for i, table := range tables {
jsonColumns := make([]jsonColumn, len(table.Columns))
for c, col := range table.Columns {
jsonColumns[c] = jsonColumn{
Name: col.Name,
Type: col.Type.String(),
IsPrimaryKey: col.PrimaryKey,
IsIncrementalKey: col.IncrementalKey,
}
}
jsonTables[i] = jsonTable{
Name: table.Name,
Title: g.titleTransformer(table),
Description: table.Description,
Columns: jsonColumns,
Relations: g.jsonifyTables(table.Relations),
}
}
return jsonTables
}
94 changes: 94 additions & 0 deletions docs/markdown.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package docs

import (
"bytes"
"fmt"
"os"
"path/filepath"
"text/template"

"github.com/cloudquery/plugin-sdk/v4/schema"
)

type templateData struct {
PluginName string
Tables schema.Tables
}

func (g *Generator) renderTablesAsMarkdown(dir string) error {
for _, table := range g.tables {
if err := g.renderAllTables(dir, table); err != nil {
return err
}
}
t, err := template.New("all_tables.md.go.tpl").Funcs(template.FuncMap{
"indentToDepth": indentToDepth,
}).ParseFS(templatesFS, "templates/all_tables*.md.go.tpl")
if err != nil {
return fmt.Errorf("failed to parse template for README.md: %v", err)
}

var b bytes.Buffer
if err := t.Execute(&b, templateData{PluginName: g.pluginName, Tables: g.tables}); err != nil {
return fmt.Errorf("failed to execute template: %v", err)
}
content := formatMarkdown(b.String())
outputPath := filepath.Join(dir, "README.md")
f, err := os.Create(outputPath)
if err != nil {
return fmt.Errorf("failed to create file %v: %v", outputPath, err)
}
f.WriteString(content)
return nil
}

func (g *Generator) renderAllTables(dir string, t *schema.Table) error {
if err := g.renderTable(dir, t); err != nil {
return err
}
for _, r := range t.Relations {
if err := g.renderAllTables(dir, r); err != nil {
return err
}
}
return nil
}

func (g *Generator) renderTable(dir string, table *schema.Table) error {
t := template.New("").Funcs(map[string]any{
"title": g.titleTransformer,
})
t, err := t.New("table.md.go.tpl").ParseFS(templatesFS, "templates/table.md.go.tpl")
if err != nil {
return fmt.Errorf("failed to parse template: %v", err)
}

outputPath := filepath.Join(dir, fmt.Sprintf("%s.md", table.Name))

var b bytes.Buffer
if err := t.Execute(&b, table); err != nil {
return fmt.Errorf("failed to execute template: %v", err)
}
content := formatMarkdown(b.String())
f, err := os.Create(outputPath)
if err != nil {
return fmt.Errorf("failed to create file %v: %v", outputPath, err)
}
f.WriteString(content)
return f.Close()
}

func formatMarkdown(s string) string {
s = reMatchNewlines.ReplaceAllString(s, "\n\n")
return reMatchHeaders.ReplaceAllString(s, `$1`+"\n\n")
}

func indentToDepth(table *schema.Table) string {
s := ""
t := table
for t.Parent != nil {
s += " "
t = t.Parent
}
return s
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ The following tables depend on {{.Name}}:
| Name | Type |
| ------------- | ------------- |
{{- range $column := $.Columns }}
|{{$column.Name}}{{if $column.PrimaryKey}} (PK){{end}}{{if $column.IncrementalKey}} (Incremental Key){{end}}|`{{$column.Type}}`|
|{{$column.Name}}{{if $column.PrimaryKey}} (PK){{end}}{{if $column.IncrementalKey}} (Incremental Key){{end}}|{{$column.Type}}|
{{- end }}
Loading