-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.go
81 lines (67 loc) · 1.88 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/goccy/go-yaml"
"github.com/pkg/errors"
)
type Setting struct {
Voicevox VoicevoxSetting `yaml:"Voicevox"`
GoogleHome GoogleHomeSetting `yaml:"GoogleHome"`
Slack SlackSetting `yaml:"Slack"`
}
type VoicevoxSetting struct {
SpeakerID uint32 `yaml:"SpeakerID"`
OpenJtalkDictDir string `yaml:"OpenJtalkDictDir"`
}
type GoogleHomeSetting struct {
DeviceName string `yaml:"DeviceName"`
Device string `yaml:"Device"`
Iface string `yaml:"Iface"`
ForceDetach bool `yaml:"ForceDetach"`
Detach bool `yaml:"Detach"`
Addr string `yaml:"Addr"`
Port int `yaml:"Port"`
UUID string `yaml:"UUID"`
Volume float32 `yaml:"Volume"`
MaxDuration float32 `yaml:"MaxDuration"`
}
type SlackSetting struct {
Token string `yaml:"Token"`
AppLevelToken string `yaml:"AppLevelToken"`
Icon string `yaml:"Icon"`
}
func ReadSettings() (*Setting, error) {
var yamlRootPath = "settings"
dir, err := os.ReadDir(yamlRootPath)
if err != nil {
return nil, errors.Wrap(err, "ReadDir")
}
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, errors.Wrap(err, fmt.Sprintf("ReadFile: %s", yamlFilePath))
}
// check format
var us Setting
err = yaml.Unmarshal(b, &us)
if err != nil {
return nil, errors.New(fmt.Sprintf("UnmarshalSettings: %s\n%s", yamlFilePath, err.Error()))
}
yamlBinary = append(yamlBinary, b...)
yamlBinary = append(yamlBinary, '\n')
}
var us Setting
err = yaml.Unmarshal(yamlBinary, &us)
if err != nil {
return nil, errors.Wrap(err, "Unmarshal")
}
return &us, nil
}