2016-07-04 12:55:17 +00:00
|
|
|
// Copyright (C) 2016 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2016-07-04 12:55:17 +00:00
|
|
|
|
|
|
|
// +build ignore
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
2020-08-25 06:11:14 +00:00
|
|
|
"path/filepath"
|
2016-07-04 10:40:29 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
2020-08-25 06:11:14 +00:00
|
|
|
for _, arg := range flag.Args() {
|
|
|
|
matches, err := filepath.Glob(arg)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
for _, file := range matches {
|
|
|
|
if stat, err := os.Stat(file); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
} else if stat.IsDir() {
|
|
|
|
err := filepath.Walk(file, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if filepath.Ext(path) == ".proto" {
|
|
|
|
return formatProtoFile(path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := formatProtoFile(file); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatProtoFile(file string) error {
|
|
|
|
log.Println("Formatting", file)
|
2016-07-04 10:40:29 +00:00
|
|
|
in, err := os.Open(file)
|
|
|
|
if err != nil {
|
2020-08-25 06:11:14 +00:00
|
|
|
return err
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
2020-08-25 06:11:14 +00:00
|
|
|
defer in.Close()
|
2016-07-04 10:40:29 +00:00
|
|
|
out, err := os.Create(file + ".tmp")
|
|
|
|
if err != nil {
|
2020-08-25 06:11:14 +00:00
|
|
|
return err
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
2020-08-25 06:11:14 +00:00
|
|
|
defer out.Close()
|
2016-07-04 10:40:29 +00:00
|
|
|
if err := formatProto(in, out); err != nil {
|
2020-08-25 06:11:14 +00:00
|
|
|
return err
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
2016-11-03 22:06:51 +00:00
|
|
|
in.Close()
|
|
|
|
out.Close()
|
2020-08-25 06:11:14 +00:00
|
|
|
return os.Rename(file+".tmp", file)
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func formatProto(in io.Reader, out io.Writer) error {
|
|
|
|
sc := bufio.NewScanner(in)
|
|
|
|
lineExp := regexp.MustCompile(`([^=]+)\s+([^=\s]+?)\s*=(.+)`)
|
|
|
|
var tw *tabwriter.Writer
|
|
|
|
for sc.Scan() {
|
|
|
|
line := sc.Text()
|
|
|
|
if strings.HasPrefix(line, "//") {
|
|
|
|
if _, err := fmt.Fprintln(out, line); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ms := lineExp.FindStringSubmatch(line)
|
|
|
|
for i := range ms {
|
|
|
|
ms[i] = strings.TrimSpace(ms[i])
|
|
|
|
}
|
|
|
|
if len(ms) == 4 && ms[1] != "option" {
|
|
|
|
typ := strings.Join(strings.Fields(ms[1]), " ")
|
|
|
|
name := ms[2]
|
|
|
|
id := ms[3]
|
|
|
|
if tw == nil {
|
|
|
|
tw = tabwriter.NewWriter(out, 4, 4, 1, ' ', 0)
|
|
|
|
}
|
|
|
|
if typ == "" {
|
|
|
|
// We're in an enum
|
|
|
|
fmt.Fprintf(tw, "\t%s\t= %s\n", name, id)
|
|
|
|
} else {
|
|
|
|
// Message
|
|
|
|
fmt.Fprintf(tw, "\t%s\t%s\t= %s\n", typ, name, id)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if tw != nil {
|
|
|
|
if err := tw.Flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tw = nil
|
|
|
|
}
|
|
|
|
if _, err := fmt.Fprintln(out, line); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|