-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
96 lines (85 loc) · 1.98 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 gomods
import (
"strconv"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/spf13/afero"
)
type Config struct {
GoBinary string `json:"gobinary,omitempty"`
Workers int `json:"workers,omitempty"`
Cache Cache `json:"cache,omitempty"`
fs afero.Fs
}
type Cache struct {
Enable bool
Type string `json:"type,omitempty"`
Path string `json:"path,omitempty"`
}
const (
// DefaultGomodsCacheType is the default cache mode
DefaultGomodsCacheType = "tmp"
// DefaultGomodsWorkers is the default number of parallel workers
DefaultGomodsWorkers = 1
)
// SetDefaults sets the default values for gomods config
// if the fields are empty
func (conf *Config) SetDefaults() {
conf.fs = afero.NewOsFs()
if conf.GoBinary == "" {
conf.GoBinary = DefaultGoBinaryPath
}
if conf.Cache.Enable {
if conf.Cache.Type == "" {
conf.Cache.Type = DefaultGomodsCacheType
}
if conf.Cache.Path == "" {
conf.Cache.Path = afero.GetTempDir(conf.fs, "")
}
}
if conf.Workers == 0 {
conf.Workers = DefaultGomodsWorkers
}
}
// ParseGomods parses the txtdirect config for gomods
func (conf *Config) ParseGomods(d *caddyfile.Dispenser) error {
switch d.Val() {
case "gobinary":
conf.GoBinary = d.RemainingArgs()[0]
case "workers":
value, err := strconv.Atoi(d.RemainingArgs()[0])
if err != nil {
return d.ArgErr()
}
conf.Workers = value
case "cache":
conf.Cache.Enable = true
d.Next()
if d.Val() != "{" {
break
}
for d.Next() {
if d.Val() == "}" {
continue
}
err := conf.Cache.ParseCache(d)
if err != nil {
return err
}
}
default:
return d.ArgErr() // unhandled option for gomods
}
return nil
}
// ParseCache parses the txtdirect config for gomods cache
func (cache *Cache) ParseCache(d *caddyfile.Dispenser) error {
switch d.Val() {
case "type":
cache.Type = d.RemainingArgs()[0]
case "path":
cache.Path = d.RemainingArgs()[0]
default:
return d.ArgErr() // unhandled option for gomods cache
}
return nil
}