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 | |
| 41 | case "deprecated": |
| 42 | return pb.Feature_TYPE_DEPRECATED |
| 43 | |
| 44 | default: |
| 45 | return pb.Feature_TYPE_UNKNOWN |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func main() { |
| 50 | flag.Parse() |
| 51 | |
Adrià Vilanova Martínez | 34d806c | 2021-12-08 18:37:38 +0100 | [diff] [blame^] | 52 | 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ínez | e94112f | 2021-09-04 01:04:24 +0200 | [diff] [blame] | 64 | 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 | } |