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