Files
flipper/iOS/FlipperKit/SKEnvironmentVariables.m
Pascal Hartig f87dfdfea0 Read port configuration from UserDefaults
Summary:
This allows us to configure a simulator to use a non-default port pair by using the `defaults` mechanism. For instance, this can be done by running

```
xcrun simctl spawn booted defaults write "Apple Global Domain" "com.facebook.flipper.ports" -string "9088,9089"
```

Reviewed By: lblasa, timur-valiev

Differential Revision: D30731874

fbshipit-source-id: 689d60b1c392f36dceef1b3c3cfa0c88f54a7a82
2021-09-06 06:06:13 -07:00

51 lines
1.5 KiB
Objective-C

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#ifdef FB_SONARKIT_ENABLED
#import "SKEnvironmentVariables.h"
static int const DEFAULT_INSECURE_PORT = 8089;
static int const DEFAULT_SECURE_PORT = 8088;
@implementation SKEnvironmentVariables
+ (int)getInsecurePort {
NSString* envVar = [self getFlipperPortsVariable];
return [self extractIntFromPropValue:envVar
atIndex:0
withDefault:DEFAULT_INSECURE_PORT];
}
+ (int)getSecurePort {
NSString* envVar = [self getFlipperPortsVariable];
return [self extractIntFromPropValue:envVar
atIndex:1
withDefault:DEFAULT_SECURE_PORT];
}
+ (int)extractIntFromPropValue:(NSString*)propValue
atIndex:(int)index
withDefault:(int)fallback {
NSArray<NSString*>* components = [propValue componentsSeparatedByString:@","];
NSString* component = [components objectAtIndex:index];
int envInt = [component intValue];
return envInt > 0 ? envInt : fallback;
}
+ (NSString*)getFlipperPortsVariable {
// Try to retrieve from environment first.
NSString* value = NSProcessInfo.processInfo.environment[@"FLIPPER_PORTS"];
// If empty, check defaults instead.
if ([value length] == 0) {
value = [[NSUserDefaults standardUserDefaults]
stringForKey:@"com.facebook.flipper.ports"];
}
return value;
}
@end
#endif