-
Notifications
You must be signed in to change notification settings - Fork 38
/
config.go
123 lines (106 loc) · 2.82 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package config
import (
"bytes"
_ "embed"
"fmt"
"os"
"path/filepath"
"github.com/gabotechs/dep-tree/internal/utils"
"gopkg.in/yaml.v3"
"github.com/gabotechs/dep-tree/internal/check"
golang "github.com/gabotechs/dep-tree/internal/go"
"github.com/gabotechs/dep-tree/internal/js"
"github.com/gabotechs/dep-tree/internal/python"
"github.com/gabotechs/dep-tree/internal/rust"
)
const DefaultConfigPath = ".dep-tree.yml"
//go:embed sample-config.yml
var SampleConfig string
type Config struct {
Path string
Source string
Exclude []string `yaml:"exclude"`
Only []string `yaml:"only"`
UnwrapExports bool `yaml:"unwrapExports"`
Check check.Config `yaml:"check"`
Js js.Config `yaml:"js"`
Rust rust.Config `yaml:"rust"`
Python python.Config `yaml:"python"`
Golang golang.Config `yaml:"golang"`
}
func NewConfigCwd() Config {
var cwd, _ = os.Getwd()
return Config{Path: cwd}
}
func (c *Config) EnsureAbsPaths() {
for i, file := range c.Exclude {
if !filepath.IsAbs(file) {
c.Exclude[i] = filepath.Join(c.Path, file)
}
}
for i, file := range c.Only {
if !filepath.IsAbs(file) {
c.Only[i] = filepath.Join(c.Path, file)
}
}
}
func (c *Config) ValidatePatterns() error {
for _, pattern := range c.Exclude {
if _, err := utils.GlobstarMatch(pattern, ""); err != nil {
return fmt.Errorf("exclude pattern '%s' is not correctly formatted", pattern)
}
}
for _, pattern := range c.Only {
if _, err := utils.GlobstarMatch(pattern, ""); err != nil {
return fmt.Errorf("only pattern '%s' is not correctly formatted", pattern)
}
}
return nil
}
func ParseConfigFromFile(cfgPath string) (*Config, error) {
// Default values.
cfg := Config{
Path: cfgPath,
Source: "default",
UnwrapExports: false,
Js: js.Config{
Workspaces: true,
TsConfigPaths: true,
},
Python: python.Config{
ExcludeConditionalImports: false,
},
Rust: rust.Config{},
}
var isDefault bool
if cfgPath == "" {
isDefault = true
cfgPath = DefaultConfigPath
}
absCfgPath, err := filepath.Abs(cfgPath)
if err != nil {
return nil, err
}
cfg.Path = filepath.Dir(absCfgPath)
cfg.Check.Path = cfg.Path
// If a specific path was requested, and it does not exist, fail
// If no specific path was requested, and the default config path does not exist, succeed
content, err := os.ReadFile(cfgPath)
if os.IsNotExist(err) {
if isDefault {
return &cfg, nil
}
return nil, err
} else if err != nil {
return nil, err
}
cfg.Source = "file"
decoder := yaml.NewDecoder(bytes.NewReader(content))
decoder.KnownFields(true)
err = decoder.Decode(&cfg)
if err != nil {
return nil, fmt.Errorf(`config file "%s" is not a valid yml file: %w`, cfgPath, err)
}
cfg.Check.Init(filepath.Dir(absCfgPath))
return &cfg, nil
}