This repository has been archived by the owner on May 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
88 lines (72 loc) · 1.59 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
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"gopkg.in/yaml.v3"
)
// AWSConfig The configuration of AWS client.
type AWSConfig struct {
ClientID string
ClientSecret string
Region string
Sender string
}
// Instance The configurations of server instance.
type Instance struct {
Addr string
URI string
Scheme string
}
// InstanceGroup The configurations of the group of instances.
type InstanceGroup struct {
Instances []Instance
Type string
Name string
}
// Config is the configuration of the health checker.
type Config struct {
Instances []Instance
Groups []InstanceGroup
AWS AWSConfig
URI string
Scheme string
Timeout int
Receivers *[]*string
Receiver *string
}
// loadConfig gets configurations from file.
func loadConfig(path string) (*Config, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
extension := getFileExtension(file.Name())
switch extension {
case "json":
return parseJSONFile(file)
case "yaml", "yml":
return parseYAMLFile(file)
default:
return nil, fmt.Errorf("unsupported %s file format", extension)
}
}
func parseJSONFile(file *os.File) (*Config, error) {
cfg := new(Config)
if err := json.NewDecoder(file).Decode(cfg); err != nil {
return nil, err
}
return cfg, nil
}
func parseYAMLFile(file *os.File) (*Config, error) {
cfg := new(Config)
if err := yaml.NewDecoder(file).Decode(cfg); err != nil {
return nil, err
}
return cfg, nil
}
func getFileExtension(name string) string {
arr := strings.Split(name, ".")
return strings.ToLower(arr[len(arr)-1])
}