mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-04 12:19:09 -04:00
feat(config): add ability to get values of all variables
This commit is contained in:
parent
f6b8cc7005
commit
2d1c026e73
4 changed files with 22 additions and 19 deletions
|
@ -5,8 +5,8 @@ void main() {
|
|||
final config = Config.open();
|
||||
|
||||
print('All entries of system/global config:');
|
||||
// config.variables hold key/value from config file
|
||||
for (final entry in config.variables.entries) {
|
||||
final entries = config.getEntries();
|
||||
for (final entry in entries.entries) {
|
||||
print('${entry.key}: ${entry.value}');
|
||||
}
|
||||
// .close should be called on object to free memory when done.
|
||||
|
@ -18,7 +18,8 @@ void main() {
|
|||
final repoConfig = Config.open(path: '.git/config');
|
||||
|
||||
print('All entries of repo config:');
|
||||
for (final entry in repoConfig.variables.entries) {
|
||||
final entries = repoConfig.getEntries();
|
||||
for (final entry in entries.entries) {
|
||||
print('${entry.key}: ${entry.value}');
|
||||
}
|
||||
|
||||
|
@ -32,7 +33,8 @@ void main() {
|
|||
try {
|
||||
final globalConfig = Config.global();
|
||||
|
||||
final userName = globalConfig.variables['user.name'];
|
||||
// get value of config variable
|
||||
final userName = globalConfig.getValue('user.name');
|
||||
print('\nUser Name from global config: $userName');
|
||||
|
||||
globalConfig.close();
|
||||
|
|
|
@ -182,12 +182,12 @@ void setString(Pointer<git_config> cfg, String name, String value) {
|
|||
}
|
||||
|
||||
/// Iterate over all the config variables.
|
||||
Map<String, dynamic> getVariables(Pointer<git_config> cfg) {
|
||||
Map<String, String> getEntries(Pointer<git_config> cfg) {
|
||||
final iterator = calloc<Pointer<git_config_iterator>>();
|
||||
final entry = calloc<Pointer<git_config_entry>>();
|
||||
libgit2.git_config_iterator_new(iterator, cfg);
|
||||
var error = 0;
|
||||
final entries = <String, dynamic>{};
|
||||
final entries = <String, String>{};
|
||||
|
||||
while (error != -31) {
|
||||
error = libgit2.git_config_next(entry, iterator.value);
|
||||
|
|
|
@ -39,8 +39,6 @@ class Config {
|
|||
}
|
||||
}
|
||||
|
||||
variables = config.getVariables(configPointer.value);
|
||||
|
||||
libgit2.git_libgit2_shutdown();
|
||||
}
|
||||
|
||||
|
@ -55,7 +53,6 @@ class Config {
|
|||
try {
|
||||
final systemPath = config.findSystem();
|
||||
configPointer = config.open(systemPath);
|
||||
variables = config.getVariables(configPointer.value);
|
||||
} catch (e) {
|
||||
configPointer = nullptr;
|
||||
rethrow;
|
||||
|
@ -75,7 +72,6 @@ class Config {
|
|||
try {
|
||||
final globalPath = config.findGlobal();
|
||||
configPointer = config.open(globalPath);
|
||||
variables = config.getVariables(configPointer.value);
|
||||
} catch (e) {
|
||||
configPointer = nullptr;
|
||||
rethrow;
|
||||
|
@ -95,7 +91,6 @@ class Config {
|
|||
try {
|
||||
final xdgPath = config.findXdg();
|
||||
configPointer = config.open(xdgPath);
|
||||
variables = config.getVariables(configPointer.value);
|
||||
} catch (e) {
|
||||
configPointer = nullptr;
|
||||
rethrow;
|
||||
|
@ -110,8 +105,10 @@ class Config {
|
|||
/// Pointer to memory address for allocated config object.
|
||||
late Pointer<Pointer<git_config>> configPointer;
|
||||
|
||||
/// Map of key/value entries from config file.
|
||||
Map<String, dynamic> variables = {};
|
||||
/// Returns map of all the config variables and their values
|
||||
Map<String, String> getEntries() {
|
||||
return config.getEntries(configPointer.value);
|
||||
}
|
||||
|
||||
/// Returns the value of config [variable]
|
||||
String getValue(String variable) {
|
||||
|
@ -132,7 +129,6 @@ class Config {
|
|||
} else {
|
||||
config.setString(configPointer.value, variable, value);
|
||||
}
|
||||
variables = config.getVariables(configPointer.value);
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
|
@ -145,7 +141,6 @@ class Config {
|
|||
void deleteVariable(String key) {
|
||||
try {
|
||||
config.deleteVariable(configPointer.value, key);
|
||||
variables = config.getVariables(configPointer.value);
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
|
|
|
@ -43,6 +43,11 @@ void main() {
|
|||
expect(config, isA<Config>());
|
||||
});
|
||||
|
||||
test('getEntries() returns map with variables and values', () {
|
||||
final entries = config.getEntries();
|
||||
expect(entries['remote.origin.url'], equals('someurl'));
|
||||
});
|
||||
|
||||
group('getValue()', () {
|
||||
test('returns value of variable', () {
|
||||
expect(config.getValue('core.bare'), equals('false'));
|
||||
|
@ -59,23 +64,24 @@ void main() {
|
|||
group('setValue()', () {
|
||||
test('sets boolean value for provided variable', () {
|
||||
config.setValue('core.bare', true);
|
||||
expect(config.variables['core.bare'], equals('true'));
|
||||
expect(config.getValue('core.bare'), equals('true'));
|
||||
});
|
||||
|
||||
test('sets integer value for provided variable', () {
|
||||
config.setValue('core.repositoryformatversion', 1);
|
||||
expect(config.variables['core.repositoryformatversion'], equals('1'));
|
||||
expect(config.getValue('core.repositoryformatversion'), equals('1'));
|
||||
});
|
||||
|
||||
test('sets string value for provided variable', () {
|
||||
config.setValue('remote.origin.url', 'updated');
|
||||
expect(config.variables['remote.origin.url'], equals('updated'));
|
||||
expect(config.getValue('remote.origin.url'), equals('updated'));
|
||||
});
|
||||
});
|
||||
|
||||
test('deletes variable', () {
|
||||
config.deleteVariable('core.bare');
|
||||
expect(config.variables['core.bare'], isNull);
|
||||
final entries = config.getEntries();
|
||||
expect(entries['core.bare'], isNull);
|
||||
});
|
||||
|
||||
test('throws on deleting non existing variable', () {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue