| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | |
| 3 // that can be found in the LICENSE file. | |
| 4 | |
| 5 package common | |
| 6 | |
| 7 import ( | |
| 8 "encoding/json" | |
| 9 "fmt" | |
| 10 "os" | |
| 11 ) | |
| 12 | |
| 13 // WriteJSONFile writes object as json encoded into filePath with 2 spaces | |
| 14 // indentation. File permission is set to user only. | |
| 15 func WriteJSONFile(filePath string, object interface{}) error { | |
| 16 d, err := json.MarshalIndent(object, "", " ") | |
| 17 if err != nil { | |
| 18 return fmt.Errorf("failed to encode %s: %s", filePath, err) | |
| 19 } | |
| 20 | |
| 21 f, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) | |
| 22 if err != nil { | |
| 23 return fmt.Errorf("failed to open %s: %s", filePath, err) | |
| 24 } | |
| 25 defer f.Close() | |
| 26 if _, err := f.Write(d); err != nil { | |
| 27 return fmt.Errorf("failed to write %s: %s", filePath, err) | |
| 28 } | |
| 29 return nil | |
| 30 } | |
| OLD | NEW |