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

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: 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_cmd", flag.PanicOnError)
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 printUsage()
41 os.Exit(1)
42 }
43
44 inputFileName := flagSet.Arg(0)
45 if inputFileName == "" {
46 fmt.Fprintln(os.Stderr, "No .mojom file given.")
47 printUsage()
48 os.Exit(1)
49 }
50
51 originalBytes, err := ioutil.ReadFile(inputFileName)
52 if err != nil {
53 ErrorExit(err.Error())
54 }
55
56 original := string(originalBytes[:])
57
58 var formatted string
59 formatted, err = formatter.FormatMojom(inputFileName, original)
60 if err != nil {
61 ErrorExit(err.Error())
62 }
63
64 if overwrite {
65 if formatted != original {
66 if err := ioutil.WriteFile(inputFileName, []byte(formatt ed), 0); err != nil {
67 ErrorExit(err.Error())
68 }
69 }
70 } else {
71 fmt.Print(formatted)
72 }
73 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698