Skip to content

Commit 765df6c

Browse files
committed
Clena up config command
1 parent 0ee3b2d commit 765df6c

File tree

2 files changed

+17
-43
lines changed

2 files changed

+17
-43
lines changed

lib/src/commands/config_command.dart

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,43 +33,18 @@ class ConfigCommand extends BaseCommand {
3333
final currentConfig = ConfigRepository.loadAppConfig();
3434
var updatedConfig = currentConfig;
3535

36-
// TODO: Consolidate redundant code
37-
38-
if (wasParsed(ConfigKeys.flutterUrl.paramKey)) {
39-
final flutterRepo = stringArg(ConfigKeys.flutterUrl.paramKey);
40-
logger.info('Setting flutter repo to: ${yellow.wrap(flutterRepo)}');
41-
// current.flutterUrl = flutterRepo;
42-
updatedConfig = currentConfig.copyWith(flutterUrl: flutterRepo);
43-
}
44-
45-
if (wasParsed(ConfigKeys.gitCachePath.paramKey)) {
46-
final gitCachePath = stringArg(ConfigKeys.gitCachePath.paramKey);
47-
logger.info('Setting git cache path to: ${yellow.wrap(gitCachePath)}');
48-
// currentConfig.gitCachePath = gitCachePath;
49-
updatedConfig = currentConfig.copyWith(gitCachePath: gitCachePath);
50-
}
51-
52-
if (wasParsed(ConfigKeys.useGitCache.paramKey)) {
53-
final gitCache = boolArg(ConfigKeys.useGitCache.paramKey);
54-
logger.info(
55-
'Setting use git cache to: ${yellow.wrap(gitCache.toString())}',
56-
);
57-
updatedConfig = currentConfig.copyWith(useGitCache: gitCache);
58-
}
59-
60-
if (wasParsed(ConfigKeys.cachePath.paramKey)) {
61-
final cachePath = stringArg(ConfigKeys.cachePath.paramKey);
62-
logger.info('Setting fvm path to: ${yellow.wrap(cachePath)}');
63-
updatedConfig = currentConfig.copyWith(cachePath: cachePath);
36+
void updateConfigKey<T>(ConfigKeys key, T value) {
37+
if (wasParsed(key.paramKey)) {
38+
final updatedMap = AppConfig.fromMap({key.name: value});
39+
logger.info(
40+
'Setting ${key.paramKey} to: ${yellow.wrap(value.toString())}',
41+
);
42+
updatedConfig = updatedConfig.merge(updatedMap);
43+
}
6444
}
6545

66-
if (wasParsed(ConfigKeys.priviledgedAccess.paramKey)) {
67-
final priviledgedAccess = boolArg(ConfigKeys.priviledgedAccess.paramKey);
68-
logger.info(
69-
'Setting priviledged access to: ${yellow.wrap(priviledgedAccess.toString())}',
70-
);
71-
updatedConfig =
72-
currentConfig.copyWith(priviledgedAccess: priviledgedAccess);
46+
for (var key in ConfigKeys.values) {
47+
updateConfigKey(key, argResults![key.paramKey]);
7348
}
7449

7550
// Save
@@ -78,7 +53,7 @@ class ConfigCommand extends BaseCommand {
7853
final updateProgress = logger.progress('Saving settings');
7954
// Update settings
8055
try {
81-
ConfigRepository.save(currentConfig);
56+
ConfigRepository.save(updatedConfig);
8257
} catch (error) {
8358
updateProgress.fail('Failed to save settings');
8459
rethrow;

lib/src/models/config_model.dart

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ enum ConfigKeys {
2828

2929
final String description;
3030

31-
ChangeCase get _recase => ChangeCase(toString());
31+
ChangeCase get _recase => ChangeCase(name);
3232

3333
String get envKey => 'FVM_${_recase.constantCase}';
3434

@@ -41,34 +41,33 @@ enum ConfigKeys {
4141
ConfigKeys.cachePath: () {
4242
argParser.addOption(
4343
ConfigKeys.cachePath.paramKey,
44-
help: 'Path where $kPackageName will cache versions',
44+
help: ConfigKeys.cachePath.description,
4545
);
4646
},
4747
ConfigKeys.useGitCache: () {
4848
argParser.addFlag(
4949
ConfigKeys.useGitCache.paramKey,
50-
help:
51-
'Enable/Disable git cache globally, which is used for faster version installs.',
50+
help: ConfigKeys.useGitCache.description,
5251
defaultsTo: true,
5352
negatable: true,
5453
);
5554
},
5655
ConfigKeys.gitCachePath: () {
5756
argParser.addOption(
5857
ConfigKeys.gitCachePath.paramKey,
59-
help: 'Path where local Git reference cache is stored',
58+
help: ConfigKeys.gitCachePath.description,
6059
);
6160
},
6261
ConfigKeys.flutterUrl: () {
6362
argParser.addOption(
6463
ConfigKeys.flutterUrl.paramKey,
65-
help: 'Flutter repository Git URL to clone from',
64+
help: ConfigKeys.flutterUrl.description,
6665
);
6766
},
6867
ConfigKeys.priviledgedAccess: () {
6968
argParser.addFlag(
7069
ConfigKeys.priviledgedAccess.paramKey,
71-
help: 'Enable/Disable priviledged access for FVM',
70+
help: ConfigKeys.priviledgedAccess.description,
7271
defaultsTo: true,
7372
negatable: true,
7473
);

0 commit comments

Comments
 (0)