This repository was archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli_config_file_source.go
More file actions
44 lines (38 loc) · 1.55 KB
/
cli_config_file_source.go
File metadata and controls
44 lines (38 loc) · 1.55 KB
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
package configo
// CLIConfigFileSource registers a command line flag for specifying an optional json config file,
// beyond any other config file definitions that follow this one. It is intended to be used to provide an
// override to the regularly used config file(s), like when you might be debugging in production (admit it,
// you've been there too).
type CLIConfigFileSource struct {
flagName string
commandLine *CLISource
json Source
}
// FromDefaultCLIConfigFileSource registers a command line flag called "config" for specifying
// an alternate JSON config file.
func FromDefaultCLIConfigFileSource() *CLIConfigFileSource {
return FromCLIConfigFileSource("config")
}
// FromCLIConfigFileSource registers a command line flag with the given flagName for specifying
// an alternate JSON config file.
func FromCLIConfigFileSource(flagName string) *CLIConfigFileSource {
return &CLIConfigFileSource{
flagName: flagName,
commandLine: FromCLI(Flag(flagName, "The default configuration file path.")),
}
}
// Initialize parses the command line flag and reads the altnerate JSON source.
func (this *CLIConfigFileSource) Initialize() {
this.commandLine.Initialize()
path, err := this.commandLine.Strings(this.flagName)
if err == nil && len(path) > 0 {
this.json = FromOptionalJSONFile(path[0])
}
}
// Strings reads the key from the JSON source if it was successfully loaded during Initialize.
func (this *CLIConfigFileSource) Strings(key string) ([]string, error) {
if this.json == nil {
return nil, ErrKeyNotFound
}
return this.json.Strings(key)
}