forked from dhamidi/leader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_config_file.go
43 lines (36 loc) · 1010 Bytes
/
load_config_file.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
package main
import "io"
// LoadConfigFile loads a configuration file in JSON format and
// applies its contents to the current key map in the application
// context.
type LoadConfigFile struct {
*Context
Filename string
}
// NewLoadConfigFile creates a command for loading the configuration stored in filename.
func NewLoadConfigFile(context *Context, filename string) *LoadConfigFile {
return &LoadConfigFile{
Context: context,
Filename: filename,
}
}
// Execute implements Command by parsing the file named by filename as
// JSON and applying the configuration settings stored within to the
// current keymap.
func (cmd *LoadConfigFile) Execute() error {
configFile, err := cmd.Files.Open(cmd.Filename)
if err != nil {
return err
}
defer func() {
if closer, ok := configFile.(io.Closer); ok {
closer.Close()
}
}()
config := NewConfig()
if err := config.ParseJSON(configFile); err != nil {
return err
}
config.MergeIntoKeyMap(cmd.Context, cmd.CurrentKeyMap)
return nil
}