-
Notifications
You must be signed in to change notification settings - Fork 26
/
option.go
37 lines (32 loc) · 855 Bytes
/
option.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
package registry
import (
"errors"
"fmt"
)
// regConfig contains all options for the server.
type regConfig struct {
freezeAtPercent float64
freezeDirs []string
}
// Option is a function that sets a value in a regConfig.
type Option func(*regConfig) error
// getOpts creates a regConfig and applies Options to it.
func getOpts(opts []Option) (regConfig, error) {
var cfg regConfig
for i, opt := range opts {
if err := opt(&cfg); err != nil {
return regConfig{}, fmt.Errorf("option %d error: %s", i, err)
}
}
return cfg, nil
}
func WithFreezer(freezeDirs []string, freezeAtPercent float64) Option {
return func(c *regConfig) error {
if len(freezeDirs) != 0 && freezeAtPercent == 0 {
return errors.New("cannot freeze at 0 percent usage")
}
c.freezeAtPercent = freezeAtPercent
c.freezeDirs = freezeDirs
return nil
}
}