@@ -9,12 +9,12 @@ import (
9
9
10
10
"github.com/aquasecurity/go-version/pkg/version"
11
11
"github.com/goccy/go-yaml"
12
+ "github.com/k1LoW/errors"
12
13
"github.com/k1LoW/expand"
13
14
"github.com/k1LoW/tbls/dict"
14
15
"github.com/k1LoW/tbls/schema"
15
16
ver "github.com/k1LoW/tbls/version"
16
17
"github.com/minio/pkg/wildcard"
17
- "github.com/pkg/errors"
18
18
"github.com/samber/lo"
19
19
)
20
20
@@ -356,12 +356,15 @@ func (c *Config) LoadEnviron() error {
356
356
}
357
357
358
358
// LoadConfigFile load config file
359
- func (c * Config ) LoadConfigFile (path string ) error {
359
+ func (c * Config ) LoadConfigFile (path string ) (err error ) {
360
+ defer func () {
361
+ err = errors .WithStack (err )
362
+ }()
360
363
if path == "" && os .Getenv ("TBLS_DSN" ) == "" {
361
364
for _ , p := range DefaultConfigFilePaths {
362
365
if f , err := os .Stat (filepath .Join (c .root , p )); err == nil && ! f .IsDir () {
363
366
if path != "" {
364
- return errors .Errorf ("duplicate config file [%s, %s]" , path , p )
367
+ return fmt .Errorf ("duplicate config file [%s, %s]" , path , p )
365
368
}
366
369
path = p
367
370
}
@@ -373,23 +376,25 @@ func (c *Config) LoadConfigFile(path string) error {
373
376
374
377
fullPath , err := filepath .Abs (path )
375
378
if err != nil {
376
- return errors . Wrap ( errors . WithStack ( err ), "failed to load config file" )
379
+ return fmt . Errorf ( "failed to load config file: %w" , err )
377
380
}
378
381
379
382
buf , err := os .ReadFile (filepath .Clean (fullPath ))
380
383
if err != nil {
381
- return errors . Wrap ( errors . WithStack ( err ), "failed to load config file" )
384
+ return fmt . Errorf ( "failed to load config file: %w" , err )
382
385
}
383
386
c .Path = filepath .Clean (fullPath )
384
387
385
388
return c .LoadConfig (buf )
386
389
}
387
390
388
391
// LoadConfig load config from []byte
389
- func (c * Config ) LoadConfig (in []byte ) error {
390
- err := yaml .Unmarshal (expand .ExpandenvYAMLBytes (in ), c )
391
- if err != nil {
392
- return errors .Wrap (errors .WithStack (err ), "failed to load config file" )
392
+ func (c * Config ) LoadConfig (in []byte ) (err error ) {
393
+ defer func () {
394
+ err = errors .WithStack (err )
395
+ }()
396
+ if err := yaml .Unmarshal (expand .ExpandenvYAMLBytes (in ), c ); err != nil {
397
+ return fmt .Errorf ("failed to load config file: %w" , err )
393
398
}
394
399
c .MergedDict .Merge (c .Dict .Dump ())
395
400
return nil
@@ -611,15 +616,18 @@ func (c *Config) detectShowColumnsForER(s *schema.Schema) error {
611
616
return nil
612
617
}
613
618
614
- func mergeAdditionalRelations (s * schema.Schema , relations []AdditionalRelation ) error {
619
+ func mergeAdditionalRelations (s * schema.Schema , relations []AdditionalRelation ) (err error ) {
620
+ defer func () {
621
+ err = errors .WithStack (err )
622
+ }()
615
623
for _ , r := range relations {
616
624
c , err := schema .ToCardinality (r .Cardinality )
617
625
if err != nil {
618
- return errors . Wrap ( err , "failed to add relation" )
626
+ return fmt . Errorf ( "failed to add relation: %w" , err )
619
627
}
620
628
pc , err := schema .ToCardinality (r .ParentCardinality )
621
629
if err != nil {
622
- return errors . Wrap ( err , "failed to add relation" )
630
+ return fmt . Errorf ( "failed to add relation: %w" , err )
623
631
}
624
632
relation := & schema.Relation {
625
633
Cardinality : c ,
@@ -633,24 +641,24 @@ func mergeAdditionalRelations(s *schema.Schema, relations []AdditionalRelation)
633
641
}
634
642
relation .Table , err = s .FindTableByName (r .Table )
635
643
if err != nil {
636
- return errors . Wrap ( err , "failed to add relation" )
644
+ return fmt . Errorf ( "failed to add relation: %w" , err )
637
645
}
638
646
for _ , c := range r .Columns {
639
647
column , err := relation .Table .FindColumnByName (c )
640
648
if err != nil {
641
- return errors . Wrap ( err , "failed to add relation" )
649
+ return fmt . Errorf ( "failed to add relation: %w" , err )
642
650
}
643
651
relation .Columns = append (relation .Columns , column )
644
652
column .ParentRelations = append (column .ParentRelations , relation )
645
653
}
646
654
relation .ParentTable , err = s .FindTableByName (r .ParentTable )
647
655
if err != nil {
648
- return errors . Wrap ( err , "failed to add relation" )
656
+ return fmt . Errorf ( "failed to add relation: %w" , err )
649
657
}
650
658
for _ , c := range r .ParentColumns {
651
659
column , err := relation .ParentTable .FindColumnByName (c )
652
660
if err != nil {
653
- return errors . Wrap ( err , "failed to add relation" )
661
+ return fmt . Errorf ( "failed to add relation: %w" , err )
654
662
}
655
663
relation .ParentColumns = append (relation .ParentColumns , column )
656
664
column .ChildRelations = append (column .ChildRelations , relation )
@@ -665,11 +673,11 @@ func mergeAdditionalRelations(s *schema.Schema, relations []AdditionalRelation)
665
673
cr .Def = r .Def
666
674
cr .Cardinality , err = schema .ToCardinality (r .Cardinality )
667
675
if err != nil {
668
- return errors . Wrap ( err , "failed to add relation" )
676
+ return fmt . Errorf ( "failed to add relation: %w" , err )
669
677
}
670
678
cr .ParentCardinality , err = schema .ToCardinality (r .ParentCardinality )
671
679
if err != nil {
672
- return errors . Wrap ( err , "failed to add relation" )
680
+ return fmt . Errorf ( "failed to add relation: %w" , err )
673
681
}
674
682
}
675
683
} else {
@@ -679,11 +687,14 @@ func mergeAdditionalRelations(s *schema.Schema, relations []AdditionalRelation)
679
687
return nil
680
688
}
681
689
682
- func mergeAdditionalComments (s * schema.Schema , comments []AdditionalComment ) error {
690
+ func mergeAdditionalComments (s * schema.Schema , comments []AdditionalComment ) (err error ) {
691
+ defer func () {
692
+ err = errors .WithStack (err )
693
+ }()
683
694
for _ , c := range comments {
684
695
table , err := s .FindTableByName (c .Table )
685
696
if err != nil {
686
- return errors . Wrap ( err , "failed to add table comment" )
697
+ return fmt . Errorf ( "failed to add table comment: %w" , err )
687
698
}
688
699
if c .TableComment != "" {
689
700
table .Comment = c .TableComment
@@ -696,14 +707,14 @@ func mergeAdditionalComments(s *schema.Schema, comments []AdditionalComment) err
696
707
for c , comment := range c .ColumnComments {
697
708
column , err := table .FindColumnByName (c )
698
709
if err != nil {
699
- return errors . Wrap ( err , "failed to add column comment" )
710
+ return fmt . Errorf ( "failed to add column comment: %w" , err )
700
711
}
701
712
column .Comment = comment
702
713
}
703
714
for c , labels := range c .ColumnLabels {
704
715
column , err := table .FindColumnByName (c )
705
716
if err != nil {
706
- return errors . Wrap ( err , "failed to add column comment" )
717
+ return fmt . Errorf ( "failed to add column comment: %w" , err )
707
718
}
708
719
for _ , l := range labels {
709
720
column .Labels = column .Labels .Merge (l )
@@ -712,21 +723,21 @@ func mergeAdditionalComments(s *schema.Schema, comments []AdditionalComment) err
712
723
for i , comment := range c .IndexComments {
713
724
index , err := table .FindIndexByName (i )
714
725
if err != nil {
715
- return errors . Wrap ( err , "failed to add index comment" )
726
+ return fmt . Errorf ( "failed to add index comment: %w" , err )
716
727
}
717
728
index .Comment = comment
718
729
}
719
730
for c , comment := range c .ConstraintComments {
720
731
constraint , err := table .FindConstraintByName (c )
721
732
if err != nil {
722
- return errors . Wrap ( err , "failed to add constraint comment" )
733
+ return fmt . Errorf ( "failed to add constraint comment: %w" , err )
723
734
}
724
735
constraint .Comment = comment
725
736
}
726
737
for t , comment := range c .TriggerComments {
727
738
trigger , err := table .FindTriggerByName (t )
728
739
if err != nil {
729
- return errors . Wrap ( err , "failed to add trigger comment" )
740
+ return fmt . Errorf ( "failed to add trigger comment: %w" , err )
730
741
}
731
742
trigger .Comment = comment
732
743
}
0 commit comments