forked from cloudfoundry/bosh-bootloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration_parser.go
62 lines (49 loc) · 1.6 KB
/
configuration_parser.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
package application
import "github.com/cloudfoundry/bosh-bootloader/storage"
var getState func(string) (storage.State, error) = storage.GetState
type commandLineParser interface {
Parse(arguments []string) (CommandLineConfiguration, error)
}
type stateStore interface {
Set(state storage.State) error
}
type ConfigurationParser struct {
commandLineParser commandLineParser
}
func NewConfigurationParser(commandLineParser commandLineParser) ConfigurationParser {
return ConfigurationParser{
commandLineParser: commandLineParser,
}
}
func (p ConfigurationParser) Parse(arguments []string) (Configuration, error) {
commandLineConfiguration, err := p.commandLineParser.Parse(arguments)
if err != nil {
return Configuration{}, err
}
configuration := Configuration{
Global: GlobalConfiguration{
StateDir: commandLineConfiguration.StateDir,
EndpointOverride: commandLineConfiguration.EndpointOverride,
Debug: commandLineConfiguration.Debug,
},
Command: commandLineConfiguration.Command,
SubcommandFlags: commandLineConfiguration.SubcommandFlags,
State: storage.State{},
}
if !p.isHelpOrVersion(configuration.Command, configuration.SubcommandFlags) {
configuration.State, err = getState(configuration.Global.StateDir)
if err != nil {
return Configuration{}, err
}
}
return configuration, nil
}
func (ConfigurationParser) isHelpOrVersion(command string, subcommandFlags StringSlice) bool {
if command == "help" || command == "version" {
return true
}
if subcommandFlags.ContainsAny("--help", "-h", "--version", "-v") {
return true
}
return false
}