godoc/static: makestatic.go: gofmt generated code

Change-Id: Ibe0948fa283af94b1213e9ef344ef9eda6e59c05
Reviewed-on: https://go-review.googlesource.com/23532
Reviewed-by: Andrew Gerrand <adg@golang.org>
This commit is contained in:
Konstantin Shaposhnikov 2016-05-28 10:59:14 +08:00 committed by Andrew Gerrand
parent ba484b064f
commit 3560419ce9
1 changed files with 12 additions and 11 deletions

View File

@ -10,9 +10,9 @@
package main package main
import ( import (
"bufio"
"bytes" "bytes"
"fmt" "fmt"
"go/format"
"io/ioutil" "io/ioutil"
"os" "os"
"unicode/utf8" "unicode/utf8"
@ -83,27 +83,28 @@ func makestatic() error {
return err return err
} }
defer f.Close() defer f.Close()
w := bufio.NewWriter(f) buf := new(bytes.Buffer)
fmt.Fprintf(w, "%v\npackage static\n\n", warning) fmt.Fprintf(buf, "%v\npackage static\n\n", warning)
fmt.Fprintf(w, "var Files = map[string]string{\n") fmt.Fprintf(buf, "var Files = map[string]string{\n")
for _, fn := range files { for _, fn := range files {
b, err := ioutil.ReadFile(fn) b, err := ioutil.ReadFile(fn)
if err != nil { if err != nil {
return err return err
} }
fmt.Fprintf(w, "\t%q: ", fn) fmt.Fprintf(buf, "\t%q: ", fn)
if utf8.Valid(b) { if utf8.Valid(b) {
fmt.Fprintf(w, "`%s`", sanitize(b)) fmt.Fprintf(buf, "`%s`", sanitize(b))
} else { } else {
fmt.Fprintf(w, "%q", b) fmt.Fprintf(buf, "%q", b)
} }
fmt.Fprintln(w, ",\n") fmt.Fprintln(buf, ",\n")
} }
fmt.Fprintln(w, "}") fmt.Fprintln(buf, "}")
if err := w.Flush(); err != nil { fmtbuf, err := format.Source(buf.Bytes())
if err != nil {
return err return err
} }
return f.Close() return ioutil.WriteFile("static.go", fmtbuf, 0666)
} }
// sanitize prepares a valid UTF-8 string as a raw string constant. // sanitize prepares a valid UTF-8 string as a raw string constant.