forked from krystal/kcm-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
67 lines (54 loc) · 1.32 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
package main
import (
"io/fs"
"io/ioutil"
"strconv"
"gopkg.in/yaml.v3"
)
type Config struct {
Certificates []Certificate `yaml:"certificates"`
}
type Certificate struct {
URL string `yaml:"url"`
Paths Paths `yaml:"paths"`
Permissions Permissions `yaml:"permissions"`
Commands []string `yaml:"commands"`
}
type Paths struct {
PrivateKey string `yaml:"private_key"`
Certificate string `yaml:"certificate"`
Chain string `yaml:"chain"`
CertificateWithChain string `yaml:"certificate_with_chain"`
}
type Permissions struct {
Certificates int64 `yaml:"certificates"`
Keys int64 `yaml:"keys"`
}
func (p Permissions) CertificatesFileMode() fs.FileMode {
if p.Certificates == 0 {
return fs.FileMode(0644)
}
i := strconv.FormatInt(p.Certificates, 10)
fm, _ := strconv.ParseInt(i, 8, 32)
return fs.FileMode(fm)
}
func (p Permissions) KeysFileMode() fs.FileMode {
if p.Keys == 0 {
return fs.FileMode(0600)
}
i := strconv.FormatInt(p.Keys, 10)
fm, _ := strconv.ParseInt(i, 8, 32)
return fs.FileMode(fm)
}
func NewConfigFromFile(path string) (*Config, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
var config Config
err = yaml.Unmarshal(data, &config)
if err != nil {
return nil, err
}
return &config, nil
}