-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.go
68 lines (56 loc) · 1.62 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
package service
import (
"github.com/cerana/cerana/pkg/errors"
"github.com/cerana/cerana/provider"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
// Config holds all configuration for the provider.
type Config struct {
*provider.Config
}
// ConfigData defines the structure of the config data (e.g. in the config file)
type ConfigData struct {
provider.ConfigData
RollbackCloneCmd string `json:"rollback_clone_cmd"`
DatasetCloneDir string `json:"dataset_clone_dir"`
}
// RollbackCloneCmd returns the full path of the clone/rollback script datasets
// for services.
func (c *Config) RollbackCloneCmd() string {
var dcb string
_ = c.UnmarshalKey("rollback_clone_cmd", &dcb)
// Checked at validation time
return dcb
}
// DatasetCloneDir returns the zfs path in which to clone datasets.
func (c *Config) DatasetCloneDir() string {
var dcp string
_ = c.UnmarshalKey("dataset_clone_dir", &dcp)
// Checked at validation time
return dcp
}
// LoadConfig loads and validates the config data.
func (c *Config) LoadConfig() error {
if err := c.Config.LoadConfig(); err != nil {
return err
}
return c.Validate()
}
// NewConfig creates a new instance of Config.
func NewConfig(flagSet *pflag.FlagSet, v *viper.Viper) *Config {
return &Config{provider.NewConfig(flagSet, v)}
}
// Validate returns whether the config is valid, containing necessary values.
func (c *Config) Validate() error {
if err := c.Config.Validate(); err != nil {
return err
}
if c.RollbackCloneCmd() == "" {
return errors.New("missing rollback_clone_cmd")
}
if c.DatasetCloneDir() == "" {
return errors.New("missing dataset_clone_dir")
}
return nil
}