-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
74 lines (63 loc) · 1.6 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
package yeoman
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
)
const (
ServerConfigName = "yeoman.json"
AppConfigName = "app.json"
)
type LogFormat string
const (
LogFormatDefault LogFormat = ""
LogFormatConsole LogFormat = "console"
LogFormatJSON LogFormat = "json"
)
type LogLevel string
const (
LogLevelDefault LogLevel = ""
LogLevelDebug LogLevel = "debug"
LogLevelInfo LogLevel = "info"
)
type Config struct {
Log LogConfig `json:"log,omitempty"`
ProviderRegistries []ProviderRegistry `json:"providerRegistries"`
Store string `json:"store"`
}
type LogConfig struct {
Format LogFormat `json:"format,omitempty"`
Level LogLevel `json:"level,omitempty"`
}
type ProviderRegistry struct {
Provider string `json:"provider"`
Registry string `json:"registry"`
ServiceAccount string `json:"serviceAccount"`
}
func ParseConfigForService(name string) (Config, error) {
var conf Config
byt, err := os.ReadFile(ServerConfigName)
if errors.Is(err, os.ErrNotExist) {
byt, err = os.ReadFile(filepath.Join(name, ServerConfigName))
}
if err != nil {
return conf, fmt.Errorf("read file: %w", err)
}
if err := json.Unmarshal(byt, &conf); err != nil {
return conf, fmt.Errorf("unmarshal: %w", err)
}
return conf, nil
}
func ParseConfig(configPath string) (Config, error) {
var conf Config
byt, err := os.ReadFile(configPath)
if err != nil {
return conf, fmt.Errorf("read file: %w", err)
}
if err := json.Unmarshal(byt, &conf); err != nil {
return conf, fmt.Errorf("unmarshal: %w", err)
}
return conf, nil
}