Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(85)

Side by Side Diff: src/cmd/gofmt/gofmt.go

Issue 163085: code review 163085: move ReadFile, WriteFile, and ReadDir into a separate i... (Closed)
Patch Set: code review 163085: move ReadFile, WriteFile, and ReadDir into a separate i... Created 15 years, 4 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/cmd/godoc/godoc.go ('k') | src/cmd/hgpatch/main.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2009 The Go Authors. All rights reserved. 1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 package main 5 package main
6 6
7 import ( 7 import (
8 "bytes"; 8 "bytes";
9 "flag"; 9 "flag";
10 "fmt"; 10 "fmt";
11 "go/ast"; 11 "go/ast";
12 "go/parser"; 12 "go/parser";
13 "go/printer"; 13 "go/printer";
14 "go/scanner"; 14 "go/scanner";
15 » "io"; 15 » "io/ioutil";
16 "os"; 16 "os";
17 pathutil "path"; 17 pathutil "path";
18 "strings"; 18 "strings";
19 ) 19 )
20 20
21 21
22 var ( 22 var (
23 // main operation modes 23 // main operation modes
24 list = flag.Bool("l", false, "list files whose formatting dif fers from gofmt's"); 24 list = flag.Bool("l", false, "list files whose formatting dif fers from gofmt's");
25 write = flag.Bool("w", false, "write result to (source) file i nstead of stdout"); 25 write = flag.Bool("w", false, "write result to (source) file i nstead of stdout");
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 } 79 }
80 80
81 81
82 func isGoFile(d *os.Dir) bool { 82 func isGoFile(d *os.Dir) bool {
83 // ignore non-Go files 83 // ignore non-Go files
84 return d.IsRegular() && !strings.HasPrefix(d.Name, ".") && strings.HasSu ffix(d.Name, ".go") 84 return d.IsRegular() && !strings.HasPrefix(d.Name, ".") && strings.HasSu ffix(d.Name, ".go")
85 } 85 }
86 86
87 87
88 func processFile(f *os.File) os.Error { 88 func processFile(f *os.File) os.Error {
89 » src, err := io.ReadAll(f); 89 » src, err := ioutil.ReadAll(f);
90 if err != nil { 90 if err != nil {
91 return err 91 return err
92 } 92 }
93 93
94 file, err := parser.ParseFile(f.Name(), src, parserMode); 94 file, err := parser.ParseFile(f.Name(), src, parserMode);
95 if err != nil { 95 if err != nil {
96 return err 96 return err
97 } 97 }
98 98
99 if rewrite != nil { 99 if rewrite != nil {
100 file = rewrite(file) 100 file = rewrite(file)
101 } 101 }
102 102
103 var res bytes.Buffer; 103 var res bytes.Buffer;
104 _, err = (&printer.Config{printerMode, *tabwidth, nil}).Fprint(&res, fil e); 104 _, err = (&printer.Config{printerMode, *tabwidth, nil}).Fprint(&res, fil e);
105 if err != nil { 105 if err != nil {
106 return err 106 return err
107 } 107 }
108 108
109 if bytes.Compare(src, res.Bytes()) != 0 { 109 if bytes.Compare(src, res.Bytes()) != 0 {
110 // formatting has changed 110 // formatting has changed
111 if *list { 111 if *list {
112 fmt.Fprintln(os.Stdout, f.Name()) 112 fmt.Fprintln(os.Stdout, f.Name())
113 } 113 }
114 if *write { 114 if *write {
115 » » » err = io.WriteFile(f.Name(), res.Bytes(), 0); 115 » » » err = ioutil.WriteFile(f.Name(), res.Bytes(), 0);
116 if err != nil { 116 if err != nil {
117 return err 117 return err
118 } 118 }
119 } 119 }
120 } 120 }
121 121
122 if !*list && !*write { 122 if !*list && !*write {
123 _, err = os.Stdout.Write(res.Bytes()) 123 _, err = os.Stdout.Write(res.Bytes())
124 } 124 }
125 125
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 if err := processFileByName(path); err != nil { 200 if err := processFileByName(path); err != nil {
201 report(err) 201 report(err)
202 } 202 }
203 case dir.IsDirectory(): 203 case dir.IsDirectory():
204 walkDir(path) 204 walkDir(path)
205 } 205 }
206 } 206 }
207 207
208 os.Exit(exitCode); 208 os.Exit(exitCode);
209 } 209 }
OLDNEW
« no previous file with comments | « src/cmd/godoc/godoc.go ('k') | src/cmd/hgpatch/main.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b