protofmt.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (C) 2016 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. //go:build ignore
  7. // +build ignore
  8. package main
  9. import (
  10. "bufio"
  11. "flag"
  12. "fmt"
  13. "io"
  14. "log"
  15. "os"
  16. "path/filepath"
  17. "regexp"
  18. "strings"
  19. "text/tabwriter"
  20. )
  21. func main() {
  22. flag.Parse()
  23. for _, arg := range flag.Args() {
  24. matches, err := filepath.Glob(arg)
  25. if err != nil {
  26. log.Fatal(err)
  27. }
  28. for _, file := range matches {
  29. if stat, err := os.Stat(file); err != nil {
  30. log.Fatal(err)
  31. } else if stat.IsDir() {
  32. err := filepath.Walk(file, func(path string, info os.FileInfo, err error) error {
  33. if err != nil {
  34. return err
  35. }
  36. if filepath.Ext(path) == ".proto" {
  37. return formatProtoFile(path)
  38. }
  39. return nil
  40. })
  41. if err != nil {
  42. log.Fatal(err)
  43. }
  44. } else {
  45. if err := formatProtoFile(file); err != nil {
  46. log.Fatal(err)
  47. }
  48. }
  49. }
  50. }
  51. }
  52. func formatProtoFile(file string) error {
  53. log.Println("Formatting", file)
  54. in, err := os.Open(file)
  55. if err != nil {
  56. return err
  57. }
  58. defer in.Close()
  59. out, err := os.Create(file + ".tmp")
  60. if err != nil {
  61. return err
  62. }
  63. defer out.Close()
  64. if err := formatProto(in, out); err != nil {
  65. return err
  66. }
  67. in.Close()
  68. out.Close()
  69. return os.Rename(file+".tmp", file)
  70. }
  71. func formatProto(in io.Reader, out io.Writer) error {
  72. sc := bufio.NewScanner(in)
  73. lineExp := regexp.MustCompile(`([^=]+)\s+([^=\s]+?)\s*=(.+)`)
  74. var tw *tabwriter.Writer
  75. for sc.Scan() {
  76. line := sc.Text()
  77. if strings.HasPrefix(line, "//") {
  78. if _, err := fmt.Fprintln(out, line); err != nil {
  79. return err
  80. }
  81. continue
  82. }
  83. ms := lineExp.FindStringSubmatch(line)
  84. for i := range ms {
  85. ms[i] = strings.TrimSpace(ms[i])
  86. }
  87. if len(ms) == 4 && ms[1] != "option" {
  88. typ := strings.Join(strings.Fields(ms[1]), " ")
  89. name := ms[2]
  90. id := ms[3]
  91. if tw == nil {
  92. tw = tabwriter.NewWriter(out, 4, 4, 1, ' ', 0)
  93. }
  94. if typ == "" {
  95. // We're in an enum
  96. fmt.Fprintf(tw, "\t%s\t= %s\n", name, id)
  97. } else {
  98. // Message
  99. fmt.Fprintf(tw, "\t%s\t%s\t= %s\n", typ, name, id)
  100. }
  101. } else {
  102. if tw != nil {
  103. if err := tw.Flush(); err != nil {
  104. return err
  105. }
  106. tw = nil
  107. }
  108. if _, err := fmt.Fprintln(out, line); err != nil {
  109. return err
  110. }
  111. }
  112. }
  113. return nil
  114. }