console.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package csconfig
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. log "github.com/sirupsen/logrus"
  7. "gopkg.in/yaml.v2"
  8. )
  9. const (
  10. SEND_CUSTOM_SCENARIOS = "custom"
  11. SEND_TAINTED_SCENARIOS = "tainted"
  12. SEND_MANUAL_SCENARIOS = "manual"
  13. SEND_LIVE_DECISIONS = "live_decisions"
  14. )
  15. var CONSOLE_CONFIGS = []string{SEND_CUSTOM_SCENARIOS, SEND_LIVE_DECISIONS, SEND_MANUAL_SCENARIOS, SEND_TAINTED_SCENARIOS}
  16. type ConsoleConfig struct {
  17. ShareManualDecisions *bool `yaml:"share_manual_decisions"`
  18. ShareTaintedScenarios *bool `yaml:"share_custom"`
  19. ShareCustomScenarios *bool `yaml:"share_tainted"`
  20. ShareDecisions *bool `yaml:"share_decisions"`
  21. }
  22. func (c *LocalApiServerCfg) LoadConsoleConfig() error {
  23. c.ConsoleConfig = &ConsoleConfig{}
  24. if _, err := os.Stat(c.ConsoleConfigPath); err != nil && os.IsNotExist(err) {
  25. log.Debugf("no console configuration to load")
  26. return nil
  27. }
  28. yamlFile, err := ioutil.ReadFile(c.ConsoleConfigPath)
  29. if err != nil {
  30. return fmt.Errorf("reading console config file '%s': %s", c.ConsoleConfigPath, err)
  31. }
  32. err = yaml.Unmarshal(yamlFile, c.ConsoleConfig)
  33. if err != nil {
  34. return fmt.Errorf("unmarshaling console config file '%s': %s", c.ConsoleConfigPath, err)
  35. }
  36. if c.ConsoleConfig.ShareCustomScenarios == nil {
  37. log.Debugf("no share_custom scenarios found, setting to false")
  38. c.ConsoleConfig.ShareCustomScenarios = new(bool)
  39. }
  40. if c.ConsoleConfig.ShareTaintedScenarios == nil {
  41. log.Debugf("no share_tainted scenarios found, setting to false")
  42. c.ConsoleConfig.ShareTaintedScenarios = new(bool)
  43. }
  44. if c.ConsoleConfig.ShareManualDecisions == nil {
  45. log.Debugf("no share_manual scenarios found, setting to false")
  46. c.ConsoleConfig.ShareManualDecisions = new(bool)
  47. }
  48. if c.ConsoleConfig.ShareDecisions == nil {
  49. log.Debugf("no share_decisions scenarios found, setting to false")
  50. c.ConsoleConfig.ShareDecisions = new(bool)
  51. }
  52. log.Infof("Console configuration '%s' loaded successfully", c.ConsoleConfigPath)
  53. return nil
  54. }