io.go 882 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package glue
  2. import (
  3. "encoding/json"
  4. "io"
  5. "os"
  6. "github.com/pkg/errors"
  7. )
  8. func isZero(path string) bool {
  9. stat, err := os.Stat(path)
  10. if os.IsNotExist(err) {
  11. return true
  12. }
  13. if err != nil {
  14. return true
  15. }
  16. return stat.Size() == 0
  17. }
  18. func copyToExistingFile(to, from string) error {
  19. src, err := os.Open(from)
  20. if err != nil {
  21. return errors.Wrap(err, "opening file "+from)
  22. }
  23. defer src.Close()
  24. dest, err := os.OpenFile(to, os.O_RDWR|os.O_TRUNC, 0666)
  25. if err != nil {
  26. return errors.Wrap(err, "opening file "+to)
  27. }
  28. defer dest.Close()
  29. _, err = io.Copy(dest, src)
  30. return err
  31. }
  32. func readJSONFile(file string, obj interface{}) error {
  33. f, err := os.Open(file)
  34. if err != nil {
  35. return errors.Wrap(err, "opening "+file)
  36. }
  37. defer f.Close()
  38. if err := json.NewDecoder(f).Decode(obj); err != nil {
  39. return errors.Wrap(err, "unmarshaling "+file)
  40. }
  41. return nil
  42. }