sanitize.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. if action.MaxConcurrent < 1 {
  26. action.MaxConcurrent = 1
  27. }
  28. for idx := range action.Arguments {
  29. action.Arguments[idx].sanitize()
  30. }
  31. }
  32. func (arg *ActionArgument) sanitize() {
  33. if arg.Title == "" {
  34. arg.Title = arg.Name
  35. }
  36. for idx, choice := range arg.Choices {
  37. if choice.Title == "" {
  38. arg.Choices[idx].Title = choice.Value
  39. }
  40. }
  41. arg.sanitizeNoType()
  42. // TODO Validate the default against the type checker, but this creates a
  43. // import loop
  44. }
  45. func (arg *ActionArgument) sanitizeNoType() {
  46. if len(arg.Choices) == 0 && arg.Type == "" {
  47. log.WithFields(log.Fields{
  48. "arg": arg.Name,
  49. }).Warn("Argument type isn't set, will default to 'ascii' but this may not be safe. You should set a type specifically.")
  50. arg.Type = "ascii"
  51. }
  52. }