claugeas/main.cpp

118 lines
3.1 KiB
C++
Raw Normal View History

2022-11-02 18:25:09 +03:00
#include <iostream>
#include <exception>
#include <augeas.h>
2022-11-02 21:27:10 +03:00
#include <vector>
#include <map>
// Testing and training using augeas with C++
2022-11-02 19:27:57 +03:00
// https://github.com/hercules-team/augeas/blob/master/tests/test-api.c
2022-11-02 18:25:09 +03:00
2022-11-02 19:27:57 +03:00
static const char *abs_top_srcdir;
static const char *root = "../root";
static const char *loadpath = "/opt/homebrew/share/augeas/lenses/dist";
static void testGet() {
int r;
const char *value;
const char *label;
struct augeas *aug;
char *s;
2022-11-02 20:12:19 +03:00
aug = aug_init(root, loadpath, AUG_NO_STDINC | AUG_NO_LOAD);
2022-11-02 19:27:57 +03:00
r = aug_load_file(aug, "/etc/hosts");
2022-11-02 22:13:01 +03:00
r = aug_source(aug, "/files/etc/hosts/1/ipaddr", NULL);
2022-11-02 19:27:57 +03:00
std::cout << "The value read from node: " << s << std::endl;
aug_close(aug);
}
// This prints the actual file
static void printPreview(const std::string& matchPath, const std::string& filePath) {
2022-11-02 19:27:57 +03:00
struct augeas *aug;
int r;
char *s;
char *etc_hosts_fn = NULL;
FILE *hosts_fp = NULL;
char *hosts_txt = NULL;
2022-11-02 18:25:09 +03:00
2022-11-02 20:12:19 +03:00
aug = aug_init(root, loadpath, AUG_NO_STDINC | AUG_NO_LOAD);
2022-11-02 18:25:09 +03:00
r = aug_load_file(aug, filePath.c_str());
r = aug_preview(aug, matchPath.c_str(), &s);
2022-11-02 19:27:57 +03:00
std::cout << s << std::endl;
free(hosts_txt);
free(s);
aug_close(aug);
2022-11-02 18:25:09 +03:00
}
2022-11-02 21:27:10 +03:00
/// Get the tree of the /etc/hosts file printed in the console
/// Implementation based on https://github.com/Nexenta/node-augeas/blob/756276c432cc9e506836800ac9d217bedbbef55c/libaugeas.cc#L784
2022-11-02 22:13:01 +03:00
static void printAugTree(const std::string& matchPath, const std::string& filePath) {
2022-11-02 21:27:10 +03:00
struct augeas *aug;
int r;
FILE *out = tmpfile();
aug = aug_init(root, loadpath, AUG_NO_STDINC | AUG_NO_LOAD);
2022-11-02 22:13:01 +03:00
r = aug_load_file(aug, filePath.c_str());
r = aug_print(aug, out,matchPath.c_str());
2022-11-02 21:27:10 +03:00
std::map <std::string, std::string> stdBindList;
std::map <std::string , std::string>::iterator pos;
char line[256];
rewind(out);
while (fgets(line, 256, out) != NULL) {
// remove end of line
line[strlen(line) - 1] = '\0';
std::string s = line;
;
// skip comments
if (s.find("#comment") != std::string::npos)
continue;
s = s.substr(matchPath.length() - 1);
// split by '=' sign
size_t eqpos = s.find(" = ");
if (eqpos == std::string::npos)
continue;
// extract key and value
std::string key = s.substr(0, eqpos);
std::string value = s.substr(eqpos + 3);
// remove '"' sign from around value
value.erase(value.begin());
value.erase(value.end() - 1);
stdBindList.insert(std::pair<std::string,std::string>(key,value)); // 2
}
fclose(out);
for (pos = stdBindList.begin();pos!=stdBindList.end();pos++)
{
std::cout << "Key: " << pos->first << ";" << " Value: " << pos->second << std::endl;
}
aug_close(aug);
}
2022-11-02 19:27:57 +03:00
int main(int argc, char *argv[]) {
2022-11-02 22:13:01 +03:00
//testGet();
printPreview("/files/etc/hosts/1", "/etc/hosts");
//printPreview("/files/etc/ssh/sshd_config/*", "/etc/ssh/sshd_config");
2022-11-02 22:13:01 +03:00
printAugTree("/files/etc/hosts/*", "/etc/hosts");
printAugTree("/files/etc/ssh/sshd_config/*", "/etc/ssh/sshd_config");
2022-11-02 18:25:09 +03:00
return 0;
}