Skip to content

Commit

Permalink
Merge pull request #613 from k1LoW/change-errors
Browse files Browse the repository at this point in the history
Use github.com/k1LoW/errors
  • Loading branch information
k1LoW authored Aug 11, 2024
2 parents b7b6535 + ed003a9 commit 7ec2bcf
Show file tree
Hide file tree
Showing 31 changed files with 202 additions and 118 deletions.
4 changes: 2 additions & 2 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"os"
"path/filepath"

"github.com/pkg/errors"
"github.com/k1LoW/errors"
"github.com/spf13/cobra"
)

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ import (
"path/filepath"
"strings"

"github.com/k1LoW/errors"
"github.com/k1LoW/tbls/cmdutil"
"github.com/k1LoW/tbls/config"
"github.com/k1LoW/tbls/datasource"
"github.com/k1LoW/tbls/output/gviz"
"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"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
5 changes: 3 additions & 2 deletions cmd/out.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
package cmd

import (
"fmt"
"io"
"os"

"github.com/k1LoW/errors"
"github.com/k1LoW/tbls/cmdutil"
"github.com/k1LoW/tbls/config"
"github.com/k1LoW/tbls/output"
Expand All @@ -36,7 +38,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"
)

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(err, errors.StackTraces(err))
} else {
fmt.Println(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmdutil/when.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
61 changes: 36 additions & 25 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand All @@ -373,23 +376,25 @@ 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)

return c.LoadConfig(buf)
}

// 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
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
}
Expand Down
Loading

0 comments on commit 7ec2bcf

Please sign in to comment.