Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(275)

Side by Side Diff: mojom/mojom_parser/fmt_cmd.go

Issue 1702873002: Mojom formatter incorporated in the mojom binary. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Update sha1 Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // This file contains fmtCmd which formats a .mojom file and outputs the
6 // formatted mojom file to stdout.
7 // The fmt command is invoked as follows:
8 //
9 // mojom fmt [-w] <mojom_file>
10 //
11 // If -w is specified and the formatted file is different from the original ,
12 // the file is overwritten with the formatted version.
13 package main
14
15 import (
16 "flag"
17 "fmt"
18 "io/ioutil"
19 "mojom/mojom_parser/formatter"
20 "os"
21 "path/filepath"
22 )
23
24 // fmtCmd implements the fmt command for the mojom tool.
25 // The slice of strings |args| is the list of arguments passed on the command
26 // line starting with the program name and followed by the invoked command.
27 func fmtCmd(args []string) {
28 flagSet := flag.NewFlagSet("fmt", flag.ContinueOnError)
29 var overwrite bool
30 flagSet.BoolVar(&overwrite, "w", false,
31 "Overwrite the specified file with the formatted version if it i s different from the original.")
32
33 printUsage := func() {
34 fmt.Fprintf(os.Stderr, "Usage: %s fmt [-w] <mojom_file>\n\n", fi lepath.Base(args[0]))
35 flagSet.PrintDefaults()
36 }
37
38 if err := flagSet.Parse(args[2:]); err != nil {
39 fmt.Fprintln(os.Stderr, err.Error())
40 os.Exit(1)
41 }
42
43 inputFileName := flagSet.Arg(0)
44 if inputFileName == "" {
45 fmt.Fprintln(os.Stderr, "No .mojom file given.")
46 printUsage()
47 os.Exit(1)
48 }
49
50 originalBytes, err := ioutil.ReadFile(inputFileName)
51 if err != nil {
52 ErrorExit(err.Error())
53 }
54
55 original := string(originalBytes[:])
56
57 var formatted string
58 formatted, err = formatter.FormatMojom(inputFileName, original)
59 if err != nil {
60 ErrorExit(err.Error())
61 }
62
63 if overwrite {
64 if formatted != original {
65 if err := ioutil.WriteFile(inputFileName, []byte(formatt ed), 0); err != nil {
66 ErrorExit(err.Error())
67 }
68 }
69 } else {
70 fmt.Print(formatted)
71 }
72 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698