Skip to content

Commit 6dd2cf1

Browse files
committed
Add --include/--exclude option / Change logic of --distance/--table for tbls out
1 parent f676c3a commit 6dd2cf1

File tree

10 files changed

+529
-161
lines changed

10 files changed

+529
-161
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ tbls.exe
33
dist/
44
dbdoc/
55
coverage.out
6-
testdb.sqlite3
6+
*.sqlite3
77
.envrc
88
.go-version
99
.tbls.yml

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ test_ext_subcommand: build
144144
env PATH="${PWD}/testdata/bin:${PATH}" TBLS_DSN=pg://postgres:pgpass@localhost:55432/testdb?sslmode=disable $(TBLS) echo | grep 'TBLS_DSN=pg://postgres:pgpass@localhost:55432/testdb?sslmode=disable' > /dev/null
145145
echo hello | env PATH="${PWD}/testdata/bin:${PATH}" $(TBLS) echo -c ./testdata/ext_subcommand_tbls.yml | grep 'STDIN=hello' > /dev/null
146146

147+
generate_test_json: build
148+
sqlite3 $(PWD)/filter_tables.sqlite3 < testdata/ddl/filter_tables.sql
149+
$(TBLS) out sq://$(PWD)/filter_tables.sqlite3 -t json > testdata/filter_tables.json
150+
147151
lint:
148152
golangci-lint run ./...
149153

cmd/doc.go

