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

Unified Diff: client/cmd/isolate/batch_archive.go

Issue 2938823003: Move WriteJSONFile to its sole caller's package, with some refactoring. (Closed)
Patch Set: Created 3 years, 6 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | client/internal/common/json.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: client/cmd/isolate/batch_archive.go
diff --git a/client/cmd/isolate/batch_archive.go b/client/cmd/isolate/batch_archive.go
index a1dddcc98932def3278c0c938ae84d5ded6b8233..772c7a96a056fab64fbd03b8b74757ab023695ce 100644
--- a/client/cmd/isolate/batch_archive.go
+++ b/client/cmd/isolate/batch_archive.go
@@ -176,7 +176,7 @@ func (c *batchArchiveRun) main(a subcommands.Application, args []string) error {
duration := time.Since(start)
// Only write the file once upload is confirmed.
if err == nil && c.dumpJSON != "" {
- err = common.WriteJSONFile(c.dumpJSON, data)
+ err = writeJSONDigestFile(c.dumpJSON, data)
}
if !c.defaultFlags.Quiet {
stats := arch.Stats()
@@ -236,6 +236,32 @@ func strippedIsolatedName(isolated string) string {
return name
}
+func writeJSONDigestFile(filePath string, data map[string]isolated.HexDigest) error {
+ digestBytes, err := json.MarshalIndent(data, "", " ")
+ if err != nil {
+ return fmt.Errorf("encoding digest JSON: %s", err)
+ }
+ return writeFile(filePath, digestBytes)
+}
+
+// writeFile writes data to filePath. File permission is set to user only.
+func writeFile(filePath string, data []byte) error {
+ f, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
+ if err != nil {
+ return fmt.Errorf("opening %s: %s", filePath, err)
+ }
+ // NOTE: We don't defer f.Close here, because it may return an error.
+
+ _, writeErr := f.Write(data)
+ closeErr := f.Close()
+ if writeErr != nil {
+ return fmt.Errorf("writing %s: %s", filePath, writeErr)
+ } else if closeErr != nil {
+ return fmt.Errorf("closing %s: %s", filePath, closeErr)
+ }
+ return nil
+}
+
func (c *batchArchiveRun) Run(a subcommands.Application, args []string, _ subcommands.Env) int {
if err := c.Parse(a, args); err != nil {
fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err)
« no previous file with comments | « no previous file | client/internal/common/json.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698