From 6992624b19759578a2ffd125b34f0b17f7b7a2cc Mon Sep 17 00:00:00 2001 From: Wvader <34067397+wvader@users.noreply.github.com> Date: Fri, 4 Nov 2022 21:10:00 +0000 Subject: [PATCH] Refactor --- README | 5 ++++ README.md | 10 ------- main.cpp | 83 +++++++++++++++++++++++++++++++++++-------------------- main.h | 9 ++++-- 4 files changed, 64 insertions(+), 43 deletions(-) create mode 100644 README delete mode 100644 README.md diff --git a/README b/README new file mode 100644 index 0000000..a4d62dc --- /dev/null +++ b/README @@ -0,0 +1,5 @@ +claugeas - Augeas Wrapper + +Library that facilitates C# and other language bindings. +This is a necessary library for the Augeas C# bindings. + diff --git a/README.md b/README.md deleted file mode 100644 index 0022c5b..0000000 --- a/README.md +++ /dev/null @@ -1,10 +0,0 @@ -# clAugeas - -Lbrary of functions for editing of relevant files to manage development environments. - -## TODO : -- Possibility to create a collection of sites for the development environment (dev, quality and production) each site can be of the following types: - 1) static with SSL - 2) reverse Proxy to existing service (systemd) with SSL. -- The domain must be valid -- The IP must be valid. \ No newline at end of file diff --git a/main.cpp b/main.cpp index 2488a4a..f86dc72 100644 --- a/main.cpp +++ b/main.cpp @@ -8,62 +8,86 @@ inline bool path_exists (const std::string& name) { return (stat (name.c_str(), &buffer) == 0); } -CLAPI void testSource(const augSettings settings) { - int r; - struct augeas *aug; - char *s; +CLAPI void* initAug(augSettings settings, int flags) { - aug = aug_init(settings.root, settings.loadPath, AUG_NO_STDINC | AUG_NO_LOAD); + if(!path_exists(std::string(settings.root))) { + std::cout << "error: root path is invalid." << settings.root << std::endl; + return nullptr; + } - r = aug_load_file(aug, "/etc/hosts"); - r = aug_source(aug, "/files/etc/hosts/1/ipaddr", nullptr); - - std::cout << "The value read from node: " << s << std::endl; - - aug_close(aug); + return aug_init(settings.root, settings.loadPath, flags); } -// This prints the actual file -CLAPI void printPreview(const augSettings settings, const char* matchPath, const char* filePath) { - struct augeas *aug; + +CLAPI void closeAug(void* aug) { + aug_close((augeas*)aug); +} + +// Wrapper of aug_preview +CLAPI void printPreview(augeas* aug, const char* matchPath, const char* filePath) { + + if(aug == nullptr) { + std::cout << "error: augeas is not initialized." << std::endl; + return; + } + int r; char *s; char *etc_hosts_fn = nullptr; FILE *hosts_fp = nullptr; char *hosts_txt = nullptr; - aug = aug_init(settings.root, settings.loadPath, AUG_NO_STDINC | AUG_NO_LOAD); + + r = aug_load_file(aug,filePath); - r = aug_load_file(aug, filePath); - r = aug_preview(aug, matchPath, &s); + if(r != 0) { + std::cout << "Error loading file." << std::endl; + return; + } + + r = aug_preview(aug,matchPath,&s); + + if(r != 0) { + std::cout << "Failure previewing." << std::endl; + return; + } std::cout << s << std::endl; free(hosts_txt); free(s); - aug_close(aug); } +// Print tree of the matchPath CLAPI void printAugTree( - const augSettings settings, + augeas *aug, const char* matchPath, const char* filePath ) { - struct augeas *aug; + + if(aug == nullptr) { + std::cout << "error: augeas is not initialized." << std::endl; + return; + } + int r; + FILE *out = tmpfile(); - aug = aug_init(settings.root, settings.loadPath, AUG_NO_STDINC | AUG_NO_LOAD); - - if(!path_exists(std::string(settings.root))) { - std::cout << "ERROR Path is invalid: " << settings.root << std::endl; - } - std::cout << settings.root<< std::endl; - std::cout << settings.loadPath<< std::endl; - r = aug_load_file(aug, filePath); + + if(r != 0) { + std::cout << "error: can't load file." << std::endl; + return; + } + r = aug_print(aug, out,matchPath); + if(r != 0) { + std::cout << "error: aug_print failure." << std::endl; + return; + } + std::map stdBindList; std::map ::iterator pos; @@ -99,7 +123,6 @@ CLAPI void printAugTree( std::cout << "Key: " << pos->first << ";" << " Value: " << pos->second << std::endl; } - aug_close(aug); -}; +} } diff --git a/main.h b/main.h index a0273e5..fca0c16 100644 --- a/main.h +++ b/main.h @@ -13,12 +13,15 @@ extern "C" { -CLAPI void printPreview (augSettings settings, +CLAPI void* initAug (augSettings settings, int flags); + +CLAPI void printPreview (augeas* aug, const char* matchPath, const char* filePath); - -CLAPI void printAugTree (augSettings settings, +CLAPI void printAugTree (augeas* aug, const char* matchPath, const char* filePath); + +CLAPI void closeAug (void* aug); }