protofmt.go 2.4 KB

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