-
Notifications
You must be signed in to change notification settings - Fork 4
/
views_cmd.go
168 lines (153 loc) · 4.49 KB
/
views_cmd.go
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package ddl
import (
"database/sql"
"flag"
"fmt"
"go/format"
"io"
"os"
"path/filepath"
"strings"
)
// ViewsCmd implements the `sqddl views` subcommand.
type ViewsCmd struct {
// (Required) DB is the database.
DB *sql.DB
// (Required) Dialect is the database dialect.
Dialect string
// PackageName is the name of the package for the generated go code. Leave
// blank to use "_".
PackageName string
// Filename is the name of the file to write the table structs into. Leave
// blank to write to Stdout instead.
Filename string
// Stdout specifies the command's standard out to write to if no Filename
// is provided. If nil, the command writes to os.Stdout.
Stdout io.Writer
// Schemas is the list of schemas that will be included.
Schemas []string
// ExcludeSchemas is the list of schemas that will be excluded.
ExcludeSchemas []string
// Views is the list of tables that will be included.
Views []string
// ExcludeViews is the list of tables that will be excluded.
ExcludeViews []string
db string // -db flag.
}
func ViewsCommand(args ...string) (*ViewsCmd, error) {
var cmd ViewsCmd
var historyTable, schemas, excludeSchemas, views, excludeViews string
flagset := flag.NewFlagSet("", flag.ContinueOnError)
flagset.StringVar(&cmd.db, "db", "", "(required) Database URL/DSN.")
flagset.StringVar(&historyTable, "history-table", "", "(ignored)")
flagset.StringVar(&cmd.PackageName, "pkg", "_", "Package name used in the generated code.")
flagset.StringVar(&cmd.Filename, "file", "", "Name of the file to write the generated code into. Leave blank to write to stdout.")
flagset.StringVar(&schemas, "schemas", "", "Comma-separated list of schemas to be included.")
flagset.StringVar(&excludeSchemas, "exclude-schemas", "", "Comma-separated list of schemas to be excluded.")
flagset.StringVar(&views, "views", "", "Comma-separated list of views to be included.")
flagset.StringVar(&excludeViews, "exclude-views", "", "Comma-separated list of views to be excluded.")
flagset.Usage = func() {
fmt.Fprint(flagset.Output(), `Usage:
sqddl views -db <DATABASE_URL> [FLAGS]
sqddl views -db 'postgres://user:pass@localhost:5432/sakila'
sqddl views -db 'postgres://user:pass@localhost:5432/sakila' -pkg tables -file tables/views.go
sqddl views -db 'postgres://user:pass@localhost:5432/sakila' -schemas schema1,schema2,schema3 -exclude-views view1,view2,view3
Flags:
`)
flagset.PrintDefaults()
}
err := flagset.Parse(args)
if err != nil {
return nil, err
}
if cmd.db == "" {
return nil, fmt.Errorf("-db empty or not provided")
}
if schemas != "" {
cmd.Schemas = strings.Split(schemas, ",")
}
if excludeSchemas != "" {
cmd.ExcludeSchemas = strings.Split(excludeSchemas, ",")
}
if views != "" {
cmd.Views = strings.Split(views, ",")
}
if excludeViews != "" {
cmd.ExcludeViews = strings.Split(excludeViews, ",")
}
var driverName, dsn string
cmd.Dialect, driverName, dsn = NormalizeDSN(cmd.db)
if cmd.Dialect == "" {
return nil, fmt.Errorf("could not identity dialect for -db %q", cmd.db)
}
cmd.DB, err = sql.Open(driverName, dsn)
if err != nil {
return nil, err
}
return &cmd, nil
}
func (cmd *ViewsCmd) Run() error {
if cmd.DB == nil {
return fmt.Errorf("nil DB")
}
if cmd.Dialect == "" {
return fmt.Errorf("empty Dialect")
}
if cmd.Stdout == nil {
cmd.Stdout = os.Stdout
}
if cmd.db != "" {
defer cmd.DB.Close()
}
viewStructs, err := NewViewStructs(cmd.Dialect, cmd.DB, Filter{
Schemas: cmd.Schemas,
ExcludeSchemas: cmd.ExcludeSchemas,
Views: cmd.Views,
ExcludeViews: cmd.ExcludeViews,
})
if err != nil {
return err
}
text, err := viewStructs.MarshalText()
if err != nil {
return err
}
if len(text) == 0 {
return nil
}
if cmd.PackageName == "" && cmd.Filename != "" {
err = os.MkdirAll(filepath.Dir(cmd.Filename), 0755)
if err != nil {
return err
}
dirname := filepath.Base(filepath.Dir(cmd.Filename))
if dirname != "" && dirname != "." {
cmd.PackageName = strings.ReplaceAll(dirname, " ", "_")
}
}
if cmd.PackageName == "" {
cmd.PackageName = "_"
}
text, err = format.Source(text)
if err != nil {
return err
}
out := cmd.Stdout
if cmd.Filename != "" {
file, err := os.OpenFile(cmd.Filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer file.Close()
out = file
}
_, err = io.WriteString(out, "package "+cmd.PackageName+"\n\nimport \"github.com/bokwoon95/sq\"\n\n")
if err != nil {
return err
}
_, err = out.Write(text)
if err != nil {
return err
}
return nil
}