local.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. *
  3. * Copyright 2019 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package main
  19. import (
  20. "encoding/gob"
  21. "fmt"
  22. "os"
  23. )
  24. func loadSnapshot(snapshotFileName string) (*snapshot, error) {
  25. logger.Infof("opening snapshot file %s", snapshotFileName)
  26. snapshotFile, err := os.Open(snapshotFileName)
  27. if err != nil {
  28. logger.Errorf("cannot open %s: %v", snapshotFileName, err)
  29. return nil, err
  30. }
  31. defer snapshotFile.Close()
  32. logger.Infof("decoding snapshot file %s", snapshotFileName)
  33. s := &snapshot{}
  34. decoder := gob.NewDecoder(snapshotFile)
  35. if err = decoder.Decode(s); err != nil {
  36. logger.Errorf("cannot decode %s: %v", snapshotFileName, err)
  37. return nil, err
  38. }
  39. return s, nil
  40. }
  41. func localCommand() error {
  42. if *flagSnapshot == "" {
  43. return fmt.Errorf("-snapshot flag missing")
  44. }
  45. s, err := loadSnapshot(*flagSnapshot)
  46. if err != nil {
  47. return err
  48. }
  49. if *flagStreamStatsCatapultJSON == "" {
  50. return fmt.Errorf("snapshot file specified without an action to perform")
  51. }
  52. if *flagStreamStatsCatapultJSON != "" {
  53. if err = streamStatsCatapultJSON(s, *flagStreamStatsCatapultJSON); err != nil {
  54. return err
  55. }
  56. }
  57. return nil
  58. }