blob: a4f97d890fd95ddaea3ead54b690549ce9c00d68 [file] [log] [blame]
Adrià Vilanova Martíneze94112f2021-09-04 01:04:24 +02001package main
2
3import (
4 "context"
5 "encoding/json"
6 "flag"
7 "log"
8 "os"
9
10 "google.golang.org/grpc"
Adrià Vilanova Martínez34d806c2021-12-08 18:37:38 +010011 "google.golang.org/grpc/credentials"
Adrià Vilanova Martíneze94112f2021-09-04 01:04:24 +020012 "google.golang.org/grpc/metadata"
13
Adrià Vilanova Martínez34d806c2021-12-08 18:37:38 +010014 "github.com/johnsiilver/getcert"
15
Adrià Vilanova Martíneze94112f2021-09-04 01:04:24 +020016 pb "gomodules.avm99963.com/twpt-server/api_proto"
17)
18
19var (
20 grpcEndpoint = flag.String("grpcEndpoint", "", "gRPC endpoint address.")
21 jwt = flag.String("jwt", "", "JWT credentials.")
Adrià Vilanova Martínez34d806c2021-12-08 18:37:38 +010022 insecure = flag.Bool("insecure", false, "Set if the connection to the gRPC endpoint is insecure.")
Adrià Vilanova Martíneze94112f2021-09-04 01:04:24 +020023)
24
25type Features map[string]Feature
26
27type Feature struct {
28 DefaultValue interface{} `json:"defaultValue"`
29 Context string `json:"context"`
30 KillSwitchType string `json:"killSwitchType"`
31}
32
33func convertStringTypeToPb(context string) pb.Feature_Type {
34 switch context {
35 case "option":
36 return pb.Feature_TYPE_OPTION
37
38 case "experiment":
39 return pb.Feature_TYPE_EXPERIMENT
40
41 case "deprecated":
42 return pb.Feature_TYPE_DEPRECATED
43
44 default:
45 return pb.Feature_TYPE_UNKNOWN
46 }
47}
48
49func main() {
50 flag.Parse()
51
Adrià Vilanova Martínez34d806c2021-12-08 18:37:38 +010052 var err error
53 var conn *grpc.ClientConn
54
55 if *insecure {
56 conn, err = grpc.Dial(*grpcEndpoint, grpc.WithInsecure())
57 } else {
58 tlsCert, _, err2 := getcert.FromTLSServer(*grpcEndpoint, false)
59 if err2 != nil {
60 log.Fatalf("error while retrieving public certificate: %v\n", err2)
61 }
62 conn, err = grpc.Dial(*grpcEndpoint, grpc.WithTransportCredentials(credentials.NewServerTLSFromCert(&tlsCert)))
63 }
Adrià Vilanova Martíneze94112f2021-09-04 01:04:24 +020064 if err != nil {
65 log.Fatalf("error while connecting to gRPC endpoint: %v\n", err)
66 }
67 defer conn.Close()
68
69 client := pb.NewKillSwitchServiceClient(conn)
70 md := metadata.Pairs("authorization", *jwt)
71 ctx := metadata.NewOutgoingContext(context.Background(), md)
72
73 var jsonFeatures Features
74 err = json.NewDecoder(os.Stdin).Decode(&jsonFeatures)
75 if err != nil {
76 log.Fatalf("can't decode JSON file: %v\n", err)
77 }
78
79 features := make([]*pb.Feature, 0)
80 for codename, jsonFeature := range jsonFeatures {
81 if jsonFeature.Context == "internal" {
82 continue
83 }
84
85 feature := &pb.Feature{
86 Codename: codename,
87 Type: convertStringTypeToPb(jsonFeature.KillSwitchType),
88 }
89 features = append(features, feature)
90 }
91
92 request := &pb.SyncFeaturesRequest{
93 Features: features,
94 }
95 _, err = client.SyncFeatures(ctx, request)
96 if err != nil {
97 log.Fatalf("error syncing features: %v\n", err)
98 }
99
100 log.Println("Synced features successfully.")
101}