Skip to content

Commit

Permalink
Merge 6dd2cf1 into f676c3a
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Jan 5, 2023
2 parents f676c3a + 6dd2cf1 commit d5b8e04
Show file tree
Hide file tree
Showing 10 changed files with 529 additions and 161 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ tbls.exe
dist/
dbdoc/
coverage.out
testdb.sqlite3
*.sqlite3
.envrc
.go-version
.tbls.yml
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ test_ext_subcommand: build
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
echo hello | env PATH="${PWD}/testdata/bin:${PATH}" $(TBLS) echo -c ./testdata/ext_subcommand_tbls.yml | grep 'STDIN=hello' > /dev/null

generate_test_json: build
sqlite3 $(PWD)/filter_tables.sqlite3 < testdata/ddl/filter_tables.sql
$(TBLS) out sq://$(PWD)/filter_tables.sqlite3 -t json > testdata/filter_tables.json

lint:
golangci-lint run ./...

Expand Down
3 changes: 3 additions & 0 deletions cmd/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ func loadDocArgs(args []string) ([]config.Option, error) {
options = append(options, config.ERSkip(withoutER))
}
options = append(options, config.BaseUrl(baseUrl))
options = append(options, config.Include(includes))
options = append(options, config.Exclude(excludes))
if len(args) == 2 {
options = append(options, config.DSNURL(args[0]))
options = append(options, config.DocPath(args[1]))
Expand Down Expand Up @@ -227,6 +229,7 @@ func init() {
docCmd.Flags().StringVarP(&when, "when", "", "", "command execute condition")
docCmd.Flags().StringVarP(&baseUrl, "base-url", "b", "", "base url for links")
docCmd.Flags().BoolVarP(&rmDist, "rm-dist", "", false, "remove files in docPath before generating documents")

if err := docCmd.MarkZshCompPositionalArgumentFile(2); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
Expand Down
29 changes: 12 additions & 17 deletions cmd/out.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ import (
)

var (
format string
outPath string
tableName string
distance int
format string
outPath string
tables []string
distance int
)

// outCmd represents the doc command
Expand Down Expand Up @@ -125,17 +125,7 @@ var outCmd = &cobra.Command{
wr = os.Stdout
}

if tableName == "" {
err = o.OutputSchema(wr, s)
} else {
t, ferr := s.FindTableByName(tableName)
if ferr != nil {
return err
}
err = o.OutputTable(wr, t)
}

if err != nil {
if err := o.OutputSchema(wr, s); err != nil {
return err
}

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

options = append(options, config.Include(append(tables, includes...)))
options = append(options, config.Exclude(excludes))

if len(args) == 1 {
options = append(options, config.DSNURL(args[0]))
}
Expand All @@ -165,7 +158,9 @@ func init() {
outCmd.Flags().StringVarP(&configPath, "config", "c", "", "config file path")
outCmd.Flags().StringVarP(&format, "format", "t", "json", "output format")
outCmd.Flags().StringVarP(&outPath, "out", "o", "", "output file path")
outCmd.Flags().StringVar(&tableName, "table", "", "table name")
outCmd.Flags().IntVarP(&distance, "distance", "", config.DefaultDistance, "distance between tables that display associations in the ER")
outCmd.Flags().StringSliceVarP(&tables, "table", "", []string{}, "target table")
outCmd.Flags().StringSliceVarP(&includes, "include", "", []string{}, "tables to include")
outCmd.Flags().StringSliceVarP(&excludes, "exclude", "", []string{}, "tables to exclude")
outCmd.Flags().IntVarP(&distance, "distance", "", 0, "distance between related tables to be displayed")
outCmd.Flags().StringVarP(&when, "when", "", "", "command execute condition")
}
7 changes: 7 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,15 @@ var erFormat string
// when is a option that command execute condition
var when string

// base url for links
var baseUrl string

// tables to include
var includes []string

// tables to excludes
var excludes []string

const rootUsageTemplate = `Usage:{{if .Runnable}}{{if ne .UseLine "tbls [flags]" }}
{{.UseLine}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
Expand Down
76 changes: 60 additions & 16 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ const DefaultERFormat = "svg"

const SchemaFileName = "schema.json"

// DefaultDistance is the default distance between tables that display relations in the ER
var DefaultDistance = 1
// DefaultERDistance is the default distance between tables that display relations in the ER
var DefaultERDistance = 1

// Config is tbls config
type Config struct {
Expand All @@ -41,6 +41,7 @@ type Config struct {
ER ER `yaml:"er,omitempty"`
Include []string `yaml:"include,omitempty"`
Exclude []string `yaml:"exclude,omitempty"`
Distance int `yaml:"distance,omitempty"`
Lint Lint `yaml:"lint,omitempty"`
LintExclude []string `yaml:"lintExclude,omitempty"`
Relations []AdditionalRelation `yaml:"relations,omitempty"`
Expand Down Expand Up @@ -163,10 +164,10 @@ func ERFormat(erFormat string) Option {
}
}

// Distance return Option set Config.ER.Distance
// Distance return Option set Config.Distance
func Distance(distance int) Option {
return func(c *Config) error {
c.ER.Distance = &distance
c.Distance = distance
return nil
}
}
Expand All @@ -181,6 +182,26 @@ func BaseUrl(baseUrl string) Option {
}
}

// Include return Option set Config.Include
func Include(i []string) Option {
return func(c *Config) error {
if len(i) > 0 {
c.Include = i
}
return nil
}
}

// Exclude return Option set Config.Exclude
func Exclude(e []string) Option {
return func(c *Config) error {
if len(e) > 0 {
c.Exclude = e
}
return nil
}
}

// New return Config
func New() (*Config, error) {
c := Config{}
Expand Down Expand Up @@ -237,7 +258,7 @@ func (c *Config) setDefault() error {
}

if c.ER.Distance == nil {
c.ER.Distance = &DefaultDistance
c.ER.Distance = &DefaultERDistance
}

return nil
Expand Down Expand Up @@ -373,31 +394,54 @@ func (c *Config) MergeAdditionalData(s *schema.Schema) error {
func (c *Config) FilterTables(s *schema.Schema) error {
i := append(c.Include, s.NormalizeTableNames(c.Include)...)
e := append(c.Exclude, s.NormalizeTableNames(c.Exclude)...)

includes := []*schema.Table{}
excludes := []*schema.Table{}
for _, t := range s.Tables {
li, mi := matchLength(i, t.Name)
le, me := matchLength(e, t.Name)
switch {
case len(c.Include) == 0:
if me {
err := excludeTableFromSchema(t.Name, s)
if err != nil {
return errors.Wrap(errors.WithStack(err), fmt.Sprintf("failed to filter table '%s'", t.Name))
}
excludes = append(excludes, t)
continue
}
includes = append(includes, t)
case mi:
if me && li < le {
err := excludeTableFromSchema(t.Name, s)
if err != nil {
return errors.Wrap(errors.WithStack(err), fmt.Sprintf("failed to filter table '%s'", t.Name))
}
excludes = append(excludes, t)
continue
}
includes = append(includes, t)
default:
err := excludeTableFromSchema(t.Name, s)
if err != nil {
return errors.Wrap(errors.WithStack(err), fmt.Sprintf("failed to filter table '%s'", t.Name))
// c.Include > 0 && !mi
excludes = append(excludes, t)
}
}

collects := []*schema.Table{}
for _, t := range includes {
ts, _, err := t.CollectTablesAndRelations(c.Distance, true)
if err != nil {
return err
}
for _, tt := range ts {
if !tt.Contains(includes) {
collects = append(collects, tt)
}
}
}

for _, t := range excludes {
if t.Contains(collects) {
continue
}
err := excludeTableFromSchema(t.Name, s)
if err != nil {
return errors.Wrap(errors.WithStack(err), fmt.Sprintf("failed to filter table '%s'", t.Name))
}
}

return nil
}

Expand Down
Loading

0 comments on commit d5b8e04

Please sign in to comment.