@@ -2,11 +2,9 @@ package main
22
33import (
44 "errors"
5- "io"
65 "log"
76 "math/rand"
87 "os"
9- "path/filepath"
108 "strings"
119 "time"
1210
@@ -20,42 +18,6 @@ type findReplace struct {
2018 replace string
2119}
2220
23- type File struct {
24- Path string
25- info os.FileInfo
26- }
27-
28- func NewFile (path string ) * File {
29- absPath , err := filepath .Abs (path )
30- if err != nil {
31- log .Fatalf ("Unable to resolve absolute path of %v: %v" , path , err )
32- }
33- return & File {Path : absPath }
34- }
35-
36- func (f * File ) Base () string {
37- return filepath .Base (f .Path )
38- }
39-
40- func (f * File ) Dir () string {
41- return filepath .Dir (f .Path )
42- }
43-
44- func (f * File ) Info () os.FileInfo {
45- if f .info == nil {
46- stat , err := os .Stat (f .Path )
47- if err != nil {
48- log .Fatalf ("Failed to stat %v: %v" , f .Path , err )
49- }
50- f .info = stat
51- }
52- return f .info
53- }
54-
55- func (f * File ) Mode () os.FileMode {
56- return f .Info ().Mode ()
57- }
58-
5921// main processes command line arguments, builds the context struct, and begins
6022// the process of walking the current working directory.
6123//
@@ -142,52 +104,9 @@ func (fr *findReplace) RenameFile(f *File) {
142104// context.
143105func (fr * findReplace ) ReplaceContents (f * File ) {
144106 // Find & replace the contents of file.
145- content := readFile ( f . Path )
107+ content := f . Read ( )
146108 if util .IsText ([]byte (content )) && strings .Contains (content , fr .find ) {
147109 newContent := strings .Replace (content , fr .find , fr .replace , - 1 )
148- writeFile (f , newContent )
149- }
150- }
151-
152- // readFile reads a file at the given path into a string.
153- func readFile (path string ) string {
154- f , err := os .Open (path )
155- if err != nil {
156- log .Fatalf ("Unable to open %v: %v" , path , err )
157- }
158- defer f .Close ()
159- builder := new (strings.Builder )
160- if _ , err := io .Copy (builder , f ); err != nil {
161- log .Fatalf ("Failed to read %v to a string: %v" , path , err )
162- }
163- return builder .String ()
164- }
165-
166- // writeFile atomically write content to file by writing it to a temporary file
167- // first, and then moving it to the destination, overwriting the original.
168- func writeFile (f * File , content string ) {
169- tempName := f .Dir () + string (os .PathSeparator ) + randomString (20 )
170- if err := os .WriteFile (tempName , []byte (content ), f .Mode ()); err != nil {
171- log .Fatalf ("Error creating tempfile in %v: %v" , f .Dir (), err )
172- }
173-
174- log .Printf ("Rewriting %v" , f .Path )
175- if err := os .Rename (tempName , f .Path ); err != nil {
176- log .Fatalf ("Unable to atomically move temp file %v to %v: %v" , tempName , f .Path , err )
177- }
178- }
179-
180- var characters = []rune ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" )
181-
182- // randomString generates a random base-62 string of the given length (or returns an
183- // empty string).
184- func randomString (n int ) string {
185- if n <= 0 {
186- return ""
187- }
188- b := make ([]rune , n )
189- for i := range b {
190- b [i ] = characters [rand .Intn (len (characters ))]
110+ f .Write (newContent )
191111 }
192- return string (b )
193112}
0 commit comments