| 1 | // The fmt command applies standard formatting to text proto files and preserves |
| 2 | // comments. |
| 3 | package mainscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/ |
| 4 | |
| 5 | import ( |
| 6 | "bufioscip-go gomod github.com/golang/go/src go1.18 bufio/" |
| 7 | "bytesscip-go gomod github.com/golang/go/src go1.18 bytes/" |
| 8 | "fmtscip-go gomod github.com/golang/go/src go1.18 fmt/" |
| 9 | "io/ioutilscip-go gomod github.com/golang/go/src go1.18 `io/ioutil`/" |
| 10 | "osscip-go gomod github.com/golang/go/src go1.18 os/" |
| 11 | "stringsscip-go gomod github.com/golang/go/src go1.18 strings/" |
| 12 | |
| 13 | "flagscip-go gomod github.com/golang/go/src go1.18 flag/" |
| 14 | // Google internal base/go package, commented out by copybara |
| 15 | logscip-go gomod github.com/golang/glog 23def4e6c14b `github.com/golang/glog`/ "github.com/golang/glogscip-go gomod github.com/golang/glog 23def4e6c14b `github.com/golang/glog`/" |
| 16 | "github.com/protocolbuffers/txtpbfmt/parserscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/parser`/" |
| 17 | ) |
| 18 | |
| 19 | var ( |
| 20 | // Top level flags. |
| 21 | dryRunscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/dryRun. = flagscip-go gomod github.com/golang/go/src go1.18 flag/.Boolscip-go gomod github.com/golang/go/src go1.18 flag/Bool().("dry_run", false, "Enable dry run mode.") |
| 22 | expandAllChildrenscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/expandAllChildren. = flagscip-go gomod github.com/golang/go/src go1.18 flag/.Boolscip-go gomod github.com/golang/go/src go1.18 flag/Bool().("expand_all_children", false, "Expand all children irrespective of initial state.") |
| 23 | skipAllColonsscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/skipAllColons. = flagscip-go gomod github.com/golang/go/src go1.18 flag/.Boolscip-go gomod github.com/golang/go/src go1.18 flag/Bool().("skip_all_colons", false, "Skip colons whenever possible.") |
| 24 | sortFieldsByFieldNamescip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/sortFieldsByFieldName. = flagscip-go gomod github.com/golang/go/src go1.18 flag/.Boolscip-go gomod github.com/golang/go/src go1.18 flag/Bool().("sort_fields_by_field_name", false, "Sort fields by field name.") |
| 25 | sortRepeatedFieldsByContentscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/sortRepeatedFieldsByContent. = flagscip-go gomod github.com/golang/go/src go1.18 flag/.Boolscip-go gomod github.com/golang/go/src go1.18 flag/Bool().("sort_repeated_fields_by_content", false, "Sort adjacent scalar fields of the same field name by their contents.") |
| 26 | sortRepeatedFieldsBySubfieldscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/sortRepeatedFieldsBySubfield. = flagscip-go gomod github.com/golang/go/src go1.18 flag/.Stringscip-go gomod github.com/golang/go/src go1.18 flag/String().("sort_repeated_fields_by_subfield", "", "Sort adjacent message fields of the given field name by the contents of the given subfield.") |
| 27 | removeDuplicateValuesForRepeatedFieldsscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/removeDuplicateValuesForRepeatedFields. = flagscip-go gomod github.com/golang/go/src go1.18 flag/.Boolscip-go gomod github.com/golang/go/src go1.18 flag/Bool().("remove_duplicate_values_for_repeated_fields", false, "Remove lines that have the same field name and scalar value as another.") |
| 28 | allowTripleQuotedStringsscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/allowTripleQuotedStrings. = flagscip-go gomod github.com/golang/go/src go1.18 flag/.Boolscip-go gomod github.com/golang/go/src go1.18 flag/Bool().("allow_triple_quoted_strings", false, `Allow Python-style """ or ''' delimited strings in input.`) |
| 29 | stdinDisplayPathscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/stdinDisplayPath. = flagscip-go gomod github.com/golang/go/src go1.18 flag/.Stringscip-go gomod github.com/golang/go/src go1.18 flag/String().("stdin_display_path", "<stdin>", "The path to display when referring to the content read from stdin.") |
| 30 | wrapStringsAtColumnscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/wrapStringsAtColumn. = flagscip-go gomod github.com/golang/go/src go1.18 flag/.Intscip-go gomod github.com/golang/go/src go1.18 flag/Int().("wrap_strings_at_column", 0, "Max columns for string field values. (0 means no wrap.)") |
| 31 | wrapHTMLStringsscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/wrapHTMLStrings. = flagscip-go gomod github.com/golang/go/src go1.18 flag/.Boolscip-go gomod github.com/golang/go/src go1.18 flag/Bool().("wrap_html_strings", false, "Wrap strings containing HTML tags. (Requires wrap_strings_at_column > 0.)") |
| 32 | wrapStringsAfterNewlinesscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/wrapStringsAfterNewlines. = flagscip-go gomod github.com/golang/go/src go1.18 flag/.Boolscip-go gomod github.com/golang/go/src go1.18 flag/Bool().("wrap_strings_after_newlines", false, "Wrap strings after newlines.") |
| 33 | wrapStringsWithoutWordwrapscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/wrapStringsWithoutWordwrap. = flagscip-go gomod github.com/golang/go/src go1.18 flag/.Boolscip-go gomod github.com/golang/go/src go1.18 flag/Bool().("wrap_strings_without_wordwrap", false, "Wrap strings at the given column only.") |
| 34 | preserveAngleBracketsscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/preserveAngleBrackets. = flagscip-go gomod github.com/golang/go/src go1.18 flag/.Boolscip-go gomod github.com/golang/go/src go1.18 flag/Bool().("preserve_angle_brackets", false, "Preserve angle brackets instead of converting to curly braces.") |
| 35 | smartQuotesscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/smartQuotes. = flagscip-go gomod github.com/golang/go/src go1.18 flag/.Boolscip-go gomod github.com/golang/go/src go1.18 flag/Bool().("smart_quotes", false, "Use single quotes around strings that contain double but not single quotes.") |
| 36 | ) |
| 37 | |
| 38 | const stdinPlaceholderPathscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/stdinPlaceholderPath. = "<stdin>" |
| 39 | |
| 40 | func readscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/read().(path string) ([]byte, error) { |
| 41 | if path == stdinPlaceholderPathscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/stdinPlaceholderPath. { |
| 42 | return ioutilscip-go gomod github.com/golang/go/src go1.18 `io/ioutil`/.ReadAllscip-go gomod github.com/golang/go/src go1.18 `io/ioutil`/ReadAll().(bufioscip-go gomod github.com/golang/go/src go1.18 bufio/.NewReaderscip-go gomod github.com/golang/go/src go1.18 bufio/NewReader().(osscip-go gomod github.com/golang/go/src go1.18 os/.Stdinscip-go gomod github.com/golang/go/src go1.18 os/Stdin.)) |
| 43 | } |
| 44 | return ioutilscip-go gomod github.com/golang/go/src go1.18 `io/ioutil`/.ReadFilescip-go gomod github.com/golang/go/src go1.18 `io/ioutil`/ReadFile().(path) |
| 45 | } |
| 46 | |
| 47 | func errorfscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/errorf().(format string, args ...interface{}) { |
| 48 | fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.18 fmt/Fprintf().(osscip-go gomod github.com/golang/go/src go1.18 os/.Stderrscip-go gomod github.com/golang/go/src go1.18 os/Stderr., format+"\n", args...) |
| 49 | } |
| 50 | |
| 51 | func contentForLoggingscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/contentForLogging().(content []byte) string { |
| 52 | res := string(content) |
| 53 | if len(res) > 100 { |
| 54 | res = res[:100] + " ... <snip> ..." |
| 55 | } |
| 56 | return res |
| 57 | } |
| 58 | |
| 59 | func mainscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/main().() { |
| 60 | flagscip-go gomod github.com/golang/go/src go1.18 flag/.Parsescip-go gomod github.com/golang/go/src go1.18 flag/Parse().() |
| 61 | paths := flagscip-go gomod github.com/golang/go/src go1.18 flag/.Argsscip-go gomod github.com/golang/go/src go1.18 flag/Args().() |
| 62 | if len(paths) == 0 { |
| 63 | paths = append(paths, stdinPlaceholderPathscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/stdinPlaceholderPath.) |
| 64 | } |
| 65 | logscip-go gomod github.com/golang/glog 23def4e6c14b `github.com/golang/glog`/.Infoscip-go gomod github.com/golang/glog 23def4e6c14b `github.com/golang/glog`/Info().("paths: ", paths) |
| 66 | errs := 0 |
| 67 | for _, path := range paths { |
| 68 | if stringsscip-go gomod github.com/golang/go/src go1.18 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.18 strings/HasPrefix().(path, "//depot/google3/") { |
| 69 | path = stringsscip-go gomod github.com/golang/go/src go1.18 strings/.Replacescip-go gomod github.com/golang/go/src go1.18 strings/Replace().(path, "//depot/google3/", "", 1) |
| 70 | } |
| 71 | displayPath := path |
| 72 | if path == stdinPlaceholderPathscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/stdinPlaceholderPath. { |
| 73 | displayPath = *stdinDisplayPathscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/stdinDisplayPath. |
| 74 | logscip-go gomod github.com/golang/glog 23def4e6c14b `github.com/golang/glog`/.Infoscip-go gomod github.com/golang/glog 23def4e6c14b `github.com/golang/glog`/Info().("path ", path, " displayed as ", displayPath) |
| 75 | } else { |
| 76 | logscip-go gomod github.com/golang/glog 23def4e6c14b `github.com/golang/glog`/.Infoscip-go gomod github.com/golang/glog 23def4e6c14b `github.com/golang/glog`/Info().("path ", path) |
| 77 | } |
| 78 | |
| 79 | content, err := readscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/read().(path) |
| 80 | if osscip-go gomod github.com/golang/go/src go1.18 os/.IsNotExistscip-go gomod github.com/golang/go/src go1.18 os/IsNotExist().(err) { |
| 81 | logscip-go gomod github.com/golang/glog 23def4e6c14b `github.com/golang/glog`/.Errorscip-go gomod github.com/golang/glog 23def4e6c14b `github.com/golang/glog`/Error().("Ignoring path: ", err) |
| 82 | errs++ |
| 83 | continue |
| 84 | } else if err != nil { |
| 85 | logscip-go gomod github.com/golang/glog 23def4e6c14b `github.com/golang/glog`/.Exitscip-go gomod github.com/golang/glog 23def4e6c14b `github.com/golang/glog`/Exit().(err) |
| 86 | } |
| 87 | |
| 88 | // Only pass the verbose logger if its level is enabled. |
| 89 | var logger parserscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/parser`/.Loggerscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/parser`/Logger# |
| 90 | if l := logscip-go gomod github.com/golang/glog 23def4e6c14b `github.com/golang/glog`/.Vscip-go gomod github.com/golang/glog 23def4e6c14b `github.com/golang/glog`/V().(2); l { |
| 91 | logger = l |
| 92 | } |
| 93 | newContent, err := parserscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/parser`/.FormatWithConfigscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/parser`/FormatWithConfig().(content, parserscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/parser`/.Configscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/parser`/Config#{ |
| 94 | ExpandAllChildrenscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/parser`/Config#ExpandAllChildren.: *expandAllChildrenscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/expandAllChildren., |
| 95 | SkipAllColonsscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/parser`/Config#SkipAllColons.: *skipAllColonsscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/skipAllColons., |
| 96 | SortFieldsByFieldNamescip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/parser`/Config#SortFieldsByFieldName.: *sortFieldsByFieldNamescip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/sortFieldsByFieldName., |
| 97 | SortRepeatedFieldsByContentscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/parser`/Config#SortRepeatedFieldsByContent.: *sortRepeatedFieldsByContentscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/sortRepeatedFieldsByContent., |
| 98 | SortRepeatedFieldsBySubfieldscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/parser`/Config#SortRepeatedFieldsBySubfield.: stringsscip-go gomod github.com/golang/go/src go1.18 strings/.Splitscip-go gomod github.com/golang/go/src go1.18 strings/Split().(*sortRepeatedFieldsBySubfieldscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/sortRepeatedFieldsBySubfield., ","), |
| 99 | RemoveDuplicateValuesForRepeatedFieldsscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/parser`/Config#RemoveDuplicateValuesForRepeatedFields.: *removeDuplicateValuesForRepeatedFieldsscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/removeDuplicateValuesForRepeatedFields., |
| 100 | AllowTripleQuotedStringsscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/parser`/Config#AllowTripleQuotedStrings.: *allowTripleQuotedStringsscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/allowTripleQuotedStrings., |
| 101 | WrapStringsAtColumnscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/parser`/Config#WrapStringsAtColumn.: *wrapStringsAtColumnscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/wrapStringsAtColumn., |
| 102 | WrapHTMLStringsscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/parser`/Config#WrapHTMLStrings.: *wrapHTMLStringsscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/wrapHTMLStrings., |
| 103 | WrapStringsAfterNewlinesscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/parser`/Config#WrapStringsAfterNewlines.: *wrapStringsAfterNewlinesscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/wrapStringsAfterNewlines., |
| 104 | WrapStringsWithoutWordwrapscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/parser`/Config#WrapStringsWithoutWordwrap.: *wrapStringsWithoutWordwrapscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/wrapStringsWithoutWordwrap., |
| 105 | PreserveAngleBracketsscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/parser`/Config#PreserveAngleBrackets.: *preserveAngleBracketsscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/preserveAngleBrackets., |
| 106 | SmartQuotesscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/parser`/Config#SmartQuotes.: *smartQuotesscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/smartQuotes., |
| 107 | Loggerscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/parser`/Config#Logger.: logger, |
| 108 | }) |
| 109 | if err != nil { |
| 110 | errorfscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/errorf().("parser.Format for path %v with content %q returned err %v", displayPath, contentForLoggingscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/contentForLogging().(content), err) |
| 111 | errs++ |
| 112 | continue |
| 113 | } |
| 114 | logscip-go gomod github.com/golang/glog 23def4e6c14b `github.com/golang/glog`/.Vscip-go gomod github.com/golang/glog 23def4e6c14b `github.com/golang/glog`/V().(2).Infofscip-go gomod github.com/golang/glog 23def4e6c14b `github.com/golang/glog`/Verbose#Infof().("New content for path %s: %q", displayPath, newContent) |
| 115 | |
| 116 | if path == stdinPlaceholderPathscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/stdinPlaceholderPath. { |
| 117 | fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Printscip-go gomod github.com/golang/go/src go1.18 fmt/Print().(string(newContent)) |
| 118 | continue |
| 119 | } |
| 120 | if bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.Equalscip-go gomod github.com/golang/go/src go1.18 bytes/Equal().(content, newContent) { |
| 121 | logscip-go gomod github.com/golang/glog 23def4e6c14b `github.com/golang/glog`/.Infoscip-go gomod github.com/golang/glog 23def4e6c14b `github.com/golang/glog`/Info().("No change for path ", displayPath) |
| 122 | continue |
| 123 | } |
| 124 | if *dryRunscip-go gomod github.com/protocolbuffers/txtpbfmt . `github.com/protocolbuffers/txtpbfmt/cmd/txtpbfmt`/dryRun. { |
| 125 | fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Printlnscip-go gomod github.com/golang/go/src go1.18 fmt/Println().(string(newContent)) |
| 126 | continue |
| 127 | } |
| 128 | if err := ioutilscip-go gomod github.com/golang/go/src go1.18 `io/ioutil`/.WriteFilescip-go gomod github.com/golang/go/src go1.18 `io/ioutil`/WriteFile().(path, newContent, 0664); err != nil { |
| 129 | logscip-go gomod github.com/golang/glog 23def4e6c14b `github.com/golang/glog`/.Exitscip-go gomod github.com/golang/glog 23def4e6c14b `github.com/golang/glog`/Exit().(err) |
| 130 | } |
| 131 | } |
| 132 | if errs > 0 { |
| 133 | logscip-go gomod github.com/golang/glog 23def4e6c14b `github.com/golang/glog`/.Exitscip-go gomod github.com/golang/glog 23def4e6c14b `github.com/golang/glog`/Exit().(errs, " error(s) encountered during execution") |
| 134 | } |
| 135 | } |