Skip to content

Commit

Permalink
Allow <stdin> to be called something else in log/error messages
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 515064137
  • Loading branch information
txtpbfmt-copybara-robot committed Mar 8, 2023
1 parent 9485b87 commit 0e3c6a0
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions cmd/txtpbfmt/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ var (
skipAllColons = flag.Bool("skip_all_colons", false, "Skip colons whenever possible.")
allowTripleQuotedStrings = flag.Bool("allow_triple_quoted_strings", false,
`Allow Python-style """ or ''' delimited strings in input.`)
stdinDisplayPath = flag.String("stdin_display_path", "<stdin>",
"The path to display when referring to the content read from stdin.")
)

const stdinPath = "<stdin>"
const stdinPlaceholderPath = "<stdin>"

func read(path string) ([]byte, error) {
if path == stdinPath {
if path == stdinPlaceholderPath {
return ioutil.ReadAll(bufio.NewReader(os.Stdin))
}
return ioutil.ReadFile(path)
Expand All @@ -50,15 +52,22 @@ func main() {
flag.Parse()
paths := flag.Args()
if len(paths) == 0 {
paths = append(paths, stdinPath)
paths = append(paths, stdinPlaceholderPath)
}
log.Info("paths: ", paths)
errs := 0
for _, path := range paths {
if strings.HasPrefix(path, "//depot/google3/") {
path = strings.Replace(path, "//depot/google3/", "", 1)
}
log.Info("path ", path)
displayPath := path
if path == stdinPlaceholderPath {
displayPath = *stdinDisplayPath
log.Info("path ", path, " displayed as ", displayPath)
} else {
log.Info("path ", path)
}

content, err := read(path)
if os.IsNotExist(err) {
log.Error("Ignoring path: ", err)
Expand All @@ -80,18 +89,18 @@ func main() {
Logger: logger,
})
if err != nil {
errorf("parser.Format for path %v with content %q returned err %v", path, contentForLogging(content), err)
errorf("parser.Format for path %v with content %q returned err %v", displayPath, contentForLogging(content), err)
errs++
continue
}
log.V(2).Infof("New content for path %s: %q", path, newContent)
log.V(2).Infof("New content for path %s: %q", displayPath, newContent)

if path == stdinPath {
if path == stdinPlaceholderPath {
fmt.Print(string(newContent))
continue
}
if bytes.Equal(content, newContent) {
log.Info("No change for path ", path)
log.Info("No change for path ", displayPath)
continue
}
if *dryRun {
Expand Down

0 comments on commit 0e3c6a0

Please sign in to comment.