-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.go
57 lines (46 loc) · 1.19 KB
/
settings.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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/goccy/go-yaml"
"github.com/kmc-jp/ChannelInviter/database"
"github.com/kmc-jp/ChannelInviter/slack"
)
type Settings struct {
Slack slack.Settings `yaml:"Slack"`
Database database.Settings `yaml:"Database"`
}
func ReadSettings() (*Settings, error) {
var yamlRootPath = "settings"
dir, err := os.ReadDir(yamlRootPath)
if err != nil {
return nil, fmt.Errorf("ReadDir: %w", err)
}
var yamlBinary = []byte{}
for _, f := range dir {
if f.IsDir() || !(strings.HasSuffix(f.Name(), ".yaml") || strings.HasSuffix(f.Name(), ".yml")) {
continue
}
var yamlFilePath = filepath.Join(yamlRootPath, f.Name())
b, err := os.ReadFile(yamlFilePath)
if err != nil {
return nil, fmt.Errorf("ReadFile: %s %w", yamlFilePath, err)
}
// check format
var us Settings
err = yaml.Unmarshal(b, &us)
if err != nil {
return nil, fmt.Errorf("UnmarshalSettings: %s\n%w", yamlFilePath, err)
}
yamlBinary = append(yamlBinary, b...)
yamlBinary = append(yamlBinary, '\n')
}
var us Settings
err = yaml.Unmarshal(yamlBinary, &us)
if err != nil {
return nil, fmt.Errorf("Unmarshal: %w", err)
}
return &us, nil
}