@@ -6,15 +6,17 @@ import 'dart:io';
6
6
7
7
import 'package:args/args.dart' ;
8
8
import 'package:yaml/yaml.dart' ;
9
+ import 'package:config/config.dart' as pkg_config;
9
10
10
11
import 'config_exception.dart' ;
11
12
12
13
/// YAML Reader which enables to override specific values from command line.
13
14
class YamlReader {
14
- YamlReader .of (this .cli, this .yaml, this .yamlFile);
15
- Map <String , String > cli;
16
- Map <dynamic , dynamic > yaml;
17
- File ? yamlFile;
15
+ final pkg_config.Config _config;
16
+
17
+ final Uri ? _configRoot;
18
+
19
+ YamlReader .of (this ._config, this ._configRoot);
18
20
19
21
/// Parses the provided command line arguments and returns a [YamlReader] .
20
22
///
@@ -42,19 +44,15 @@ class YamlReader {
42
44
stderr.writeln (parser.usage);
43
45
exit (1 );
44
46
}
45
- final configFile = results['config' ] as String ? ;
46
- Map <dynamic , dynamic > yamlMap = {};
47
- if (configFile != null ) {
47
+ final configFilePath = results['config' ] as String ? ;
48
+ String ? configFileContents;
49
+ Uri ? configFileUri;
50
+ if (configFilePath != null ) {
48
51
try {
49
- final yamlInput = loadYaml (File (configFile).readAsStringSync (),
50
- sourceUrl: Uri .file (configFile));
51
- if (yamlInput is Map ) {
52
- yamlMap = yamlInput;
53
- } else {
54
- throw ConfigException ('YAML config must be set of key value pairs' );
55
- }
52
+ configFileContents = File (configFilePath).readAsStringSync ();
53
+ configFileUri = File (configFilePath).uri;
56
54
} on Exception catch (e) {
57
- stderr.writeln ('cannot read $configFile : $e ' );
55
+ stderr.writeln ('Cannot read $configFilePath : $e . ' );
58
56
}
59
57
}
60
58
final regex = RegExp ('([a-z-_.]+)=(.+)' );
@@ -70,96 +68,54 @@ class YamlReader {
70
68
}
71
69
}
72
70
return YamlReader .of (
73
- properties, yamlMap, configFile != null ? File (configFile) : null );
71
+ pkg_config.Config (
72
+ cliDefines: results['override' ],
73
+ environment: Platform .environment,
74
+ fileContents: configFileContents,
75
+ fileSourceUri: configFileUri),
76
+ configFileUri? .resolve ('.' ),
77
+ );
74
78
}
75
79
76
- bool ? getBool (String property) {
77
- if (cli.containsKey (property)) {
78
- final v = cli[property]! ;
79
- if (v == 'true' ) {
80
- return true ;
81
- }
82
- if (v == 'false' ) {
83
- return false ;
84
- }
85
- throw ConfigException ('expected boolean value for $property , got $v ' );
86
- }
87
- return getYamlValue <bool >(property);
88
- }
80
+ bool ? getBool (String property) => _config.getBool (property);
89
81
90
- String ? getString (String property) {
91
- final configValue = cli[property] ?? getYamlValue <String >(property);
92
- return configValue;
93
- }
82
+ String ? getString (String property) => _config.getString (property);
94
83
95
84
/// Same as [getString] but path is resolved relative to YAML config if it's
96
85
/// from YAML config.
97
- String ? getPath (String property) {
98
- final cliOverride = cli[property];
99
- if (cliOverride != null ) return cliOverride;
100
- final path = getYamlValue <String >(property);
101
- if (path == null ) return null ;
102
- // In (very unlikely) case YAML config didn't come from a file,
103
- // do not try to resolve anything.
104
- if (yamlFile == null ) return path;
105
- final yamlDir = yamlFile! .parent;
106
- return yamlDir.uri.resolve (path).toFilePath ();
107
- }
86
+ String ? getPath (String property) => _config.getPath (property)? .path;
108
87
109
- List <String >? getStringList (String property) {
110
- final configValue = cli[property] ? . split ( ';' ) ??
111
- getYamlValue < YamlList >(property) ? . cast < String >();
112
- return configValue;
113
- }
88
+ List <String >? getStringList (String property) => _config. getStringList (
89
+ property,
90
+ splitCliPattern : ';' ,
91
+ combineAllConfigs : false ,
92
+ );
114
93
115
94
List <String >? getPathList (String property) {
116
- final cliOverride = cli[property]? .split (';' );
117
- if (cliOverride != null ) return cliOverride;
118
- final paths = getYamlValue <YamlList >(property)? .cast <String >();
119
- if (paths == null ) return null ;
120
- // In (very unlikely) case YAML config didn't come from a file.
121
- if (yamlFile == null ) return paths;
122
- final yamlDir = yamlFile! .parent;
123
- return paths.map ((path) => yamlDir.uri.resolve (path).toFilePath ()).toList ();
95
+ final configResult = _config.getPathList (
96
+ property,
97
+ combineAllConfigs: false ,
98
+ splitCliPattern: ';' ,
99
+ );
100
+ return configResult? .map ((e) => e.path).toList ();
124
101
}
125
102
126
103
String ? getOneOf (String property, Set <String > values) {
127
- final value = cli[property] ?? getYamlValue < String > (property);
104
+ final value = _config. getString (property);
128
105
if (value == null || values.contains (value)) {
129
106
return value;
130
107
}
131
108
throw ConfigException ('expected one of $values for $property ' );
132
109
}
133
110
134
111
Map <String , String >? getStringMap (String property) {
135
- final value = getYamlValue <YamlMap >(property);
112
+ final value = _config. getFileValue <YamlMap >(property);
136
113
return value? .cast <String , String >();
137
114
}
138
115
139
- bool hasValue (String property) => getYamlValue <dynamic >(property) != null ;
140
-
141
- T ? getYamlValue <T >(String property) {
142
- final path = property.split ('.' );
143
- dynamic cursor = yaml;
144
- String current = '' ;
145
- for (var i in path) {
146
- if (cursor is YamlMap || cursor is Map ) {
147
- cursor = cursor[i];
148
- } else {
149
- throw ConfigException ('expected $current to be a YAML map' );
150
- }
151
- current = [if (current != '' ) current, i].join ('.' );
152
- if (cursor == null ) {
153
- return null ;
154
- }
155
- }
156
- if (cursor is ! T ) {
157
- throw ConfigException (
158
- 'expected $T for $property , got ${cursor .runtimeType }' );
159
- }
160
- return cursor;
161
- }
116
+ bool hasValue (String property) =>
117
+ _config.getFileValue <dynamic >(property) != null ;
162
118
163
119
/// Returns URI of the directory containing YAML config.
164
- Uri ? getConfigRoot () => yamlFile ? .parent.uri ;
120
+ Uri ? getConfigRoot () => _configRoot ;
165
121
}
0 commit comments