-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
96 lines (83 loc) · 2.06 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
91
92
93
94
95
96
package swerve
import (
"encoding/json"
"io/fs"
)
type Import struct {
Path string `json:"path,omitempty"`
Code string `json:"code,omitempty"`
Config map[string]any `json:"config,omitempty"`
}
type Config struct {
Title string `json:"title,omitempty"`
Imports []*Import `json:"imports,omitempty"`
KnownHashes map[string]any `json:"knownHashes,omitempty"`
NoReloadOnInstall bool `json:"noReloadOnInstall,omitempty"`
ClaimOnInstall bool `json:"claimOnInstall,omitempty"`
}
func (c *Config) Copy() (*Config, error) {
newConfig := &Config{}
b, err := json.Marshal(c)
if err != nil {
return nil, err
}
if err = json.Unmarshal(b, newConfig); err != nil {
return nil, err
}
return newConfig, err
}
type Option func(*Config)
func WithTitle(title string) Option {
return func(c *Config) {
c.Title = title
}
}
func WithImports(imports ...*Import) Option {
return func(c *Config) {
c.Imports = append(c.Imports, imports...)
}
}
func WithNoReloadOnInstall(value bool) Option {
return func(c *Config) {
c.NoReloadOnInstall = value
}
}
func WithClaimOnInstall(value bool) Option {
return func(c *Config) {
c.ClaimOnInstall = true
}
}
func WithKnownHashes(hashes ...string) Option {
return func(c *Config) {
if c.KnownHashes == nil {
c.KnownHashes = make(map[string]any, len(hashes))
}
for _, hash := range hashes {
c.KnownHashes[hash] = map[string]any{"reason": "config"}
}
}
}
func WithKnownHashesFromFS(filesystem fs.FS, hashFuncs ...func([]byte) string) Option {
hashes := []string{}
fs.WalkDir(filesystem, ".", func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() {
b, err := fs.ReadFile(filesystem, path)
if err != nil {
return err
}
for _, hf := range hashFuncs {
hashes = append(hashes, hf(b))
}
}
return nil
})
return WithKnownHashes(hashes...)
}
func NewConfig(options ...Option) *Config {
config := &Config{}
for _, option := range options {
option(config)
}
return config
}
var DefaultConfig = NewConfig()