config.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package config
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "strings"
  7. )
  8. var (
  9. OpenRegistration = false
  10. DataHome = "/usr/local/amuse"
  11. Port uint = 5008
  12. Address = "127.0.0.1"
  13. TmdbApiKey = ""
  14. )
  15. func ReadConfig(path string) error {
  16. file, err := os.Open(path)
  17. if err != nil {
  18. if os.IsNotExist(err) {
  19. return nil
  20. } else {
  21. fmt.Printf("error opening configuration %v\n", err)
  22. return err
  23. }
  24. }
  25. defer file.Close()
  26. scanner := bufio.NewScanner(file)
  27. for scanner.Scan() {
  28. line := scanner.Text()
  29. assignment := strings.Split(line, "=")
  30. variable := strings.Trim(assignment[0], " ")
  31. value := strings.Trim(assignment[1], " ")
  32. switch variable {
  33. case "OpenRegistration":
  34. OpenRegistration = value == "true"
  35. case "DataHome":
  36. DataHome = value
  37. case "Address":
  38. Address = value
  39. case "TmdbApiKey":
  40. TmdbApiKey = value
  41. case "Port":
  42. fmt.Sscanf(value, "%d", &Port)
  43. }
  44. }
  45. if err := scanner.Err(); err != nil {
  46. fmt.Printf("error reading configuration %v\n", err)
  47. return err
  48. }
  49. return nil
  50. }