|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/SimFG/etcd-analysis/core" |
| 8 | + "github.com/golang/protobuf/proto" |
| 9 | + "github.com/jhump/protoreflect/desc" |
| 10 | + "github.com/jhump/protoreflect/desc/protoparse" |
| 11 | + "github.com/jhump/protoreflect/dynamic" |
| 12 | + "github.com/spf13/cobra" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + unmarshallKey string |
| 17 | + unmarshalImportPaths []string |
| 18 | + unmarshalFileNames []string |
| 19 | + unmarshalFullMessageName string |
| 20 | +) |
| 21 | + |
| 22 | +func NewUnmarshalCmd() *cobra.Command { |
| 23 | + cmd := &cobra.Command{ |
| 24 | + Use: "unmarshal", |
| 25 | + Short: "unmarshal the etcd value", |
| 26 | + Run: unmarshalFunc, |
| 27 | + } |
| 28 | + |
| 29 | + cmd.Flags().StringVar(&unmarshallKey, "key", "", "the proto.marshal value of the full key") |
| 30 | + cmd.Flags().StringArrayVar(&unmarshalImportPaths, "import-path", []string{}, "the proto.marshal value of the full key") |
| 31 | + cmd.Flags().StringArrayVar(&unmarshalFileNames, "proto", []string{}, "the proto.marshal value of the full key") |
| 32 | + cmd.Flags().StringVar(&unmarshalFullMessageName, "full-message-name", "", "the proto.marshal value of the full key") |
| 33 | + |
| 34 | + return cmd |
| 35 | +} |
| 36 | + |
| 37 | +func unmarshalFunc(cmd *cobra.Command, args []string) { |
| 38 | + client := core.InitClient() |
| 39 | + resp, err := core.EtcdGet(client, unmarshallKey) |
| 40 | + if err != nil { |
| 41 | + core.Exit(err) |
| 42 | + } |
| 43 | + if len(resp.Kvs) == 0 { |
| 44 | + core.Exit(errors.New("not found the key")) |
| 45 | + } |
| 46 | + bytesValue := resp.Kvs[0].Value |
| 47 | + decodeBytes(bytesValue) |
| 48 | + return |
| 49 | +} |
| 50 | + |
| 51 | +func decodeBytes(bytesValue []byte) { |
| 52 | + fileNames, err := protoparse.ResolveFilenames(unmarshalImportPaths, unmarshalFileNames...) |
| 53 | + if err != nil { |
| 54 | + fmt.Println("counld not resolve file names") |
| 55 | + core.Exit(err) |
| 56 | + } |
| 57 | + p := protoparse.Parser{ |
| 58 | + ImportPaths: unmarshalImportPaths, |
| 59 | + InferImportPaths: len(unmarshalImportPaths) == 0, |
| 60 | + IncludeSourceCodeInfo: true, |
| 61 | + } |
| 62 | + |
| 63 | + fds, err := p.ParseFiles(fileNames...) |
| 64 | + if err != nil { |
| 65 | + fmt.Println("could not parse given files") |
| 66 | + core.Exit(err) |
| 67 | + } |
| 68 | + var messageType *desc.MessageDescriptor |
| 69 | + for _, protoDesc := range fds { |
| 70 | + messageType = protoDesc.FindMessage(unmarshalFullMessageName) |
| 71 | + if messageType != nil { |
| 72 | + break |
| 73 | + } |
| 74 | + } |
| 75 | + if messageType == nil { |
| 76 | + fmt.Println("could not find message type, please check the message name is full, name: " + unmarshalFullMessageName) |
| 77 | + return |
| 78 | + } |
| 79 | + message := dynamic.NewMessage(messageType) |
| 80 | + err = proto.Unmarshal(bytesValue, message) |
| 81 | + if err != nil { |
| 82 | + fmt.Println("could not unmarshal bytes") |
| 83 | + core.Exit(err) |
| 84 | + } |
| 85 | + fmt.Println() |
| 86 | + fields := message.GetKnownFields() |
| 87 | + for _, field := range fields { |
| 88 | + fieldValue := message.GetField(field) |
| 89 | + fmt.Printf("%s: %v\n", field.GetName(), fieldValue) |
| 90 | + } |
| 91 | +} |
0 commit comments