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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « no previous file | client/internal/common/json.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 2015 The LUCI Authors. All rights reserved. 1 // Copyright 2015 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 package main 5 package main
6 6
7 import ( 7 import (
8 "encoding/json" 8 "encoding/json"
9 "errors" 9 "errors"
10 "fmt" 10 "fmt"
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 data[item.name] = item.Digest() 169 data[item.name] = item.Digest()
170 fmt.Printf("%s%s %s\n", prefix, item.Digest(), item.nam e) 170 fmt.Printf("%s%s %s\n", prefix, item.Digest(), item.nam e)
171 } else { 171 } else {
172 fmt.Fprintf(os.Stderr, "%s%s %s\n", prefix, item.name, item.Error()) 172 fmt.Fprintf(os.Stderr, "%s%s %s\n", prefix, item.name, item.Error())
173 } 173 }
174 } 174 }
175 err = arch.Close() 175 err = arch.Close()
176 duration := time.Since(start) 176 duration := time.Since(start)
177 // Only write the file once upload is confirmed. 177 // Only write the file once upload is confirmed.
178 if err == nil && c.dumpJSON != "" { 178 if err == nil && c.dumpJSON != "" {
179 » » err = common.WriteJSONFile(c.dumpJSON, data) 179 » » err = writeJSONDigestFile(c.dumpJSON, data)
180 } 180 }
181 if !c.defaultFlags.Quiet { 181 if !c.defaultFlags.Quiet {
182 stats := arch.Stats() 182 stats := arch.Stats()
183 fmt.Fprintf(os.Stderr, "Hits : %5d (%s)\n", stats.TotalHits() , stats.TotalBytesHits()) 183 fmt.Fprintf(os.Stderr, "Hits : %5d (%s)\n", stats.TotalHits() , stats.TotalBytesHits())
184 fmt.Fprintf(os.Stderr, "Misses : %5d (%s)\n", stats.TotalMisses (), stats.TotalBytesPushed()) 184 fmt.Fprintf(os.Stderr, "Misses : %5d (%s)\n", stats.TotalMisses (), stats.TotalBytesPushed())
185 fmt.Fprintf(os.Stderr, "Duration: %s\n", units.Round(duration, t ime.Millisecond)) 185 fmt.Fprintf(os.Stderr, "Duration: %s\n", units.Round(duration, t ime.Millisecond))
186 } 186 }
187 return err 187 return err
188 } 188 }
189 189
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 // strippedIsolatedName returns the base name of an isolated path, with the exte nsion (if any) removed. 229 // strippedIsolatedName returns the base name of an isolated path, with the exte nsion (if any) removed.
230 func strippedIsolatedName(isolated string) string { 230 func strippedIsolatedName(isolated string) string {
231 name := filepath.Base(isolated) 231 name := filepath.Base(isolated)
232 // Strip the extension if there is one. 232 // Strip the extension if there is one.
233 if dotIndex := strings.LastIndex(name, "."); dotIndex != -1 { 233 if dotIndex := strings.LastIndex(name, "."); dotIndex != -1 {
234 return name[0:dotIndex] 234 return name[0:dotIndex]
235 } 235 }
236 return name 236 return name
237 } 237 }
238 238
239 func writeJSONDigestFile(filePath string, data map[string]isolated.HexDigest) er ror {
240 digestBytes, err := json.MarshalIndent(data, "", " ")
241 if err != nil {
242 return fmt.Errorf("encoding digest JSON: %s", err)
243 }
244 return writeFile(filePath, digestBytes)
245 }
246
247 // writeFile writes data to filePath. File permission is set to user only.
248 func writeFile(filePath string, data []byte) error {
249 f, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
250 if err != nil {
251 return fmt.Errorf("opening %s: %s", filePath, err)
252 }
253 // NOTE: We don't defer f.Close here, because it may return an error.
254
255 _, writeErr := f.Write(data)
256 closeErr := f.Close()
257 if writeErr != nil {
258 return fmt.Errorf("writing %s: %s", filePath, writeErr)
259 } else if closeErr != nil {
260 return fmt.Errorf("closing %s: %s", filePath, closeErr)
261 }
262 return nil
263 }
264
239 func (c *batchArchiveRun) Run(a subcommands.Application, args []string, _ subcom mands.Env) int { 265 func (c *batchArchiveRun) Run(a subcommands.Application, args []string, _ subcom mands.Env) int {
240 if err := c.Parse(a, args); err != nil { 266 if err := c.Parse(a, args); err != nil {
241 fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err) 267 fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err)
242 return 1 268 return 1
243 } 269 }
244 cl, err := c.defaultFlags.StartTracing() 270 cl, err := c.defaultFlags.StartTracing()
245 if err != nil { 271 if err != nil {
246 fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err) 272 fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err)
247 return 1 273 return 1
248 } 274 }
249 defer cl.Close() 275 defer cl.Close()
250 if err := c.main(a, args); err != nil { 276 if err := c.main(a, args); err != nil {
251 fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err) 277 fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err)
252 return 1 278 return 1
253 } 279 }
254 return 0 280 return 0
255 } 281 }
OLDNEW
« 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