From b2a8a98a7f32a510d69ad95931db42e0633a4f31 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Sun, 11 Aug 2024 10:11:47 +0900 Subject: [PATCH 1/2] Use github.com/k1LoW/errors --- cmd/completion.go | 2 +- cmd/coverage.go | 2 +- cmd/doc.go | 2 +- cmd/lint.go | 2 +- cmd/out.go | 2 +- cmd/root.go | 3 ++- cmdutil/when.go | 2 +- config/config.go | 2 +- datasource/datasource.go | 2 +- drivers/bq/bq.go | 2 +- drivers/clickhouse/clickhouse.go | 2 +- drivers/clickhouse/clickhouse_test.go | 5 +++-- drivers/dynamo/dynamo.go | 2 +- drivers/mongodb/mongodb.go | 2 +- drivers/mssql/mssql.go | 2 +- drivers/mysql/mysql.go | 2 +- drivers/postgres/postgres.go | 2 +- drivers/snowflake/snowflake.go | 2 +- drivers/spanner/spanner.go | 2 +- drivers/sqlite/sqlite.go | 2 +- go.mod | 3 ++- go.sum | 2 ++ output/config/config.go | 2 +- output/dot/dot.go | 2 +- output/gviz/gviz.go | 2 +- output/md/md.go | 2 +- output/mermaid/mermaid.go | 2 +- output/plantuml/plantuml.go | 2 +- output/xlsx/xlsx.go | 2 +- schema/filter.go | 2 +- schema/schema.go | 2 +- 31 files changed, 36 insertions(+), 31 deletions(-) diff --git a/cmd/completion.go b/cmd/completion.go index 62428919f..edf59d14e 100644 --- a/cmd/completion.go +++ b/cmd/completion.go @@ -26,7 +26,7 @@ import ( "os" "path/filepath" - "github.com/pkg/errors" + "github.com/k1LoW/errors" "github.com/spf13/cobra" ) diff --git a/cmd/coverage.go b/cmd/coverage.go index 5ae7c93dd..478fcd611 100644 --- a/cmd/coverage.go +++ b/cmd/coverage.go @@ -26,12 +26,12 @@ import ( "fmt" "os" + "github.com/k1LoW/errors" "github.com/k1LoW/tbls/cmdutil" "github.com/k1LoW/tbls/config" "github.com/k1LoW/tbls/coverage" "github.com/labstack/gommon/color" "github.com/mattn/go-runewidth" - "github.com/pkg/errors" "github.com/spf13/cobra" ) diff --git a/cmd/doc.go b/cmd/doc.go index 7e0c4c48e..b15078cfa 100644 --- a/cmd/doc.go +++ b/cmd/doc.go @@ -26,6 +26,7 @@ import ( "path/filepath" "strings" + "github.com/k1LoW/errors" "github.com/k1LoW/tbls/cmdutil" "github.com/k1LoW/tbls/config" "github.com/k1LoW/tbls/datasource" @@ -33,7 +34,6 @@ import ( "github.com/k1LoW/tbls/output/json" "github.com/k1LoW/tbls/output/md" "github.com/k1LoW/tbls/schema" - "github.com/pkg/errors" "github.com/spf13/cobra" ) diff --git a/cmd/lint.go b/cmd/lint.go index e15b5c9e7..7c87b774d 100644 --- a/cmd/lint.go +++ b/cmd/lint.go @@ -25,11 +25,11 @@ import ( "os" "reflect" + "github.com/k1LoW/errors" "github.com/k1LoW/tbls/cmdutil" "github.com/k1LoW/tbls/config" "github.com/k1LoW/tbls/datasource" "github.com/labstack/gommon/color" - "github.com/pkg/errors" "github.com/spf13/cobra" ) diff --git a/cmd/out.go b/cmd/out.go index 2cba1c8ab..4fdfa5211 100644 --- a/cmd/out.go +++ b/cmd/out.go @@ -24,6 +24,7 @@ import ( "io" "os" + "github.com/k1LoW/errors" "github.com/k1LoW/tbls/cmdutil" "github.com/k1LoW/tbls/config" "github.com/k1LoW/tbls/output" @@ -36,7 +37,6 @@ import ( "github.com/k1LoW/tbls/output/plantuml" "github.com/k1LoW/tbls/output/xlsx" "github.com/k1LoW/tbls/output/yaml" - "github.com/pkg/errors" "github.com/spf13/cobra" ) diff --git a/cmd/root.go b/cmd/root.go index 7e7a626c1..b472d2996 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -29,6 +29,7 @@ import ( "strconv" "strings" + "github.com/k1LoW/errors" "github.com/k1LoW/tbls/cmdutil" "github.com/k1LoW/tbls/config" "github.com/k1LoW/tbls/output/json" @@ -271,7 +272,7 @@ func printError(err error) { env := os.Getenv("DEBUG") debug, _ := strconv.ParseBool(env) if env != "" && debug { - fmt.Printf("%+v\n", err) + fmt.Println(errors.StackTraces(err)) } else { fmt.Println(err) } diff --git a/cmdutil/when.go b/cmdutil/when.go index a79a1e9d1..a35db3af2 100644 --- a/cmdutil/when.go +++ b/cmdutil/when.go @@ -6,7 +6,7 @@ import ( "github.com/expr-lang/expr" "github.com/expr-lang/expr/ast" - "github.com/pkg/errors" + "github.com/k1LoW/errors" ) // AST walker which replaces `$IDENTIFIER` with `Env.IDENTIFIER` member lookup expressions. diff --git a/config/config.go b/config/config.go index 21361f9c8..2b1cdf486 100644 --- a/config/config.go +++ b/config/config.go @@ -9,12 +9,12 @@ import ( "github.com/aquasecurity/go-version/pkg/version" "github.com/goccy/go-yaml" + "github.com/k1LoW/errors" "github.com/k1LoW/expand" "github.com/k1LoW/tbls/dict" "github.com/k1LoW/tbls/schema" ver "github.com/k1LoW/tbls/version" "github.com/minio/pkg/wildcard" - "github.com/pkg/errors" "github.com/samber/lo" ) diff --git a/datasource/datasource.go b/datasource/datasource.go index 4256ad092..b953ca168 100644 --- a/datasource/datasource.go +++ b/datasource/datasource.go @@ -10,6 +10,7 @@ import ( "strings" "time" + "github.com/k1LoW/errors" "github.com/k1LoW/ghfs" "github.com/k1LoW/go-github-client/v58/factory" "github.com/k1LoW/tbls/config" @@ -23,7 +24,6 @@ import ( "github.com/k1LoW/tbls/drivers/snowflake" "github.com/k1LoW/tbls/drivers/sqlite" "github.com/k1LoW/tbls/schema" - "github.com/pkg/errors" "github.com/xo/dburl" ) diff --git a/drivers/bq/bq.go b/drivers/bq/bq.go index 99df2e981..21f0cf091 100644 --- a/drivers/bq/bq.go +++ b/drivers/bq/bq.go @@ -7,10 +7,10 @@ import ( "strings" "cloud.google.com/go/bigquery" + "github.com/k1LoW/errors" "github.com/k1LoW/tbls/ddl" "github.com/k1LoW/tbls/dict" "github.com/k1LoW/tbls/schema" - "github.com/pkg/errors" ) // Bigquery struct diff --git a/drivers/clickhouse/clickhouse.go b/drivers/clickhouse/clickhouse.go index 64325d473..c6721230d 100644 --- a/drivers/clickhouse/clickhouse.go +++ b/drivers/clickhouse/clickhouse.go @@ -6,9 +6,9 @@ import ( "regexp" _ "github.com/ClickHouse/clickhouse-go/v2" + "github.com/k1LoW/errors" "github.com/k1LoW/tbls/dict" "github.com/k1LoW/tbls/schema" - "github.com/pkg/errors" "github.com/samber/lo" ) diff --git a/drivers/clickhouse/clickhouse_test.go b/drivers/clickhouse/clickhouse_test.go index e8f1890ca..e4636ddbb 100644 --- a/drivers/clickhouse/clickhouse_test.go +++ b/drivers/clickhouse/clickhouse_test.go @@ -5,10 +5,11 @@ package clickhouse import ( "database/sql" "fmt" - "github.com/k1LoW/tbls/schema" - "github.com/xo/dburl" "os" "testing" + + "github.com/k1LoW/tbls/schema" + "github.com/xo/dburl" ) var db *sql.DB diff --git a/drivers/dynamo/dynamo.go b/drivers/dynamo/dynamo.go index c7ef81791..3baf916c7 100644 --- a/drivers/dynamo/dynamo.go +++ b/drivers/dynamo/dynamo.go @@ -6,9 +6,9 @@ import ( "regexp" "github.com/aws/aws-sdk-go/service/dynamodb" + "github.com/k1LoW/errors" "github.com/k1LoW/tbls/dict" "github.com/k1LoW/tbls/schema" - "github.com/pkg/errors" ) var re = regexp.MustCompile(`(?s)\n\s*`) diff --git a/drivers/mongodb/mongodb.go b/drivers/mongodb/mongodb.go index 17b2745fa..801960167 100644 --- a/drivers/mongodb/mongodb.go +++ b/drivers/mongodb/mongodb.go @@ -8,9 +8,9 @@ import ( "sort" "strings" + "github.com/k1LoW/errors" "github.com/k1LoW/tbls/dict" "github.com/k1LoW/tbls/schema" - "github.com/pkg/errors" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" diff --git a/drivers/mssql/mssql.go b/drivers/mssql/mssql.go index 0a72574cd..9fd5820c4 100644 --- a/drivers/mssql/mssql.go +++ b/drivers/mssql/mssql.go @@ -7,10 +7,10 @@ import ( "strconv" "strings" + "github.com/k1LoW/errors" "github.com/k1LoW/tbls/ddl" "github.com/k1LoW/tbls/dict" "github.com/k1LoW/tbls/schema" - "github.com/pkg/errors" ) var defaultSchemaName = "dbo" diff --git a/drivers/mysql/mysql.go b/drivers/mysql/mysql.go index b28518355..10a044906 100644 --- a/drivers/mysql/mysql.go +++ b/drivers/mysql/mysql.go @@ -8,11 +8,11 @@ import ( "strings" "github.com/aquasecurity/go-version/pkg/version" + "github.com/k1LoW/errors" "github.com/k1LoW/tbls/ddl" "github.com/k1LoW/tbls/dict" "github.com/k1LoW/tbls/drivers" "github.com/k1LoW/tbls/schema" - "github.com/pkg/errors" ) var reFK = regexp.MustCompile(`FOREIGN KEY \((.+)\) REFERENCES ([^\s\)]+)\s?\(([^\)]+)\)`) diff --git a/drivers/postgres/postgres.go b/drivers/postgres/postgres.go index d602e8e27..404c949c5 100644 --- a/drivers/postgres/postgres.go +++ b/drivers/postgres/postgres.go @@ -7,11 +7,11 @@ import ( "strings" "github.com/aquasecurity/go-version/pkg/version" + "github.com/k1LoW/errors" "github.com/k1LoW/tbls/ddl" "github.com/k1LoW/tbls/dict" "github.com/k1LoW/tbls/schema" "github.com/lib/pq" - "github.com/pkg/errors" ) var reFK = regexp.MustCompile(`FOREIGN KEY \((.+)\) REFERENCES ([^\s\)]+)\s?\(([^\)]+)\)`) diff --git a/drivers/snowflake/snowflake.go b/drivers/snowflake/snowflake.go index eba8bcd00..13f7689c0 100644 --- a/drivers/snowflake/snowflake.go +++ b/drivers/snowflake/snowflake.go @@ -3,8 +3,8 @@ package snowflake import ( "database/sql" + "github.com/k1LoW/errors" "github.com/k1LoW/tbls/schema" - "github.com/pkg/errors" _ "github.com/snowflakedb/gosnowflake" ) diff --git a/drivers/spanner/spanner.go b/drivers/spanner/spanner.go index b11549539..64fed2aaf 100644 --- a/drivers/spanner/spanner.go +++ b/drivers/spanner/spanner.go @@ -6,8 +6,8 @@ import ( "strings" "cloud.google.com/go/spanner" + "github.com/k1LoW/errors" "github.com/k1LoW/tbls/schema" - "github.com/pkg/errors" "google.golang.org/api/iterator" ) diff --git a/drivers/sqlite/sqlite.go b/drivers/sqlite/sqlite.go index ede5a48ce..43fe7c97b 100644 --- a/drivers/sqlite/sqlite.go +++ b/drivers/sqlite/sqlite.go @@ -8,9 +8,9 @@ import ( "regexp" + "github.com/k1LoW/errors" "github.com/k1LoW/tbls/ddl" "github.com/k1LoW/tbls/schema" - "github.com/pkg/errors" "github.com/samber/lo" ) diff --git a/go.mod b/go.mod index 167b1a440..4537d505f 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/goccy/go-yaml v1.12.0 github.com/google/go-cmp v0.6.0 github.com/k1LoW/duration v1.2.0 + github.com/k1LoW/errors v0.2.1 github.com/k1LoW/expand v0.12.0 github.com/k1LoW/ffff v0.2.0 github.com/k1LoW/ghfs v1.3.1 @@ -28,7 +29,6 @@ require ( github.com/microsoft/go-mssqldb v1.7.2 github.com/minio/pkg v1.7.5 github.com/olekukonko/tablewriter v0.0.5 - github.com/pkg/errors v0.9.1 github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 github.com/samber/lo v1.46.0 github.com/snowflakedb/gosnowflake v1.10.1 @@ -124,6 +124,7 @@ require ( github.com/paulmach/orb v0.11.1 // indirect github.com/pierrec/lz4/v4 v4.1.21 // indirect github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/rivo/uniseg v0.4.4 // indirect github.com/sahilm/fuzzy v0.1.0 // indirect github.com/segmentio/asm v1.2.0 // indirect diff --git a/go.sum b/go.sum index 0339a05e8..daa61a643 100644 --- a/go.sum +++ b/go.sum @@ -992,6 +992,8 @@ github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+ github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes= github.com/k1LoW/duration v1.2.0 h1:qq1gWtPh7YROFyerBufVP+ATR11mOOHDInrcC/Xe/6A= github.com/k1LoW/duration v1.2.0/go.mod h1:qUa0NptIiUl5EUsCc8wIiSaHuNjS4wmpYNMHp0l6pos= +github.com/k1LoW/errors v0.2.1 h1:HHdnTugYfmZL4QsbVhcYNlQxVC1ExsEp7q7Npdxvqoc= +github.com/k1LoW/errors v0.2.1/go.mod h1:sNlL6+/dTai2f2R7/vcDMMRlrd/C7kXONQT81b2kZPY= github.com/k1LoW/expand v0.12.0 h1:EfkT/VoGVe/B6Jo8WYS5dliXOPJ0xkdkGfzOQPl+0Y8= github.com/k1LoW/expand v0.12.0/go.mod h1:jJkWgMSibfX88Q+4m4Jse6FQP62M4ia2YaQWQKmD4nA= github.com/k1LoW/ffff v0.2.0 h1:k5IYb2ggSKrkgVRkFnn1fs4xVUkJxanTH5gInmEKTDA= diff --git a/output/config/config.go b/output/config/config.go index d94bd6c47..0b0ba0d34 100644 --- a/output/config/config.go +++ b/output/config/config.go @@ -5,9 +5,9 @@ import ( "io" "github.com/goccy/go-yaml" + "github.com/k1LoW/errors" "github.com/k1LoW/tbls/config" "github.com/k1LoW/tbls/schema" - "github.com/pkg/errors" ) const NoTableComment = "table comment required." diff --git a/output/dot/dot.go b/output/dot/dot.go index bebe5b58b..818d68b41 100644 --- a/output/dot/dot.go +++ b/output/dot/dot.go @@ -6,10 +6,10 @@ import ( "os" "text/template" + "github.com/k1LoW/errors" "github.com/k1LoW/tbls/config" "github.com/k1LoW/tbls/output" "github.com/k1LoW/tbls/schema" - "github.com/pkg/errors" "github.com/samber/lo" ) diff --git a/output/gviz/gviz.go b/output/gviz/gviz.go index eff8996e0..0f4b0ae2c 100644 --- a/output/gviz/gviz.go +++ b/output/gviz/gviz.go @@ -10,11 +10,11 @@ import ( "github.com/beta/freetype/truetype" "github.com/goccy/go-graphviz" + "github.com/k1LoW/errors" "github.com/k1LoW/ffff" "github.com/k1LoW/tbls/config" "github.com/k1LoW/tbls/output/dot" "github.com/k1LoW/tbls/schema" - "github.com/pkg/errors" "golang.org/x/image/font" "golang.org/x/image/font/opentype" "golang.org/x/image/font/sfnt" diff --git a/output/md/md.go b/output/md/md.go index c362f52bf..fc3ebf550 100644 --- a/output/md/md.go +++ b/output/md/md.go @@ -11,12 +11,12 @@ import ( "strings" "text/template" + "github.com/k1LoW/errors" "github.com/k1LoW/tbls/config" "github.com/k1LoW/tbls/output" "github.com/k1LoW/tbls/output/mermaid" "github.com/k1LoW/tbls/schema" "github.com/mattn/go-runewidth" - "github.com/pkg/errors" "github.com/pmezard/go-difflib/difflib" "github.com/samber/lo" "gitlab.com/golang-commonmark/mdurl" diff --git a/output/mermaid/mermaid.go b/output/mermaid/mermaid.go index 818b8302d..c56620010 100644 --- a/output/mermaid/mermaid.go +++ b/output/mermaid/mermaid.go @@ -6,10 +6,10 @@ import ( "os" "text/template" + "github.com/k1LoW/errors" "github.com/k1LoW/tbls/config" "github.com/k1LoW/tbls/output" "github.com/k1LoW/tbls/schema" - "github.com/pkg/errors" ) //go:embed templates/* diff --git a/output/plantuml/plantuml.go b/output/plantuml/plantuml.go index 43594c3ad..08d354877 100644 --- a/output/plantuml/plantuml.go +++ b/output/plantuml/plantuml.go @@ -6,10 +6,10 @@ import ( "os" "text/template" + "github.com/k1LoW/errors" "github.com/k1LoW/tbls/config" "github.com/k1LoW/tbls/output" "github.com/k1LoW/tbls/schema" - "github.com/pkg/errors" ) //go:embed templates/* diff --git a/output/xlsx/xlsx.go b/output/xlsx/xlsx.go index 5514c8e23..cb63e8c43 100644 --- a/output/xlsx/xlsx.go +++ b/output/xlsx/xlsx.go @@ -8,10 +8,10 @@ import ( "strings" "unicode/utf8" + "github.com/k1LoW/errors" "github.com/k1LoW/tbls/config" "github.com/k1LoW/tbls/schema" "github.com/loadoff/excl" - "github.com/pkg/errors" ) // Xlsx struct diff --git a/schema/filter.go b/schema/filter.go index 72a68f492..f08dd1365 100644 --- a/schema/filter.go +++ b/schema/filter.go @@ -4,8 +4,8 @@ import ( "fmt" "strings" + "github.com/k1LoW/errors" "github.com/minio/pkg/wildcard" - "github.com/pkg/errors" "github.com/samber/lo" ) diff --git a/schema/schema.go b/schema/schema.go index e435881b1..0bf8dd5b0 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -7,8 +7,8 @@ import ( "sort" "strings" + "github.com/k1LoW/errors" "github.com/k1LoW/tbls/dict" - "github.com/pkg/errors" "github.com/samber/lo" ) From ed003a9a5f556d99f539cc26ecafc4c94100be30 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Sun, 11 Aug 2024 13:45:17 +0900 Subject: [PATCH 2/2] Fix --- cmd/completion.go | 2 +- cmd/out.go | 3 +- cmd/root.go | 2 +- config/config.go | 59 +++++++++++++++++++-------------- datasource/datasource.go | 62 +++++++++++++++++++++------------- drivers/mysql/mysql.go | 7 ++-- drivers/postgres/postgres.go | 28 +++++++++++----- drivers/sqlite/sqlite.go | 14 +++++--- schema/filter.go | 14 +++++--- schema/schema.go | 64 +++++++++++++++++++++++++----------- 10 files changed, 167 insertions(+), 88 deletions(-) diff --git a/cmd/completion.go b/cmd/completion.go index edf59d14e..e9a0bc429 100644 --- a/cmd/completion.go +++ b/cmd/completion.go @@ -51,7 +51,7 @@ tbls completion fish ~/.config/fish/completions/tbls.fish ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, Args: func(cmd *cobra.Command, args []string) error { if len(args) != 1 { - return errors.Errorf("accepts 1 arg, received %d", len(args)) + return fmt.Errorf("accepts 1 arg, received %d", len(args)) } if err := cobra.OnlyValidArgs(cmd, args); err != nil { return err diff --git a/cmd/out.go b/cmd/out.go index 4fdfa5211..224bcec09 100644 --- a/cmd/out.go +++ b/cmd/out.go @@ -21,6 +21,7 @@ package cmd import ( + "fmt" "io" "os" @@ -102,7 +103,7 @@ var outCmd = &cobra.Command{ case "config": o = tbls_config.New(c) default: - return errors.Errorf("unsupported format '%s'", format) + return fmt.Errorf("unsupported format '%s'", format) } var wr io.Writer diff --git a/cmd/root.go b/cmd/root.go index b472d2996..a63741747 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -272,7 +272,7 @@ func printError(err error) { env := os.Getenv("DEBUG") debug, _ := strconv.ParseBool(env) if env != "" && debug { - fmt.Println(errors.StackTraces(err)) + fmt.Println(err, errors.StackTraces(err)) } else { fmt.Println(err) } diff --git a/config/config.go b/config/config.go index 2b1cdf486..e935559e9 100644 --- a/config/config.go +++ b/config/config.go @@ -356,12 +356,15 @@ func (c *Config) LoadEnviron() error { } // LoadConfigFile load config file -func (c *Config) LoadConfigFile(path string) error { +func (c *Config) LoadConfigFile(path string) (err error) { + defer func() { + err = errors.WithStack(err) + }() if path == "" && os.Getenv("TBLS_DSN") == "" { for _, p := range DefaultConfigFilePaths { if f, err := os.Stat(filepath.Join(c.root, p)); err == nil && !f.IsDir() { if path != "" { - return errors.Errorf("duplicate config file [%s, %s]", path, p) + return fmt.Errorf("duplicate config file [%s, %s]", path, p) } path = p } @@ -373,12 +376,12 @@ func (c *Config) LoadConfigFile(path string) error { fullPath, err := filepath.Abs(path) if err != nil { - return errors.Wrap(errors.WithStack(err), "failed to load config file") + return fmt.Errorf("failed to load config file: %w", err) } buf, err := os.ReadFile(filepath.Clean(fullPath)) if err != nil { - return errors.Wrap(errors.WithStack(err), "failed to load config file") + return fmt.Errorf("failed to load config file: %w", err) } c.Path = filepath.Clean(fullPath) @@ -386,10 +389,12 @@ func (c *Config) LoadConfigFile(path string) error { } // LoadConfig load config from []byte -func (c *Config) LoadConfig(in []byte) error { - err := yaml.Unmarshal(expand.ExpandenvYAMLBytes(in), c) - if err != nil { - return errors.Wrap(errors.WithStack(err), "failed to load config file") +func (c *Config) LoadConfig(in []byte) (err error) { + defer func() { + err = errors.WithStack(err) + }() + if err := yaml.Unmarshal(expand.ExpandenvYAMLBytes(in), c); err != nil { + return fmt.Errorf("failed to load config file: %w", err) } c.MergedDict.Merge(c.Dict.Dump()) return nil @@ -611,15 +616,18 @@ func (c *Config) detectShowColumnsForER(s *schema.Schema) error { return nil } -func mergeAdditionalRelations(s *schema.Schema, relations []AdditionalRelation) error { +func mergeAdditionalRelations(s *schema.Schema, relations []AdditionalRelation) (err error) { + defer func() { + err = errors.WithStack(err) + }() for _, r := range relations { c, err := schema.ToCardinality(r.Cardinality) if err != nil { - return errors.Wrap(err, "failed to add relation") + return fmt.Errorf("failed to add relation: %w", err) } pc, err := schema.ToCardinality(r.ParentCardinality) if err != nil { - return errors.Wrap(err, "failed to add relation") + return fmt.Errorf("failed to add relation: %w", err) } relation := &schema.Relation{ Cardinality: c, @@ -633,24 +641,24 @@ func mergeAdditionalRelations(s *schema.Schema, relations []AdditionalRelation) } relation.Table, err = s.FindTableByName(r.Table) if err != nil { - return errors.Wrap(err, "failed to add relation") + return fmt.Errorf("failed to add relation: %w", err) } for _, c := range r.Columns { column, err := relation.Table.FindColumnByName(c) if err != nil { - return errors.Wrap(err, "failed to add relation") + return fmt.Errorf("failed to add relation: %w", err) } relation.Columns = append(relation.Columns, column) column.ParentRelations = append(column.ParentRelations, relation) } relation.ParentTable, err = s.FindTableByName(r.ParentTable) if err != nil { - return errors.Wrap(err, "failed to add relation") + return fmt.Errorf("failed to add relation: %w", err) } for _, c := range r.ParentColumns { column, err := relation.ParentTable.FindColumnByName(c) if err != nil { - return errors.Wrap(err, "failed to add relation") + return fmt.Errorf("failed to add relation: %w", err) } relation.ParentColumns = append(relation.ParentColumns, column) column.ChildRelations = append(column.ChildRelations, relation) @@ -665,11 +673,11 @@ func mergeAdditionalRelations(s *schema.Schema, relations []AdditionalRelation) cr.Def = r.Def cr.Cardinality, err = schema.ToCardinality(r.Cardinality) if err != nil { - return errors.Wrap(err, "failed to add relation") + return fmt.Errorf("failed to add relation: %w", err) } cr.ParentCardinality, err = schema.ToCardinality(r.ParentCardinality) if err != nil { - return errors.Wrap(err, "failed to add relation") + return fmt.Errorf("failed to add relation: %w", err) } } } else { @@ -679,11 +687,14 @@ func mergeAdditionalRelations(s *schema.Schema, relations []AdditionalRelation) return nil } -func mergeAdditionalComments(s *schema.Schema, comments []AdditionalComment) error { +func mergeAdditionalComments(s *schema.Schema, comments []AdditionalComment) (err error) { + defer func() { + err = errors.WithStack(err) + }() for _, c := range comments { table, err := s.FindTableByName(c.Table) if err != nil { - return errors.Wrap(err, "failed to add table comment") + return fmt.Errorf("failed to add table comment: %w", err) } if c.TableComment != "" { table.Comment = c.TableComment @@ -696,14 +707,14 @@ func mergeAdditionalComments(s *schema.Schema, comments []AdditionalComment) err for c, comment := range c.ColumnComments { column, err := table.FindColumnByName(c) if err != nil { - return errors.Wrap(err, "failed to add column comment") + return fmt.Errorf("failed to add column comment: %w", err) } column.Comment = comment } for c, labels := range c.ColumnLabels { column, err := table.FindColumnByName(c) if err != nil { - return errors.Wrap(err, "failed to add column comment") + return fmt.Errorf("failed to add column comment: %w", err) } for _, l := range labels { column.Labels = column.Labels.Merge(l) @@ -712,21 +723,21 @@ func mergeAdditionalComments(s *schema.Schema, comments []AdditionalComment) err for i, comment := range c.IndexComments { index, err := table.FindIndexByName(i) if err != nil { - return errors.Wrap(err, "failed to add index comment") + return fmt.Errorf("failed to add index comment: %w", err) } index.Comment = comment } for c, comment := range c.ConstraintComments { constraint, err := table.FindConstraintByName(c) if err != nil { - return errors.Wrap(err, "failed to add constraint comment") + return fmt.Errorf("failed to add constraint comment: %w", err) } constraint.Comment = comment } for t, comment := range c.TriggerComments { trigger, err := table.FindTriggerByName(t) if err != nil { - return errors.Wrap(err, "failed to add trigger comment") + return fmt.Errorf("failed to add trigger comment: %w", err) } trigger.Comment = comment } diff --git a/datasource/datasource.go b/datasource/datasource.go index b953ca168..6342fdeed 100644 --- a/datasource/datasource.go +++ b/datasource/datasource.go @@ -3,6 +3,7 @@ package datasource import ( "bytes" "encoding/json" + "fmt" "io" "net/http" "os" @@ -28,7 +29,10 @@ import ( ) // Analyze database -func Analyze(dsn config.DSN) (*schema.Schema, error) { +func Analyze(dsn config.DSN) (_ *schema.Schema, err error) { + defer func() { + err = errors.WithStack(err) + }() urlstr := dsn.URL if strings.HasPrefix(urlstr, "https://") || strings.HasPrefix(urlstr, "http://") { return AnalyzeHTTPResource(dsn) @@ -54,11 +58,11 @@ func Analyze(dsn config.DSN) (*schema.Schema, error) { s := &schema.Schema{} u, err := dburl.Parse(urlstr) if err != nil { - return s, errors.WithStack(err) + return s, err } splitted := strings.Split(u.Short(), "/") if len(splitted) < 2 { - return s, errors.Errorf("invalid DSN: parse %s -> %#v", urlstr, u) + return s, fmt.Errorf("invalid DSN: parse %s -> %#v", urlstr, u) } opts := []drivers.Option{} @@ -129,7 +133,7 @@ func Analyze(dsn config.DSN) (*schema.Schema, error) { s.Name = splitted[1] driver = clickhouse.New(db) default: - return s, errors.Errorf("unsupported driver '%s'", u.Driver) + return s, fmt.Errorf("unsupported driver '%s'", u.Driver) } err = driver.Analyze(s) if err != nil { @@ -139,11 +143,14 @@ func Analyze(dsn config.DSN) (*schema.Schema, error) { } // AnalyzeHTTPResource analyze `https://` or `http://` -func AnalyzeHTTPResource(dsn config.DSN) (*schema.Schema, error) { +func AnalyzeHTTPResource(dsn config.DSN) (_ *schema.Schema, err error) { + defer func() { + err = errors.WithStack(err) + }() s := &schema.Schema{} req, err := http.NewRequest("GET", dsn.URL, nil) if err != nil { - return s, errors.WithStack(err) + return s, err } for k, v := range dsn.Headers { req.Header.Add(k, v) @@ -151,64 +158,70 @@ func AnalyzeHTTPResource(dsn config.DSN) (*schema.Schema, error) { client := &http.Client{Timeout: time.Duration(10) * time.Second} resp, err := client.Do(req) if err != nil { - return s, errors.WithStack(err) + return s, err } defer resp.Body.Close() dec := json.NewDecoder(resp.Body) if err := dec.Decode(s); err != nil { - return s, errors.WithStack(err) + return s, err } if err := s.Repair(); err != nil { - return s, errors.WithStack(err) + return s, err } return s, nil } // AnalyzeGitHubContent analyze `github://` -func AnalyzeGitHubContent(dsn config.DSN) (*schema.Schema, error) { +func AnalyzeGitHubContent(dsn config.DSN) (_ *schema.Schema, err error) { + defer func() { + err = errors.WithStack(err) + }() splitted := strings.SplitN(strings.TrimPrefix(dsn.URL, "github://"), "/", 3) if len(splitted) != 3 { - return nil, errors.Errorf("invalid dsn: %s", dsn) + return nil, fmt.Errorf("invalid dsn: %s", dsn) } s := &schema.Schema{} options := []factory.Option{factory.OwnerRepo(splitted[0] + "/" + splitted[1])} c, err := factory.NewGithubClient(options...) if err != nil { - return nil, errors.WithStack(err) + return nil, err } o := ghfs.Client(c) fsys, err := ghfs.New(splitted[0], splitted[1], o) if err != nil { - return nil, errors.WithStack(err) + return nil, err } b, err := fsys.ReadFile(splitted[2]) if err != nil { - return nil, errors.WithStack(err) + return nil, err } dec := json.NewDecoder(bytes.NewReader(b)) if err := dec.Decode(s); err != nil { - return s, errors.WithStack(err) + return s, err } if err := s.Repair(); err != nil { - return s, errors.WithStack(err) + return s, err } return s, nil } // AnalyzeJSON analyze `json://` -func AnalyzeJSON(urlstr string) (*schema.Schema, error) { +func AnalyzeJSON(urlstr string) (_ *schema.Schema, err error) { + defer func() { + err = errors.WithStack(err) + }() s := &schema.Schema{} splitted := strings.Split(urlstr, "json://") file, err := os.Open(splitted[1]) if err != nil { - return s, errors.WithStack(err) + return s, err } dec := json.NewDecoder(file) if err := dec.Decode(s); err != nil { - return s, errors.WithStack(err) + return s, err } if err := s.Repair(); err != nil { - return s, errors.WithStack(err) + return s, err } return s, nil } @@ -220,6 +233,9 @@ func AnalyzeJSONString(str string) (*schema.Schema, error) { // AnalyzeJSONStringOrFile analyze JSON string or JSON file func AnalyzeJSONStringOrFile(strOrPath string) (s *schema.Schema, err error) { + defer func() { + err = errors.WithStack(err) + }() s = &schema.Schema{} var buf io.Reader if strings.HasPrefix(strOrPath, "{") { @@ -227,15 +243,15 @@ func AnalyzeJSONStringOrFile(strOrPath string) (s *schema.Schema, err error) { } else { buf, err = os.Open(filepath.Clean(strOrPath)) if err != nil { - return s, errors.WithStack(err) + return s, err } } dec := json.NewDecoder(buf) if err := dec.Decode(s); err != nil { - return s, errors.WithStack(err) + return s, err } if err := s.Repair(); err != nil { - return s, errors.WithStack(err) + return s, err } return s, nil } diff --git a/drivers/mysql/mysql.go b/drivers/mysql/mysql.go index 10a044906..9cdc123f9 100644 --- a/drivers/mysql/mysql.go +++ b/drivers/mysql/mysql.go @@ -637,10 +637,13 @@ func convertColumnNullable(str string) bool { return str != "NO" } -func parseFK(def string) ([]string, string, []string, error) { +func parseFK(def string) (_ []string, _ string, _ []string, err error) { + defer func() { + err = errors.WithStack(err) + }() result := reFK.FindAllStringSubmatch(def, -1) if len(result) < 1 || len(result[0]) < 4 { - return nil, "", nil, errors.Errorf("can not parse foreign key: %s", def) + return nil, "", nil, fmt.Errorf("can not parse foreign key: %s", def) } strColumns := strings.Split(result[0][1], ", ") strParentTable := strings.Trim(result[0][2], `"`) diff --git a/drivers/postgres/postgres.go b/drivers/postgres/postgres.go index 404c949c5..b6ab4f358 100644 --- a/drivers/postgres/postgres.go +++ b/drivers/postgres/postgres.go @@ -32,7 +32,10 @@ func New(db *sql.DB) *Postgres { } // Analyze PostgreSQL database schema -func (p *Postgres) Analyze(s *schema.Schema) error { +func (p *Postgres) Analyze(s *schema.Schema) (err error) { + defer func() { + err = errors.WithStack(err) + }() d, err := p.Info() if err != nil { return errors.WithStack(err) @@ -280,7 +283,7 @@ ORDER BY tgrelid case "s": column.ExtraDef = fmt.Sprintf("GENERATED ALWAYS AS %s STORED", columnDefaultOrGenerated.String) default: - return errors.Errorf("unsupported pg_attribute.attrgenerated '%s'", attrgenerated.String) + return fmt.Errorf("unsupported pg_attribute.attrgenerated '%s'", attrgenerated.String) } columns = append(columns, column) } @@ -525,7 +528,10 @@ func (p *Postgres) EnableRsMode() { p.rsMode = true } -func (p *Postgres) queryForColumns(v string) (string, error) { +func (p *Postgres) queryForColumns(v string) (_ string, err error) { + defer func() { + err = errors.WithStack(err) + }() verGeneratedColumn, err := version.Parse("12") if err != nil { return "", err @@ -533,7 +539,7 @@ func (p *Postgres) queryForColumns(v string) (string, error) { // v => PostgreSQL 9.5.24 on x86_64-pc-linux-gnu (Debian 9.5.24-1.pgdg90+1), compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit matches := reVersion.FindStringSubmatch(v) if matches == nil || len(matches) < 2 { - return "", errors.Errorf("malformed version: %s", v) + return "", fmt.Errorf("malformed version: %s", v) } vv, err := version.Parse(matches[1]) if err != nil { @@ -661,7 +667,10 @@ GROUP BY cls.relname, idx.indexrelid, descr.description ORDER BY idx.indexrelid` } -func detectFullTableName(name string, searchPaths, fullTableNames []string) (string, error) { +func detectFullTableName(name string, searchPaths, fullTableNames []string) (_ string, err error) { + defer func() { + err = errors.WithStack(err) + }() if strings.Contains(name, ".") { return name, nil } @@ -676,7 +685,7 @@ func detectFullTableName(name string, searchPaths, fullTableNames []string) (str } } if len(fns) != 1 { - return "", errors.Errorf("can not detect table name: %s", name) + return "", fmt.Errorf("can not detect table name: %s", name) } return fns[0], nil } @@ -698,10 +707,13 @@ func convertConstraintType(t string) string { } } -func parseFK(def string) ([]string, string, []string, error) { +func parseFK(def string) (_ []string, _ string, _ []string, err error) { + defer func() { + err = errors.WithStack(err) + }() result := reFK.FindAllStringSubmatch(def, -1) if len(result) < 1 || len(result[0]) < 4 { - return nil, "", nil, errors.Errorf("can not parse foreign key: %s", def) + return nil, "", nil, fmt.Errorf("can not parse foreign key: %s", def) } strColumns := []string{} for _, c := range strings.Split(result[0][1], ", ") { diff --git a/drivers/sqlite/sqlite.go b/drivers/sqlite/sqlite.go index 43fe7c97b..673b828b8 100644 --- a/drivers/sqlite/sqlite.go +++ b/drivers/sqlite/sqlite.go @@ -42,7 +42,10 @@ type fk struct { } // Analyze SQLite database schema -func (l *Sqlite) Analyze(s *schema.Schema) error { +func (l *Sqlite) Analyze(s *schema.Schema) (err error) { + defer func() { + err = errors.WithStack(err) + }() d, err := l.Info() if err != nil { return errors.WithStack(err) @@ -77,7 +80,7 @@ WHERE name != 'sqlite_sequence' AND (type = 'table' OR type = 'view');`) tableType = "virtual table" matches := reFTS.FindStringSubmatch(tableDef) if len(matches) < 1 { - return errors.Errorf("can not parse table definition: %s", tableDef) + return fmt.Errorf("can not parse table definition: %s", tableDef) } shadowTables = append(shadowTables, fmt.Sprintf("%s_content", tableName)) shadowTables = append(shadowTables, fmt.Sprintf("%s_segdir", tableName)) @@ -491,10 +494,13 @@ func parseCheckConstraints(table *schema.Table, sql string) []*schema.Constraint return constraints } -func parseFK(def string) ([]string, string, []string, error) { +func parseFK(def string) (_ []string, _ string, _ []string, err error) { + defer func() { + err = errors.WithStack(err) + }() result := reFK.FindAllStringSubmatch(def, -1) if len(result) < 1 || len(result[0]) < 4 { - return nil, "", nil, errors.Errorf("can not parse foreign key: %s", def) + return nil, "", nil, fmt.Errorf("can not parse foreign key: %s", def) } strColumns := strings.Split(result[0][1], ", ") strParentTable := strings.Trim(result[0][2], `"`) diff --git a/schema/filter.go b/schema/filter.go index f08dd1365..32d87747a 100644 --- a/schema/filter.go +++ b/schema/filter.go @@ -16,7 +16,10 @@ type FilterOption struct { Distance int } -func (s *Schema) Filter(opt *FilterOption) error { +func (s *Schema) Filter(opt *FilterOption) (err error) { + defer func() { + err = errors.WithStack(err) + }() _, excludes, err := s.SepareteTablesThatAreIncludedOrNot(opt) if err != nil { return err @@ -24,14 +27,17 @@ func (s *Schema) Filter(opt *FilterOption) error { for _, t := range excludes { err := excludeTableFromSchema(t.Name, s) if err != nil { - return errors.Wrap(errors.WithStack(err), fmt.Sprintf("failed to filter table '%s'", t.Name)) + return fmt.Errorf("failed to filter table '%s': %w", t.Name, err) } } return nil } -func (s *Schema) SepareteTablesThatAreIncludedOrNot(opt *FilterOption) ([]*Table, []*Table, error) { +func (s *Schema) SepareteTablesThatAreIncludedOrNot(opt *FilterOption) (_ []*Table, _ []*Table, err error) { + defer func() { + err = errors.WithStack(err) + }() i := append(opt.Include, s.NormalizeTableNames(opt.Include)...) e := append(opt.Exclude, s.NormalizeTableNames(opt.Exclude)...) @@ -93,7 +99,7 @@ func (s *Schema) SepareteTablesThatAreIncludedOrNot(opt *FilterOption) ([]*Table // assert if len(s.Tables) != len(includes2)+len(excludes2) { - return nil, nil, errors.Errorf("failed to separate tables. expected: %d, actual: %d", len(s.Tables), len(includes2)+len(excludes2)) + return nil, nil, fmt.Errorf("failed to separate tables. expected: %d, actual: %d", len(s.Tables), len(includes2)+len(excludes2)) } return includes2, excludes2, nil diff --git a/schema/schema.go b/schema/schema.go index 0bf8dd5b0..38b7b0d19 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -214,17 +214,23 @@ func (s *Schema) NormalizeTableNames(names []string) []string { } // FindTableByName find table by table name -func (s *Schema) FindTableByName(name string) (*Table, error) { +func (s *Schema) FindTableByName(name string) (_ *Table, err error) { + defer func() { + err = errors.WithStack(err) + }() for _, t := range s.Tables { if s.NormalizeTableName(t.Name) == s.NormalizeTableName(name) { return t, nil } } - return nil, errors.Errorf("not found table '%s'", name) + return nil, fmt.Errorf("not found table '%s'", name) } // FindRelation find relation by columns and parent columns -func (s *Schema) FindRelation(cs, pcs []*Column) (*Relation, error) { +func (s *Schema) FindRelation(cs, pcs []*Column) (_ *Relation, err error) { + defer func() { + err = errors.WithStack(err) + }() L: for _, r := range s.Relations { if len(r.Columns) != len(cs) || len(r.ParentColumns) != len(pcs) { @@ -254,7 +260,7 @@ L: } return r, nil } - return nil, errors.Errorf("not found relation '%v, %v'", cs, pcs) + return nil, fmt.Errorf("not found relation '%v, %v'", cs, pcs) } func (s *Schema) HasTableWithLabels() bool { @@ -309,7 +315,10 @@ func (s *Schema) Sort() error { } // Repair column relations -func (s *Schema) Repair() error { +func (s *Schema) Repair() (err error) { + defer func() { + err = errors.WithStack(err) + }() if err := s.repairWithoutViewpoints(); err != nil { return err } @@ -317,14 +326,14 @@ func (s *Schema) Repair() error { for _, v := range s.Viewpoints { cs, err := s.CloneWithoutViewpoints() if err != nil { - return errors.Wrap(err, "failed to repair viewpoint") + return fmt.Errorf("failed to repair viewpoint: %w", err) } if err := cs.Filter(&FilterOption{ Include: v.Tables, IncludeLabels: v.Labels, Distance: v.Distance, }); err != nil { - return errors.Wrap(err, "failed to repair viewpoint") + return fmt.Errorf("failed to repair viewpoint: %w", err) } v.Schema = cs } @@ -367,7 +376,10 @@ func (s *Schema) CloneWithoutViewpoints() (c *Schema, err error) { return c, nil } -func (s *Schema) repairWithoutViewpoints() error { +func (s *Schema) repairWithoutViewpoints() (err error) { + defer func() { + err = errors.WithStack(err) + }() for _, t := range s.Tables { if len(t.Columns) == 0 { t.Columns = nil @@ -402,12 +414,12 @@ func (s *Schema) repairWithoutViewpoints() error { for _, r := range s.Relations { t, err := s.FindTableByName(r.Table.Name) if err != nil { - return errors.Wrap(err, "failed to repair relation") + return fmt.Errorf("failed to repair relation: %w", err) } for i, rc := range r.Columns { c, err := t.FindColumnByName(rc.Name) if err != nil { - return errors.Wrap(err, "failed to repair relation") + return fmt.Errorf("failed to repair relation: %w", err) } c.ParentRelations = append(c.ParentRelations, r) r.Columns[i] = c @@ -415,12 +427,12 @@ func (s *Schema) repairWithoutViewpoints() error { r.Table = t pt, err := s.FindTableByName(r.ParentTable.Name) if err != nil { - return errors.Wrap(err, "failed to repair relation") + return fmt.Errorf("failed to repair relation: %w", err) } for i, rc := range r.ParentColumns { pc, err := pt.FindColumnByName(rc.Name) if err != nil { - return errors.Wrap(err, "failed to repair relation") + return fmt.Errorf("failed to repair relation: %w", err) } pc.ChildRelations = append(pc.ChildRelations, r) r.ParentColumns[i] = pc @@ -435,43 +447,55 @@ func (s *Schema) repairWithoutViewpoints() error { } // FindColumnByName find column by column name -func (t *Table) FindColumnByName(name string) (*Column, error) { +func (t *Table) FindColumnByName(name string) (_ *Column, err error) { + defer func() { + err = errors.WithStack(err) + }() for _, c := range t.Columns { if c.Name == name { return c, nil } } - return nil, errors.Errorf("not found column '%s' on table '%s'", name, t.Name) + return nil, errors.New(fmt.Sprintf("not found column '%s' on table '%s'", name, t.Name)) } // FindIndexByName find index by index name -func (t *Table) FindIndexByName(name string) (*Index, error) { +func (t *Table) FindIndexByName(name string) (_ *Index, err error) { + defer func() { + err = errors.WithStack(err) + }() for _, i := range t.Indexes { if i.Name == name { return i, nil } } - return nil, errors.Errorf("not found index '%s' on table '%s'", name, t.Name) + return nil, errors.New(fmt.Sprintf("not found index '%s' on table '%s'", name, t.Name)) } // FindConstraintByName find constraint by constraint name -func (t *Table) FindConstraintByName(name string) (*Constraint, error) { +func (t *Table) FindConstraintByName(name string) (_ *Constraint, err error) { + defer func() { + err = errors.WithStack(err) + }() for _, c := range t.Constraints { if c.Name == name { return c, nil } } - return nil, errors.Errorf("not found constraint '%s' on table '%s'", name, t.Name) + return nil, errors.New(fmt.Sprintf("not found constraint '%s' on table '%s'", name, t.Name)) } // FindTriggerByName find trigger by trigger name -func (t *Table) FindTriggerByName(name string) (*Trigger, error) { +func (t *Table) FindTriggerByName(name string) (_ *Trigger, err error) { + defer func() { + err = errors.WithStack(err) + }() for _, trig := range t.Triggers { if trig.Name == name { return trig, nil } } - return nil, errors.Errorf("not found trigger '%s' on table '%s'", name, t.Name) + return nil, errors.New(fmt.Sprintf("not found trigger '%s' on table '%s'", name, t.Name)) } // FindConstrainsByColumnName find constraint by column name