-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
78 lines (65 loc) · 1.61 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
package main
/*
import (
"fmt"
"github.com/anthonyraymond/joal-cli/internal/old/common/configloader"
"github.com/anthonyraymond/joal-cli/internal/old/core/logs"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
)
var (
configFileFromRoot = func(rootConfigDir string) string {
return filepath.Join(rootConfigDir, "config.yml")
}
)
func BootstrapApp(configDir string) (*AppConfig, error) {
if err := os.MkdirAll(configDir, 0755); err != nil {
return nil, fmt.Errorf("failed to create folder '%s': %w", configDir, err)
}
// Create the configuration file if missing
f, err := os.OpenFile(configFileFromRoot(configDir), os.O_CREATE, 0755)
if err != nil {
return nil, fmt.Errorf("failed to create '%s' file: %w", configFileFromRoot(configDir), err)
}
_ = f.Close()
return readConfig(configDir)
}
func readConfig(configDir string) (*AppConfig, error) {
conf := AppConfig{}.Default()
err := configloader.ParseIntoDefault(configFileFromRoot(configDir), conf)
if err != nil {
return nil, err
}
return conf, nil
}
type AppConfig struct {
Log *logs.LogConfig `yaml:"log"`
Proxy ProxyConf `yaml:"proxy"`
}
func (ac AppConfig) Default() *AppConfig {
return &AppConfig{
Log: logs.LogConfig{}.Default(),
Proxy: ProxyConf{}.Default(),
}
}
type ProxyConf struct {
Url string `yaml:"url"`
}
func (pc ProxyConf) Default() ProxyConf {
return ProxyConf{
Url: "",
}
}
func (pc *ProxyConf) Proxy() func(*http.Request) (*url.URL, error) {
if strings.TrimSpace(pc.Url) == "" {
return nil
}
u, err := url.Parse(pc.Url)
return func(request *http.Request) (*url.URL, error) {
return u, err
}
}
*/