tomllint.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package tomllint
  2. import (
  3. "io"
  4. "regexp"
  5. "strconv"
  6. "github.com/BurntSushi/toml"
  7. "go-common/app/admin/main/config/pkg/lint"
  8. )
  9. var lineNumberRe *regexp.Regexp
  10. const filetype = "toml"
  11. type lintFn func(metadata toml.MetaData) []lint.LineErr
  12. var lintFns []lintFn
  13. type tomllint struct{}
  14. // Lint toml file return lint.Error
  15. func (tomllint) Lint(r io.Reader) lint.Error {
  16. var v interface{}
  17. var lintErr lint.Error
  18. metadata, err := toml.DecodeReader(r, &v)
  19. if err != nil {
  20. line := -1
  21. if match := lineNumberRe.FindStringSubmatch(err.Error()); len(match) == 2 {
  22. line, _ = strconv.Atoi(match[1])
  23. }
  24. lintErr = append(lintErr, lint.LineErr{Line: line, Message: err.Error()})
  25. return lintErr
  26. }
  27. for _, fn := range lintFns {
  28. if lineErrs := fn(metadata); lineErrs != nil {
  29. lintErr = append(lintErr, lineErrs...)
  30. }
  31. }
  32. if len(lintErr) == 0 {
  33. return nil
  34. }
  35. return lintErr
  36. }
  37. // not allowed defined kv that type is not Hash at top level
  38. //func noTopKV(metadata toml.MetaData) []lint.LineErr {
  39. // var lineErrs []lint.LineErr
  40. // for _, keys := range metadata.Keys() {
  41. // if len(keys) != 1 {
  42. // continue
  43. // }
  44. // typeName := metadata.Type(keys...)
  45. // if typeName != "Hash" {
  46. // lineErrs = append(lineErrs, lint.LineErr{
  47. // Line: -1,
  48. // Message: fmt.Sprintf("top level value must be Object, key: %s type is %s", keys[0], typeName),
  49. // })
  50. // }
  51. // }
  52. // return lineErrs
  53. //}
  54. // noApp not allowed app section exists
  55. func noApp(metadata toml.MetaData) []lint.LineErr {
  56. if metadata.IsDefined("app") {
  57. return []lint.LineErr{{Line: -1, Message: "请删除无用 App 配置 see: http://git.bilibili.co/platform/go-common/issues/310 (゜-゜)つロ"}}
  58. }
  59. return nil
  60. }
  61. // noIdentify not allowed identify config
  62. func noIdentify(metadata toml.MetaData) []lint.LineErr {
  63. if metadata.IsDefined("identify") {
  64. return []lint.LineErr{{Line: -1, Message: "请删除无用 Identify 配置 see: http://git.bilibili.co/platform/go-common/issues/310 (゜-゜)つロ"}}
  65. }
  66. return nil
  67. }
  68. // noCommon not allowed common config
  69. func noCommon(metadata toml.MetaData) []lint.LineErr {
  70. count := 0
  71. commonKey := []string{"version", "user", "pid", "dir", "perf"}
  72. for _, key := range commonKey {
  73. if metadata.IsDefined(key) {
  74. count++
  75. }
  76. }
  77. if count > 0 {
  78. return []lint.LineErr{{Line: -1, Message: "请删除无用 Common 配置 see: http://git.bilibili.co/platform/go-common/issues/310 (゜-゜)つロ"}}
  79. }
  80. return nil
  81. }
  82. func init() {
  83. lint.RegisterLinter(filetype, tomllint{})
  84. lintFns = []lintFn{noApp, noIdentify, noCommon}
  85. lineNumberRe = regexp.MustCompile("^Near line (\\d+)")
  86. }