-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathgenerator.go
More file actions
149 lines (129 loc) · 3.29 KB
/
generator.go
File metadata and controls
149 lines (129 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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)
}
}
func (g *Generator) GenerateJSON(dir string, format Format) error {
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return err
}
switch format {
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)
// }
// }