MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / main

Function main

pkg/redis/codec/main.go:40–95  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

38}
39
40func main() {
41 typ := flag.String("type", "", "Proto type to be used")
42 encode := flag.Bool("encode", false, "Whether encoding should be performed (default: false)")
43
44 flag.Parse()
45
46 var pb proto.Message
47 switch *typ {
48 case "":
49 log.Print("Type cannot be empty")
50 flag.Usage()
51 os.Exit(1)
52
53 case "ttnpb.EndDevice":
54 pb = &ttnpb.EndDevice{}
55 case "ttnpb.GatewayConnectionStats":
56 pb = &ttnpb.GatewayConnectionStats{}
57 case "ttnpb.ApplicationActivationSettings":
58 pb = &ttnpb.ApplicationActivationSettings{}
59 case "ttnpb.UplinkToken":
60 pb = &ttnpb.UplinkToken{}
61 default:
62 log.Printf("Unknown type: `%s`", *typ)
63 flag.Usage()
64 os.Exit(1)
65 }
66 if *encode {
67 if err := json.NewDecoder(os.Stdin).Decode(pb); err != nil {
68 log.Fatalf("Failed to read proto as JSON from stdin: %v", err)
69 }
70 s, err := redis.MarshalProto(pb)
71 if err != nil {
72 log.Fatalf("Failed to marshal proto: %v", err)
73 }
74 if _, err := os.Stdout.Write([]byte(s)); err != nil {
75 log.Fatalf("Failed to write to stdout: %v", err)
76 }
77 return
78 }
79 rd := bufio.NewReader(os.Stdin)
80 for {
81 b, err := rd.ReadString('\n')
82 if err != nil {
83 if errors.Is(err, io.EOF) {
84 break
85 }
86 log.Fatalf("Failed to read from stdin: %v", err)
87 }
88 if err := redis.UnmarshalProto(b, pb); err != nil {
89 log.Fatalf("Failed to unmarshal proto: %v", err)
90 }
91 if err := json.NewEncoder(os.Stdout).Encode(pb); err != nil {
92 log.Fatalf("Failed to write proto as JSON to stdout: %v", err)
93 }
94 }
95}

Callers

nothing calls this directly

Calls 12

MarshalProtoFunction · 0.92
UnmarshalProtoFunction · 0.92
NewDecoderMethod · 0.80
NewEncoderMethod · 0.80
StringMethod · 0.65
ParseMethod · 0.65
DecodeMethod · 0.65
FatalfMethod · 0.65
EncodeMethod · 0.65
PrintfMethod · 0.45
WriteMethod · 0.45
IsMethod · 0.45

Tested by

no test coverage detected