-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
57 lines (52 loc) · 1.18 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
package main
import (
"errors"
"io/fs"
"log"
"os"
"path"
"golang.org/x/mod/module"
"gopkg.in/yaml.v3"
)
type Config struct {
AllowedLicenses []string
GoModFiles []string
LicenseOverrides []LicenseOverride
}
type LicenseOverride struct {
ModuleVersion module.Version
SPDX string
}
func ReadConfig(filename string) Config {
var config Config
// if no config file is provided, look for one
if filename == "" {
executableName := path.Base(os.Args[0])
var possibleFileNames = []string{
"." + executableName + ".yaml",
executableName + ".yaml",
"." + executableName + ".yml",
executableName + ".yml",
"." + executableName + ".json",
executableName + ".json",
}
for _, t := range possibleFileNames {
if _, err := os.Stat(t); !errors.Is(err, fs.ErrNotExist) {
filename = t
break
}
}
}
// parse config file, if one was provided or detected
if filename != "" {
configFileBytes, err := os.ReadFile(filename)
if err != nil {
log.Fatalf("cannot read configuration file: %v", err)
}
err = yaml.Unmarshal(configFileBytes, &config)
if err != nil {
log.Fatalf("cannot parse configuration file: %v", err)
}
}
return config
}