feat(config): add ability to get value of variable

This commit is contained in:
Aleksey Kulikov 2021-06-18 16:48:13 +03:00
parent 7bea406ab9
commit f6b8cc7005
3 changed files with 43 additions and 56 deletions

View file

@ -120,16 +120,12 @@ String findXdg() {
/// Get the value of a config variable. /// Get the value of a config variable.
/// ///
/// All config files will be looked into, in the order of their
/// defined level. A higher level means a higher priority. The
/// first occurrence of the variable will be returned here.
///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<Int8> getConfigValue(Pointer<git_config> cfg, String variable) { String getValue(Pointer<git_config> cfg, String variable) {
final out = calloc<git_buf>(); final out = calloc<Pointer<git_config_entry>>();
final name = variable.toNativeUtf8().cast<Int8>(); final name = variable.toNativeUtf8().cast<Int8>();
final error = libgit2.git_config_get_path(out, cfg, name); final error = libgit2.git_config_get_entry(out, cfg, name);
final value = out.ref.ptr; final value = out.value;
calloc.free(out); calloc.free(out);
calloc.free(name); calloc.free(name);
@ -137,36 +133,7 @@ Pointer<Int8> getConfigValue(Pointer<git_config> cfg, String variable) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} }
return value; return value.ref.value.cast<Utf8>().toDartString();
}
/// Get the value of a config variable and parse it as a boolean according
/// to git-config rules.
///
/// Interprets "true", "yes", "on", 1, or any non-zero number as true.
/// Interprets "false", "no", "off", 0, or an empty string as false.
bool getBool(Pointer<git_config> cfg, String variable) {
final value = getConfigValue(cfg, variable);
final out = calloc<Int32>();
libgit2.git_config_parse_bool(out, value);
final result = out.value;
calloc.free(out);
return (result == 0) ? false : true;
}
/// Get the value of a config variable and parse it as an integer according
/// to git-config rules.
///
/// Handles suffixes like k, M, or G - kilo, mega, giga.
int getInt(Pointer<git_config> cfg, String variable) {
final value = getConfigValue(cfg, variable);
final out = calloc<Int64>();
libgit2.git_config_parse_int64(out, value);
final result = out.value;
calloc.free(out);
return result;
} }
/// Set the value of a boolean config variable in the config file with the /// Set the value of a boolean config variable in the config file with the

View file

@ -113,15 +113,24 @@ class Config {
/// Map of key/value entries from config file. /// Map of key/value entries from config file.
Map<String, dynamic> variables = {}; Map<String, dynamic> variables = {};
/// Sets the [value] of config [key] /// Returns the value of config [variable]
void setVariable(String key, dynamic value) { String getValue(String variable) {
try {
return config.getValue(configPointer.value, variable);
} catch (e) {
rethrow;
}
}
/// Sets the [value] of config [variable]
void setValue(String variable, dynamic value) {
try { try {
if (value.runtimeType == bool) { if (value.runtimeType == bool) {
config.setBool(configPointer.value, key, value); config.setBool(configPointer.value, variable, value);
} else if (value.runtimeType == int) { } else if (value.runtimeType == int) {
config.setInt(configPointer.value, key, value); config.setInt(configPointer.value, variable, value);
} else { } else {
config.setString(configPointer.value, key, value); config.setString(configPointer.value, variable, value);
} }
variables = config.getVariables(configPointer.value); variables = config.getVariables(configPointer.value);
} catch (e) { } catch (e) {

View file

@ -43,24 +43,35 @@ void main() {
expect(config, isA<Config>()); expect(config, isA<Config>());
}); });
test('gets entries of file', () { group('getValue()', () {
expect(config.variables['core.repositoryformatversion'], equals('0')); test('returns value of variable', () {
expect(config.getValue('core.bare'), equals('false'));
}); });
test('sets boolean value for provided key', () { test('throws when variable isn\'t found', () {
config.setVariable('core.bare', true); expect(
() => config.getValue('not.there'),
throwsA(isA<LibGit2Error>()),
);
});
});
group('setValue()', () {
test('sets boolean value for provided variable', () {
config.setValue('core.bare', true);
expect(config.variables['core.bare'], equals('true')); expect(config.variables['core.bare'], equals('true'));
}); });
test('sets integer value for provided key', () { test('sets integer value for provided variable', () {
config.setVariable('core.repositoryformatversion', 1); config.setValue('core.repositoryformatversion', 1);
expect(config.variables['core.repositoryformatversion'], equals('1')); expect(config.variables['core.repositoryformatversion'], equals('1'));
}); });
test('sets string value for provided key', () { test('sets string value for provided variable', () {
config.setVariable('remote.origin.url', 'updated'); config.setValue('remote.origin.url', 'updated');
expect(config.variables['remote.origin.url'], equals('updated')); expect(config.variables['remote.origin.url'], equals('updated'));
}); });
});
test('deletes variable', () { test('deletes variable', () {
config.deleteVariable('core.bare'); config.deleteVariable('core.bare');