+3
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ func loadDocArgs(args []string) ([]config.Option, error) {
190190
options = append(options, config.ERSkip(withoutER))
191191
}
192192
options = append(options, config.BaseUrl(baseUrl))
193+
options = append(options, config.Include(includes))
194+
options = append(options, config.Exclude(excludes))
193195
if len(args) == 2 {
194196
options = append(options, config.DSNURL(args[0]))
195197
options = append(options, config.DocPath(args[1]))
@@ -227,6 +229,7 @@ func init() {
227229
docCmd.Flags().StringVarP(&when, "when", "", "", "command execute condition")
228230
docCmd.Flags().StringVarP(&baseUrl, "base-url", "b", "", "base url for links")
229231
docCmd.Flags().BoolVarP(&rmDist, "rm-dist", "", false, "remove files in docPath before generating documents")
232+
230233
if err := docCmd.MarkZshCompPositionalArgumentFile(2); err != nil {
231234
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err)
232235
os.Exit(1)

cmd/out.go

+12-17
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ import (
4141
)
4242

4343
var (
44-
format string
45-
outPath string
46-
tableName string
47-
distance int
44+
format string
45+
outPath string
46+
tables []string
47+
distance int
4848
)
4949

5050
// outCmd represents the doc command
@@ -125,17 +125,7 @@ var outCmd = &cobra.Command{
125125
wr = os.Stdout
126126
}
127127

128-
if tableName == "" {
129-
err = o.OutputSchema(wr, s)
130-
} else {
131-
t, ferr := s.FindTableByName(tableName)
132-
if ferr != nil {
133-
return err
134-
}
135-
err = o.OutputTable(wr, t)
136-
}
137-
138-
if err != nil {
128+
if err := o.OutputSchema(wr, s); err != nil {
139129
return err
140130
}
141131

@@ -153,6 +143,9 @@ func loadOutArgs(args []string) ([]config.Option, error) {
153143
}
154144
options = append(options, config.Distance(distance))
155145

146+
options = append(options, config.Include(append(tables, includes...)))
147+
options = append(options, config.Exclude(excludes))
148+
156149
if len(args) == 1 {
157150
options = append(options, config.DSNURL(args[0]))
158151
}
@@ -165,7 +158,9 @@ func init() {
165158
outCmd.Flags().StringVarP(&configPath, "config", "c", "", "config file path")
166159
outCmd.Flags().StringVarP(&format, "format", "t", "json", "output format")
167160
outCmd.Flags().StringVarP(&outPath, "out", "o", "", "output file path")
168-
outCmd.Flags().StringVar(&tableName, "table", "", "table name")
169-
outCmd.Flags().IntVarP(&distance, "distance", "", config.DefaultDistance, "distance between tables that display associations in the ER")
161+
outCmd.Flags().StringSliceVarP(&tables, "table", "", []string{}, "target table")
162+
outCmd.Flags().StringSliceVarP(&includes, "include", "", []string{}, "tables to include")
163+
outCmd.Flags().StringSliceVarP(&excludes, "exclude", "", []string{}, "tables to exclude")
164+
outCmd.Flags().IntVarP(&distance, "distance", "", 0, "distance between related tables to be displayed")
170165
outCmd.Flags().StringVarP(&when, "when", "", "", "command execute condition")
171166
}

cmd/root.go

+7
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,15 @@ var erFormat string
5555
// when is a option that command execute condition
5656
var when string
5757

58+
// base url for links
5859
var baseUrl string
5960

61+
// tables to include
62+
var includes []string
63+
64+
// tables to excludes
65+
var excludes []string
66+
6067
const rootUsageTemplate = `Usage:{{if .Runnable}}{{if ne .UseLine "tbls [flags]" }}
6168
{{.UseLine}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
6269
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}

config/config.go

+60-16
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ const DefaultERFormat = "svg"
2626

2727
const SchemaFileName = "schema.json"
2828

29-
// DefaultDistance is the default distance between tables that display relations in the ER
30-
var DefaultDistance = 1
29+
// DefaultERDistance is the default distance between tables that display relations in the ER
30+
var DefaultERDistance = 1
3131

3232
// Config is tbls config
3333
type Config struct {
@@ -41,6 +41,7 @@ type Config struct {
4141
ER ER `yaml:"er,omitempty"`
4242
Include []string `yaml:"include,omitempty"`
4343
Exclude []string `yaml:"exclude,omitempty"`
44+
Distance int `yaml:"distance,omitempty"`
4445
Lint Lint `yaml:"lint,omitempty"`
4546
LintExclude []string `yaml:"lintExclude,omitempty"`
4647
Relations []AdditionalRelation `yaml:"relations,omitempty"`
@@ -163,10 +164,10 @@ func ERFormat(erFormat string) Option {
163164
}
164165
}
165166

166-
// Distance return Option set Config.ER.Distance
167+
// Distance return Option set Config.Distance
167168
func Distance(distance int) Option {
168169
return func(c *Config) error {
169-
c.ER.Distance = &distance
170+
c.Distance = distance
170171
return nil
171172
}
172173
}
@@ -181,6 +182,26 @@ func BaseUrl(baseUrl string) Option {
181182
}
182183
}
183184

185+
// Include return Option set Config.Include
186+
func Include(i []string) Option {
187+
return func(c *Config) error {
188+
if len(i) > 0 {
189+
c.Include = i
190+
}
191+
return nil
192+
}
193+
}
194+
195+
// Exclude return Option set Config.Exclude
196+
func Exclude(e []string) Option {
197+
return func(c *Config) error {
198+
if len(e) > 0 {
199+
c.Exclude = e
200+
}
201+
return nil
202+
}
203+
}
204+
184205
// New return Config
185206
func New() (*Config, error) {
186207
c := Config{}
@@ -237,7 +258,7 @@ func (c *Config) setDefault() error {
237258
}
238259

239260
if c.ER.Distance == nil {
240-
c.ER.Distance = &DefaultDistance
261+
c.ER.Distance = &DefaultERDistance
241262
}
242263

243264
return nil
@@ -373,31 +394,54 @@ func (c *Config) MergeAdditionalData(s *schema.Schema) error {
373394
func (c *Config) FilterTables(s *schema.Schema) error {
374395
i := append(c.Include, s.NormalizeTableNames(c.Include)...)
375396
e := append(c.Exclude, s.NormalizeTableNames(c.Exclude)...)
397+
398+
includes := []*schema.Table{}
399+
excludes := []*schema.Table{}
376400
for _, t := range s.Tables {
377401
li, mi := matchLength(i, t.Name)
378402
le, me := matchLength(e, t.Name)
379403
switch {
380404
case len(c.Include) == 0:
381405
if me {
382-
err := excludeTableFromSchema(t.Name, s)
383-
if err != nil {
384-
return errors.Wrap(errors.WithStack(err), fmt.Sprintf("failed to filter table '%s'", t.Name))
385-
}
406+
excludes = append(excludes, t)
407+
continue
386408
}
409+
includes = append(includes, t)
387410
case mi:
388411
if me && li < le {
389-
err := excludeTableFromSchema(t.Name, s)
390-
if err != nil {
391-
return errors.Wrap(errors.WithStack(err), fmt.Sprintf("failed to filter table '%s'", t.Name))
392-
}
412+
excludes = append(excludes, t)
413+
continue
393414
}
415+
includes = append(includes, t)
394416
default:
395-
err := excludeTableFromSchema(t.Name, s)
396-
if err != nil {
397-
return errors.Wrap(errors.WithStack(err), fmt.Sprintf("failed to filter table '%s'", t.Name))
417+
// c.Include > 0 && !mi
418+
excludes = append(excludes, t)
419+
}
420+
}
421+
422+
collects := []*schema.Table{}
423+
for _, t := range includes {
424+
ts, _, err := t.CollectTablesAndRelations(c.Distance, true)
425+
if err != nil {
426+
return err
427+
}
428+
for _, tt := range ts {
429+
if !tt.Contains(includes) {
430+
collects = append(collects, tt)
398431
}
399432
}
400433
}
434+
435+
for _, t := range excludes {
436+
if t.Contains(collects) {
437+
continue
438+
}
439+
err := excludeTableFromSchema(t.Name, s)
440+
if err != nil {
441+
return errors.Wrap(errors.WithStack(err), fmt.Sprintf("failed to filter table '%s'", t.Name))
442+
}
443+
}
444+
401445
return nil
402446
}
403447

0 commit comments

Comments
 (0)