-
Notifications
You must be signed in to change notification settings - Fork 5
/
config.go
61 lines (49 loc) 路 1.14 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
package main
import (
"encoding/json"
"io"
"io/ioutil"
"os"
"github.com/golang/glog"
)
// Contents contains the contents of the parsed config file.
type Contents struct {
AccessToken string `json:"access_token"`
SearchType string `json:"search_type"`
Query string `json:"query"`
ExternalCmdStr string `json:"external_command"`
}
// Config specifies information about the config file used for performing the experiment.
type Config struct {
FilePath string
Contents *Contents
}
// NewCfg is a constructor that returns a pointer to a Config object.
func NewCfg(fp string) *Config {
return &Config{
FilePath: fp,
}
}
// Parse opens a file and calls a private `parse` method.
func (cfg *Config) Parse() {
f, err := os.Open(cfg.FilePath)
if err != nil {
glog.Errorf("error opening config file %+v", err)
return
}
defer f.Close()
c := &Contents{}
if err := parse(f, c); err != nil {
glog.Errorf("error parsing config file %+v", err)
return
}
cfg.Contents = c
return
}
func parse(f io.Reader, d interface{}) error {
b, err := ioutil.ReadAll(f)
if err = json.Unmarshal(b, d); err != nil {
return err
}
return nil
}