-
Notifications
You must be signed in to change notification settings - Fork 9
/
configurator.go
89 lines (67 loc) · 2.15 KB
/
configurator.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
package configurator
import (
"context"
"fmt"
"net/http"
"os"
"path"
"strings"
"github.com/heetch/confita"
"github.com/heetch/confita/backend/flags"
)
type Config struct {
SwaggerAddr string `config:"swagger-addr,required"`
Package string `config:"package,required"`
Path string `config:"path,required"`
ComponentsPackage string `config:"componentsPackage"`
ComponentsPath string `config:"componentsPath"`
Authorization string `config:"authorization,short=a,description=a list of comma-separated key:value pairs to be sent as headers alongside each http request"`
PrioritizeXGoType bool `config:"prioritize-x-go-type,description=prioritize x-go-type declaration over schema type, if both are provided"`
}
func (config *Config) Defaults() *Config {
config.SwaggerAddr = "swagger.yaml"
return config
}
func (config *Config) Headers() (http.Header, error) {
headers := strings.Split(config.Authorization, ",")
out := make(http.Header)
for _, header := range headers {
keyValue := strings.Split(header, ":")
if len(keyValue) != 2 {
return nil, fmt.Errorf("invalid header format: %q", header)
}
out[keyValue[0]] = []string{keyValue[1]}
}
return out, nil
}
type Configurator struct {
config *Config `di.inject:"config"`
}
func (configurator *Configurator) concatPaths(filePath string) (string, error) {
if filePath[0] == '.' {
wd, err := os.Getwd()
if err != nil {
return "", err
}
return path.Join(wd, filePath), nil
}
return filePath, nil
}
func (configurator *Configurator) PostConstruct() (err error) {
if err := confita.NewLoader(flags.NewBackend()).Load(context.Background(), configurator.config); err != nil {
return err
}
if configurator.config.Path, err = configurator.concatPaths(configurator.config.Path); err != nil {
return err
}
if configurator.config.Package, err = configurator.concatPaths(configurator.config.Path); err != nil {
return err
}
if configurator.config.ComponentsPackage == "" {
configurator.config.ComponentsPackage = configurator.config.Package
}
if configurator.config.ComponentsPath == "" {
configurator.config.ComponentsPath = configurator.config.Path
}
return nil
}