sanitize.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package config
  2. import (
  3. log "github.com/sirupsen/logrus"
  4. )
  5. // Sanitize will look for common configuration issues, and fix them. For example,
  6. // populating undefined fields - name -> title, etc.
  7. func (cfg *Config) Sanitize() {
  8. cfg.sanitizeLogLevel()
  9. // log.Infof("cfg %p", cfg)
  10. for idx := range cfg.Actions {
  11. cfg.Actions[idx].sanitize()
  12. }
  13. }
  14. func (cfg *Config) sanitizeLogLevel() {
  15. if logLevel, err := log.ParseLevel(cfg.LogLevel); err == nil {
  16. log.Info("Setting log level to ", logLevel)
  17. log.SetLevel(logLevel)
  18. }
  19. }
  20. func (action *Action) sanitize() {
  21. if action.Timeout < 3 {
  22. action.Timeout = 3
  23. }
  24. action.Icon = lookupHTMLIcon(action.Icon)
  25. for idx := range action.Arguments {
  26. action.Arguments[idx].sanitize()
  27. }
  28. }
  29. func (arg *ActionArgument) sanitize() {
  30. if arg.Title == "" {
  31. arg.Title = arg.Name
  32. }
  33. for idx, choice := range arg.Choices {
  34. if choice.Title == "" {
  35. arg.Choices[idx].Title = choice.Value
  36. }
  37. }
  38. arg.sanitizeNoType()
  39. // TODO Validate the default against the type checker, but this creates a
  40. // import loop
  41. }
  42. func (arg *ActionArgument) sanitizeNoType() {
  43. if len(arg.Choices) == 0 && arg.Type == "" {
  44. log.WithFields(log.Fields{
  45. "arg": arg.Name,
  46. }).Warn("Argument type isn't set, will default to 'ascii' but this may not be safe. You should set a type specifically.")
  47. arg.Type = "ascii"
  48. }
  49. }