diff --git a/lib/src/bindings/config.dart b/lib/src/bindings/config.dart index 32fd168..5cc6e6f 100644 --- a/lib/src/bindings/config.dart +++ b/lib/src/bindings/config.dart @@ -174,3 +174,72 @@ String getString(Pointer cfg, String variable) { final value = getConfigValue(cfg, variable); return value.cast().toDartString(); } + +/// Set the value of a boolean config variable in the config file with the +/// highest level (usually the local one). +/// +/// Throws a [LibGit2Error] if error occured. +void setBool(Pointer cfg, String name, bool value) { + final nameC = name.toNativeUtf8().cast(); + final valueC = value ? 1 : 0; + final error = libgit2.git_config_set_bool(cfg, nameC, valueC); + calloc.free(nameC); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } +} + +/// Set the value of an integer config variable in the config file with the +/// highest level (usually the local one). +void setInt(Pointer cfg, String name, int value) { + final nameC = name.toNativeUtf8().cast(); + final error = libgit2.git_config_set_int64(cfg, nameC, value); + calloc.free(nameC); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } +} + +/// Set the value of a string config variable in the config file with the +/// highest level (usually the local one). +void setString(Pointer cfg, String name, String value) { + final nameC = name.toNativeUtf8().cast(); + final valueC = value.toNativeUtf8().cast(); + final error = libgit2.git_config_set_string(cfg, nameC, valueC); + calloc.free(nameC); + calloc.free(valueC); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } +} + +/// Iterate over all the config variables. +Map getEntries(Pointer cfg) { + final iterator = calloc>(); + final entry = calloc>(); + libgit2.git_config_iterator_new(iterator, cfg); + var error = 0; + final entries = {}; + + while (error != -31) { + error = libgit2.git_config_next(entry, iterator.value); + entries[entry.value.ref.name.cast().toDartString()] = + entry.value.ref.value.cast().toDartString(); + } + calloc.free(entry); + calloc.free(iterator); + + return entries; +} + +/// Iterate over the values of multivar +// TODO + +/// Multivars variables get/set +// TODO + +/// Deletes a config variable +// TODO \ No newline at end of file diff --git a/lib/src/config.dart b/lib/src/config.dart index 0b54c53..122919e 100644 --- a/lib/src/config.dart +++ b/lib/src/config.dart @@ -19,9 +19,79 @@ class Config { libgit2.git_libgit2_init(); if (path == null) { - configPointer = config.openDefault(); + try { + configPointer = config.openDefault(); + } catch (e) { + rethrow; + } } else { - configPointer = config.open(path!); + try { + configPointer = config.open(path!); + } catch (e) { + rethrow; + } + } + + entries = config.getEntries(configPointer.value); + + libgit2.git_libgit2_shutdown(); + } + + /// Initializes a new instance of [Config] class. + /// + /// Opens the system configuration file. + /// + /// Throws a [LibGit2Error] if error occured. + Config.system() { + libgit2.git_libgit2_init(); + + try { + final systemPath = config.findSystem(); + configPointer = config.open(systemPath); + entries = config.getEntries(configPointer.value); + } catch (e) { + configPointer = nullptr; + rethrow; + } + + libgit2.git_libgit2_shutdown(); + } + + /// Initializes a new instance of [Config] class. + /// + /// Opens the global configuration file. + /// + /// Throws a [LibGit2Error] if error occured. + Config.global() { + libgit2.git_libgit2_init(); + + try { + final globalPath = config.findGlobal(); + configPointer = config.open(globalPath); + entries = config.getEntries(configPointer.value); + } catch (e) { + configPointer = nullptr; + rethrow; + } + + libgit2.git_libgit2_shutdown(); + } + + /// Initializes a new instance of [Config] class. + /// + /// Opens the global XDG configuration file. + /// + /// Throws a [LibGit2Error] if error occured. + Config.xdg() { + libgit2.git_libgit2_init(); + + try { + final xdgPath = config.findXdg(); + configPointer = config.open(xdgPath); + entries = config.getEntries(configPointer.value); + } catch (e) { + configPointer = nullptr; + rethrow; } libgit2.git_libgit2_shutdown(); @@ -33,24 +103,25 @@ class Config { /// Pointer to memory address for allocated config object. late Pointer> configPointer; - /// Get boolean value of `key` [variable] - bool getBool(String variable) { - return config.getBool(configPointer.value, variable); - } + /// Map of key/value entries from config file. + Map entries = {}; - ///Get integer value of `key` [variable] - int getInt(String variable) { - return config.getInt(configPointer.value, variable); + /// Sets value of config key + void setEntry(String key, dynamic value) { + try { + if (value.runtimeType == bool) { + config.setBool(configPointer.value, key, value); + } else if (value.runtimeType == int) { + config.setInt(configPointer.value, key, value); + } else { + config.setString(configPointer.value, key, value); + } + entries = config.getEntries(configPointer.value); + } catch (e) { + rethrow; + } } - ///Get string value of `key` [variable] - String getString(String variable) { - return config.getString(configPointer.value, variable); - } - - /// Set value of config key - // TODO - /// Releases memory allocated for config object. void close() { calloc.free(configPointer);