/config.go
Go | 39 lines | 33 code | 6 blank | 0 comment | 4 complexity | d19e24249c8cc93549db44dde84afb32 MD5 | raw file
1package main 2 3import( 4 "os" 5 "path/filepath" 6 "io/ioutil" 7 "encoding/json" 8) 9 10type Config map[string]string 11 12func (c *Config) Set(key string, val string) { 13 (*c)[key] = val 14} 15 16func (c *Config) Is(key string) string { 17 if val, exists := (*c)[key]; exists { 18 return val 19 } 20 return "" 21} 22 23func (c *Config) Get(key string) string { 24 if val, exists := (*c)[key]; exists { 25 return val 26 } 27 return "" 28} 29 30func loadConfig() (config *Config) { 31 root, _ := filepath.Split(filepath.Clean(os.Args[0])) 32 b, err := ioutil.ReadFile(filepath.Join(root, "config.json")) 33 if err != nil { 34 panic(err) 35 return &Config{} 36 } 37 err = json.Unmarshal(b, &config) 38 return 39}