github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/profiling/cmd/local.go (about) 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 19 package main 20 21 import ( 22 "encoding/gob" 23 "fmt" 24 "os" 25 ) 26 27 func loadSnapshot(snapshotFileName string) (*snapshot, error) { 28 logger.Infof("opening snapshot file %s", snapshotFileName) 29 snapshotFile, err := os.Open(snapshotFileName) 30 if err != nil { 31 logger.Errorf("cannot open %s: %v", snapshotFileName, err) 32 return nil, err 33 } 34 defer snapshotFile.Close() 35 36 logger.Infof("decoding snapshot file %s", snapshotFileName) 37 s := &snapshot{} 38 decoder := gob.NewDecoder(snapshotFile) 39 if err = decoder.Decode(s); err != nil { 40 logger.Errorf("cannot decode %s: %v", snapshotFileName, err) 41 return nil, err 42 } 43 44 return s, nil 45 } 46 47 func localCommand() error { 48 if *flagSnapshot == "" { 49 return fmt.Errorf("-snapshot flag missing") 50 } 51 52 s, err := loadSnapshot(*flagSnapshot) 53 if err != nil { 54 return err 55 } 56 57 if *flagStreamStatsCatapultJSON == "" { 58 return fmt.Errorf("snapshot file specified without an action to perform") 59 } 60 61 if *flagStreamStatsCatapultJSON != "" { 62 if err = streamStatsCatapultJSON(s, *flagStreamStatsCatapultJSON); err != nil { 63 return err 64 } 65 } 66 67 return nil 68 }