-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
132 lines (115 loc) · 3.46 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
124
125
126
127
128
129
130
131
132
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/programmfabrik/apitest/pkg/lib/filesystem"
"github.com/programmfabrik/apitest/pkg/lib/util"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/spf13/viper"
)
type ConfigStruct struct {
Apitest struct {
Server string `mapstructure:"server"`
StoreInit map[string]any `mapstructure:"store"`
Limit struct {
Request int `mapstructure:"request"`
Response int `mapstructure:"response"`
} `mapstructure:"limit"`
Log struct {
Short bool `mapstructure:"short"`
} `mapstructure:"log"`
Report struct {
File string `mapstructure:"file"`
Format string `mapstructure:"format"`
} `mapstructure:"report"`
OAuthClient util.OAuthClientsConfig `mapstructure:"oauth_client"`
}
}
var Config ConfigStruct
var startTime time.Time
func LoadConfig(cfgFile string) {
startTime = time.Now()
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
logrus.Infof("No config file provided (will only use command line parameters)")
}
if err := viper.ReadInConfig(); err != nil {
logrus.Infof("No config \"%s\" read (will only use command line parameters): %s", cfgFile, err)
}
viper.Unmarshal(&Config)
}
// TestToolConfig gives us the basic testtool infos
type TestToolConfig struct {
ServerURL string
rootDirectorys []string
TestDirectories []string
LogNetwork bool
LogVerbose bool
LogShort bool
OAuthClient util.OAuthClientsConfig
}
// NewTestToolConfig is mostly used for testing purpose. We can setup our config with this function
func NewTestToolConfig(serverURL string, rootDirectory []string, logNetwork bool, logVerbose bool, logShort bool) (config TestToolConfig, err error) {
config = TestToolConfig{
ServerURL: serverURL,
rootDirectorys: rootDirectory,
LogNetwork: logNetwork,
LogVerbose: logVerbose,
LogShort: logShort,
OAuthClient: Config.Apitest.OAuthClient,
}
config.fillInOAuthClientNames()
err = config.extractTestDirectories()
return config, err
}
func (config *TestToolConfig) extractTestDirectories() error {
for _, rootDirectory := range config.rootDirectorys {
if _, err := filesystem.Fs.Stat(rootDirectory); err != nil {
return fmt.Errorf("The given root directory '%s' is not valid", rootDirectory)
}
}
for _, rootDirectory := range config.rootDirectorys {
err := afero.Walk(filesystem.Fs, rootDirectory, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
// Skip directories starting with "_"
if strings.Contains(path, "/_") {
// logrus.Infof("Skipping: %s", path)
return filepath.SkipDir
}
//Skip directories not containing a manifest
_, err := filesystem.Fs.Stat(filepath.Join(path, "manifest.json"))
if err != nil {
return nil
}
config.TestDirectories = append(config.TestDirectories, path)
dirRel, err := filepath.Rel(rootDirectory, path)
if err != nil {
dirRel = path
}
if dirRel == "." {
dirRel = filepath.Base(path)
}
}
return nil
})
if err != nil {
return err
}
}
return nil
}
// fillInOAuthClientNames fills in the Client field of loaded OAuthClientConfig
// structs, which the user may have left unset in the config yaml file.
func (config *TestToolConfig) fillInOAuthClientNames() {
for key, clientConfig := range config.OAuthClient {
if clientConfig.Client == "" {
clientConfig.Client = key
config.OAuthClient[key] = clientConfig
}
}
}