-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
90 lines (72 loc) · 1.96 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
89
90
package main
import (
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"strconv"
"gopkg.in/yaml.v3"
)
type (
config struct {
Certificates []certificate `yaml:"certificates"`
}
certificate struct {
URL string `yaml:"url"`
Paths paths `yaml:"paths"`
Permissions permissions `yaml:"permissions"`
Commands []string `yaml:"commands"`
}
paths struct {
//nolint:tagliatelle // https://github.com/ldez/tagliatelle/issues/10#issuecomment-1133855221
PrivateKey string `yaml:"private_key"`
Certificate string `yaml:"certificate"`
Chain string `yaml:"chain"`
//nolint:tagliatelle // https://github.com/ldez/tagliatelle/issues/10#issuecomment-1133855221
CertificateWithChain string `yaml:"certificate_with_chain"`
}
permissions struct {
Certificates int64 `yaml:"certificates"`
Keys int64 `yaml:"keys"`
}
)
const (
defaultCertificatesFileMode = uint32(0644)
defaultKeysFileMode = uint32(0600)
base10 = 10
base8 = 8
fileModeBitSize = 32
)
func parseFileMode(mode int64) fs.FileMode {
i := strconv.FormatInt(mode, base10)
fm, err := strconv.ParseInt(i, base8, fileModeBitSize)
if err != nil {
log.Println("error parsing file mode:", err)
}
return fs.FileMode(fm)
}
func (p permissions) certificatesFileMode() fs.FileMode {
if p.Certificates == 0 {
return fs.FileMode(defaultCertificatesFileMode)
}
return parseFileMode(p.Certificates)
}
func (p permissions) keysFileMode() fs.FileMode {
if p.Keys == 0 {
return fs.FileMode(defaultKeysFileMode)
}
return parseFileMode(p.Keys)
}
func newConfigFromFile(path string) (*config, error) {
data, err := os.ReadFile(filepath.Clean(path))
if err != nil {
return nil, fmt.Errorf("error reading config file: %w", err)
}
var configData config
err = yaml.Unmarshal(data, &configData)
if err != nil {
return nil, fmt.Errorf("error unmarshalling config file: %w", err)
}
return &configData, nil
}