-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
63 lines (51 loc) · 1.63 KB
/
main.go
File metadata and controls
63 lines (51 loc) · 1.63 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
package main
import (
"flag"
"fmt"
"os"
)
var (
dsn = flag.String("dsn", "username:password@tcp(localhost:3306)/database?charset=utf8mb4&parseTime=True&loc=Local&timeout=10s", "database connection string")
dir = flag.String("dir", "./output", "directory to save the file")
isWhole = flag.Bool("whole", false, "generate whole file (default false)")
isDDL = flag.Bool("ddl", false, "generate ddl info (default false)")
)
// run is the main entry point that parses flags and executes the schema documentation generation.
func run() error {
flag.Parse()
if *dsn == "" {
return fmt.Errorf("dsn is empty")
}
querier, err := NewGormQuerier(*dsn)
if err != nil {
return err
}
defer querier.Close()
return execute(querier, *dir, *isWhole, *isDDL)
}
// execute orchestrates the schema documentation generation process.
// It creates the output directory, queries the database metadata, and writes markdown files.
// When whole is true, a single file is generated; otherwise, separate files per table plus an index.
func execute(querier SchemaQuerier, outputDir string, whole, includeDDL bool) error {
if err := os.MkdirAll(outputDir, 0755); err != nil {
return fmt.Errorf("creating output directory %s: %w", outputDir, err)
}
dbName, err := querier.DatabaseName()
if err != nil {
return err
}
tables, err := querier.TableList()
if err != nil {
return err
}
if whole {
return WriteWholeFile(querier, outputDir, dbName, tables, includeDDL)
}
return WriteMultipleFiles(querier, outputDir, dbName, tables, includeDDL)
}
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
os.Exit(1)
}
}