config.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package config
  11. import (
  12. "fmt"
  13. "strconv"
  14. "devt.de/krotik/common/errorutil"
  15. )
  16. // Global variables
  17. // ================
  18. /*
  19. ProductVersion is the current version of ECAL
  20. */
  21. const ProductVersion = "1.0.0"
  22. /*
  23. Known configuration options for ECAL
  24. */
  25. const (
  26. WorkerCount = "WorkerCount"
  27. )
  28. /*
  29. DefaultConfig is the defaut configuration
  30. */
  31. var DefaultConfig = map[string]interface{}{
  32. WorkerCount: 1,
  33. }
  34. /*
  35. Config is the actual config which is used
  36. */
  37. var Config map[string]interface{}
  38. /*
  39. Initialise the config
  40. */
  41. func init() {
  42. data := make(map[string]interface{})
  43. for k, v := range DefaultConfig {
  44. data[k] = v
  45. }
  46. Config = data
  47. }
  48. // Helper functions
  49. // ================
  50. /*
  51. Str reads a config value as a string value.
  52. */
  53. func Str(key string) string {
  54. return fmt.Sprint(Config[key])
  55. }
  56. /*
  57. Int reads a config value as an int value.
  58. */
  59. func Int(key string) int {
  60. ret, err := strconv.ParseInt(fmt.Sprint(Config[key]), 10, 64)
  61. errorutil.AssertTrue(err == nil,
  62. fmt.Sprintf("Could not parse config key %v: %v", key, err))
  63. return int(ret)
  64. }
  65. /*
  66. Bool reads a config value as a boolean value.
  67. */
  68. func Bool(key string) bool {
  69. ret, err := strconv.ParseBool(fmt.Sprint(Config[key]))
  70. errorutil.AssertTrue(err == nil,
  71. fmt.Sprintf("Could not parse config key %v: %v", key, err))
  72. return ret
  73. }