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" |
| 11 | "google.golang.org/grpc/metadata" |
| 12 | |
| 13 | pb "gomodules.avm99963.com/twpt-server/api_proto" |
| 14 | ) |
| 15 | |
| 16 | var ( |
| 17 | grpcEndpoint = flag.String("grpcEndpoint", "", "gRPC endpoint address.") |
| 18 | jwt = flag.String("jwt", "", "JWT credentials.") |
| 19 | ) |
| 20 | |
| 21 | type Features map[string]Feature |
| 22 | |
| 23 | type Feature struct { |
| 24 | DefaultValue interface{} `json:"defaultValue"` |
| 25 | Context string `json:"context"` |
| 26 | KillSwitchType string `json:"killSwitchType"` |
| 27 | } |
| 28 | |
| 29 | func convertStringTypeToPb(context string) pb.Feature_Type { |
| 30 | switch context { |
| 31 | case "option": |
| 32 | return pb.Feature_TYPE_OPTION |
| 33 | |
| 34 | case "experiment": |
| 35 | return pb.Feature_TYPE_EXPERIMENT |
| 36 | |
| 37 | case "deprecated": |
| 38 | return pb.Feature_TYPE_DEPRECATED |
| 39 | |
| 40 | default: |
| 41 | return pb.Feature_TYPE_UNKNOWN |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func main() { |
| 46 | flag.Parse() |
| 47 | |
| 48 | conn, err := grpc.Dial(*grpcEndpoint, grpc.WithInsecure()) |
| 49 | if err != nil { |
| 50 | log.Fatalf("error while connecting to gRPC endpoint: %v\n", err) |
| 51 | } |
| 52 | defer conn.Close() |
| 53 | |
| 54 | client := pb.NewKillSwitchServiceClient(conn) |
| 55 | md := metadata.Pairs("authorization", *jwt) |
| 56 | ctx := metadata.NewOutgoingContext(context.Background(), md) |
| 57 | |
| 58 | var jsonFeatures Features |
| 59 | err = json.NewDecoder(os.Stdin).Decode(&jsonFeatures) |
| 60 | if err != nil { |
| 61 | log.Fatalf("can't decode JSON file: %v\n", err) |
| 62 | } |
| 63 | |
| 64 | features := make([]*pb.Feature, 0) |
| 65 | for codename, jsonFeature := range jsonFeatures { |
| 66 | if jsonFeature.Context == "internal" { |
| 67 | continue |
| 68 | } |
| 69 | |
| 70 | feature := &pb.Feature{ |
| 71 | Codename: codename, |
| 72 | Type: convertStringTypeToPb(jsonFeature.KillSwitchType), |
| 73 | } |
| 74 | features = append(features, feature) |
| 75 | } |
| 76 | |
| 77 | request := &pb.SyncFeaturesRequest{ |
| 78 | Features: features, |
| 79 | } |
| 80 | _, err = client.SyncFeatures(ctx, request) |
| 81 | if err != nil { |
| 82 | log.Fatalf("error syncing features: %v\n", err) |
| 83 | } |
| 84 | |
| 85 | log.Println("Synced features successfully.") |
| 86 | } |