/config.go

http://github.com/TheOnly92/bloody.go · Go · 39 lines · 33 code · 6 blank · 0 comment · 4 complexity · d19e24249c8cc93549db44dde84afb32 MD5 · raw file

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