mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-05 04:39:07 -04:00
refactor!: change api
This commit is contained in:
parent
3aa322ea59
commit
570e5bad52
2 changed files with 16 additions and 9 deletions
|
@ -217,7 +217,7 @@ void setString(Pointer<git_config> cfg, String name, String value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Iterate over all the config variables.
|
/// Iterate over all the config variables.
|
||||||
Map<String, dynamic> getEntries(Pointer<git_config> cfg) {
|
Map<String, dynamic> getVariables(Pointer<git_config> cfg) {
|
||||||
final iterator = calloc<Pointer<git_config_iterator>>();
|
final iterator = calloc<Pointer<git_config_iterator>>();
|
||||||
final entry = calloc<Pointer<git_config_entry>>();
|
final entry = calloc<Pointer<git_config_entry>>();
|
||||||
libgit2.git_config_iterator_new(iterator, cfg);
|
libgit2.git_config_iterator_new(iterator, cfg);
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import 'dart:ffi';
|
import 'dart:ffi';
|
||||||
|
import 'dart:io';
|
||||||
import 'package:ffi/ffi.dart';
|
import 'package:ffi/ffi.dart';
|
||||||
import 'bindings/libgit2_bindings.dart';
|
import 'bindings/libgit2_bindings.dart';
|
||||||
import 'bindings/config.dart' as config;
|
import 'bindings/config.dart' as config;
|
||||||
|
@ -7,6 +8,8 @@ import 'util.dart';
|
||||||
/// [Config] provides management of global configuration options
|
/// [Config] provides management of global configuration options
|
||||||
/// (system, global, XDG, excluding repository config)
|
/// (system, global, XDG, excluding repository config)
|
||||||
class Config {
|
class Config {
|
||||||
|
Config();
|
||||||
|
|
||||||
/// Initializes a new instance of [Config] class.
|
/// Initializes a new instance of [Config] class.
|
||||||
///
|
///
|
||||||
/// If [path] isn't provided, opens global, XDG and system config files.
|
/// If [path] isn't provided, opens global, XDG and system config files.
|
||||||
|
@ -15,7 +18,7 @@ class Config {
|
||||||
/// Git config file following the default Git config syntax (see man git-config).
|
/// Git config file following the default Git config syntax (see man git-config).
|
||||||
///
|
///
|
||||||
/// [Config] object should be closed with [close] function to release allocated memory.
|
/// [Config] object should be closed with [close] function to release allocated memory.
|
||||||
Config({this.path}) {
|
Config.open({this.path}) {
|
||||||
libgit2.git_libgit2_init();
|
libgit2.git_libgit2_init();
|
||||||
|
|
||||||
if (path == null) {
|
if (path == null) {
|
||||||
|
@ -26,13 +29,17 @@ class Config {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
|
if (File(path!).existsSync()) {
|
||||||
configPointer = config.open(path!);
|
configPointer = config.open(path!);
|
||||||
|
} else {
|
||||||
|
throw Exception('File not found');
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
rethrow;
|
rethrow;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
entries = config.getEntries(configPointer.value);
|
variables = config.getVariables(configPointer.value);
|
||||||
|
|
||||||
libgit2.git_libgit2_shutdown();
|
libgit2.git_libgit2_shutdown();
|
||||||
}
|
}
|
||||||
|
@ -48,7 +55,7 @@ class Config {
|
||||||
try {
|
try {
|
||||||
final systemPath = config.findSystem();
|
final systemPath = config.findSystem();
|
||||||
configPointer = config.open(systemPath);
|
configPointer = config.open(systemPath);
|
||||||
entries = config.getEntries(configPointer.value);
|
variables = config.getVariables(configPointer.value);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
configPointer = nullptr;
|
configPointer = nullptr;
|
||||||
rethrow;
|
rethrow;
|
||||||
|
@ -68,7 +75,7 @@ class Config {
|
||||||
try {
|
try {
|
||||||
final globalPath = config.findGlobal();
|
final globalPath = config.findGlobal();
|
||||||
configPointer = config.open(globalPath);
|
configPointer = config.open(globalPath);
|
||||||
entries = config.getEntries(configPointer.value);
|
variables = config.getVariables(configPointer.value);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
configPointer = nullptr;
|
configPointer = nullptr;
|
||||||
rethrow;
|
rethrow;
|
||||||
|
@ -88,7 +95,7 @@ class Config {
|
||||||
try {
|
try {
|
||||||
final xdgPath = config.findXdg();
|
final xdgPath = config.findXdg();
|
||||||
configPointer = config.open(xdgPath);
|
configPointer = config.open(xdgPath);
|
||||||
entries = config.getEntries(configPointer.value);
|
variables = config.getVariables(configPointer.value);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
configPointer = nullptr;
|
configPointer = nullptr;
|
||||||
rethrow;
|
rethrow;
|
||||||
|
@ -104,7 +111,7 @@ class Config {
|
||||||
late Pointer<Pointer<git_config>> configPointer;
|
late Pointer<Pointer<git_config>> configPointer;
|
||||||
|
|
||||||
/// Map of key/value entries from config file.
|
/// Map of key/value entries from config file.
|
||||||
Map<String, dynamic> entries = {};
|
Map<String, dynamic> variables = {};
|
||||||
|
|
||||||
/// Sets value of config key
|
/// Sets value of config key
|
||||||
void setEntry(String key, dynamic value) {
|
void setEntry(String key, dynamic value) {
|
||||||
|
@ -116,7 +123,7 @@ class Config {
|
||||||
} else {
|
} else {
|
||||||
config.setString(configPointer.value, key, value);
|
config.setString(configPointer.value, key, value);
|
||||||
}
|
}
|
||||||
entries = config.getEntries(configPointer.value);
|
variables = config.getVariables(configPointer.value);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
rethrow;
|
rethrow;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue