directory walking working
This commit is contained in:
parent
e6a5581cea
commit
66fd83ac02
@ -17,7 +17,7 @@ project(kc
|
||||
LANGUAGES
|
||||
CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD 23)
|
||||
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
|
||||
|
||||
|
@ -1,4 +1,9 @@
|
||||
add_executable(kc main.cpp)
|
||||
add_executable(kc
|
||||
main.cpp
|
||||
fs/fs.cpp
|
||||
logging.cpp
|
||||
config.cpp
|
||||
)
|
||||
|
||||
FetchContent_Declare(
|
||||
Boost
|
||||
|
123
src/config.cpp
123
src/config.cpp
@ -1,92 +1,61 @@
|
||||
#include "config.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
#include <boost/program_options.hpp>
|
||||
|
||||
using namespace std;
|
||||
namespace po = boost::program_options;
|
||||
|
||||
void on_age(int age)
|
||||
std::shared_ptr<po::variables_map> init_config(int argc, const char *argv[])
|
||||
{
|
||||
BOOST_LOG_TRIVIAL(info) << "On age: " << age;
|
||||
}
|
||||
try {
|
||||
po::options_description desc("Config");
|
||||
desc.add_options()
|
||||
("help", "produce help message")
|
||||
("path,p", po::value<std::string>()->default_value("."), "set root path of knowledge base")
|
||||
("config", po::value<std::string>()->default_value("kc.ini"), "config file location")
|
||||
;
|
||||
|
||||
shared_ptr<po::variables_map> init_config(int argc, const char *argv[])
|
||||
{
|
||||
int def;
|
||||
po::options_description desc("Allowed options");
|
||||
desc.add_options()
|
||||
("help", "produce help message")
|
||||
("compression,c", po::value<int>(), "set compression level")
|
||||
("def,d", po::value<int>(&def)->default_value(10), "set def level")
|
||||
("abc", po::value<int>()->default_value(25), "set abc level")
|
||||
("phone", po::value<std::vector<std::string>>()->multitoken()->zero_tokens()->composing(), "Phone")
|
||||
("age,a", po::value<int>()->notifier(on_age), "age")
|
||||
("config", po::value<std::string>(), "Config file")
|
||||
;
|
||||
po::options_description cmdline_options;
|
||||
cmdline_options.add(desc);
|
||||
|
||||
po::options_description other("Other");
|
||||
desc.add_options()
|
||||
("other-option,o", po::value<int>(), "another option")
|
||||
;
|
||||
po::options_description config_file_options;
|
||||
config_file_options.add(desc);
|
||||
|
||||
// Hidden options, will be allowed both on command line and
|
||||
// in config file, but will not be shown to the user.
|
||||
po::options_description hidden("Hidden options");
|
||||
hidden.add_options()
|
||||
("input-file", po::value< vector<string> >(), "input file");
|
||||
////////////
|
||||
// PARSE
|
||||
////////////
|
||||
|
||||
po::positional_options_description p;
|
||||
p.add("phone", -1);
|
||||
auto vm = std::make_shared<po::variables_map>();
|
||||
po::store(po::command_line_parser(argc, argv)
|
||||
.options(cmdline_options)
|
||||
.run(),
|
||||
*vm);
|
||||
|
||||
po::options_description cmdline_options;
|
||||
cmdline_options.add(desc).add(other).add(hidden);
|
||||
|
||||
po::options_description config_file_options;
|
||||
config_file_options.add(desc).add(hidden);
|
||||
|
||||
po::options_description visible("Allowed options");
|
||||
visible.add(desc).add(other);
|
||||
|
||||
////////////
|
||||
// PARSE
|
||||
////////////
|
||||
|
||||
auto vm = make_shared<po::variables_map>();
|
||||
po::store(po::command_line_parser(argc, argv)
|
||||
.options(desc)
|
||||
.positional(p)
|
||||
.run(),
|
||||
*vm);
|
||||
|
||||
if (vm->count("config"))
|
||||
{
|
||||
BOOST_LOG_TRIVIAL(info) << "Attempting file config load";
|
||||
std::ifstream ifs{(*vm)["config"].as<std::string>().c_str()};
|
||||
if (ifs) {
|
||||
BOOST_LOG_TRIVIAL(info) << "File opened, loading...";
|
||||
po::store(po::parse_config_file(ifs, config_file_options), *vm);
|
||||
if (vm->count("config"))
|
||||
{
|
||||
auto config_path = (*vm)["config"].as<std::string>();
|
||||
BOOST_LOG_TRIVIAL(info) << "Attempting file config load for " << config_path;
|
||||
|
||||
std::ifstream ifs{config_path.c_str()};
|
||||
if (ifs) {
|
||||
BOOST_LOG_TRIVIAL(info) << "File opened, loading...";
|
||||
po::store(po::parse_config_file(ifs, config_file_options), *vm);
|
||||
}
|
||||
}
|
||||
po::notify(*vm);
|
||||
|
||||
if (vm->count("help")) {
|
||||
std::cout << desc;
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return vm;
|
||||
|
||||
}
|
||||
po::notify(*vm);
|
||||
|
||||
if (vm->count("help")) {
|
||||
BOOST_LOG_TRIVIAL(info) << desc;
|
||||
|
||||
return nullptr;
|
||||
catch (const po::error &ex)
|
||||
{
|
||||
BOOST_LOG_TRIVIAL(error) << ex.what();
|
||||
}
|
||||
|
||||
if (vm->count("compression")) {
|
||||
BOOST_LOG_TRIVIAL(info) << "Compression level was set to " << (*vm)["compression"].as<int>() << ".";
|
||||
} else {
|
||||
BOOST_LOG_TRIVIAL(info) << "Compression level was not set.";
|
||||
}
|
||||
|
||||
if (vm->count("def")) {
|
||||
BOOST_LOG_TRIVIAL(info) << "def level was set to " << (*vm)["def"].as<int>() << ".";
|
||||
} else {
|
||||
BOOST_LOG_TRIVIAL(info) << "def level was not set.";
|
||||
}
|
||||
|
||||
return vm;
|
||||
}
|
8
src/config.hpp
Normal file
8
src/config.hpp
Normal file
@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <boost/program_options.hpp>
|
||||
#include <boost/log/trivial.hpp>
|
||||
|
||||
namespace po = boost::program_options;
|
||||
|
||||
std::shared_ptr<po::variables_map> init_config(int argc, const char *argv[]);
|
1
src/const.hpp
Normal file
1
src/const.hpp
Normal file
@ -0,0 +1 @@
|
||||
#pragma once
|
18
src/fs/FileEntry.hpp
Normal file
18
src/fs/FileEntry.hpp
Normal file
@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace kc {
|
||||
|
||||
class FileEntry {
|
||||
public:
|
||||
|
||||
fs::directory_entry file_entry;
|
||||
fs::path relative_path;
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
}
|
39
src/fs/fs.cpp
Normal file
39
src/fs/fs.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
#include "fs.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
static const std::string exclusions[] = {".git", ".obsidian"};
|
||||
|
||||
std::vector<kc::FileEntry> kc::walk_dir(std::string dir)
|
||||
{
|
||||
auto matched = std::vector<kc::FileEntry>();
|
||||
auto base_path = fs::path(dir);
|
||||
|
||||
for (auto const& dir_entry : fs::recursive_directory_iterator(base_path))
|
||||
{
|
||||
auto excluded = false;
|
||||
auto dir_entry_path = dir_entry.path();
|
||||
auto dir_entry_path_string = dir_entry_path.string();
|
||||
|
||||
for (auto const& exclusion: exclusions)
|
||||
{
|
||||
if (dir_entry_path_string.contains(exclusion))
|
||||
{
|
||||
excluded = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!excluded)
|
||||
{
|
||||
auto entry = kc::FileEntry();
|
||||
|
||||
entry.file_entry = dir_entry;
|
||||
entry.relative_path = fs::relative(dir_entry_path, base_path);
|
||||
|
||||
matched.push_back(entry);
|
||||
}
|
||||
}
|
||||
|
||||
return matched;
|
||||
}
|
14
src/fs/fs.hpp
Normal file
14
src/fs/fs.hpp
Normal file
@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
#include "FileEntry.hpp"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace kc {
|
||||
|
||||
std::vector<kc::FileEntry> walk_dir(std::string dir);
|
||||
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
#include "logging.hpp"
|
||||
|
||||
#include <boost/log/core.hpp>
|
||||
#include <boost/log/trivial.hpp>
|
||||
#include <boost/log/expressions.hpp>
|
||||
|
3
src/logging.hpp
Normal file
3
src/logging.hpp
Normal file
@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
void init_logging();
|
34
src/main.cpp
34
src/main.cpp
@ -4,29 +4,27 @@
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
|
||||
#include "logging.cpp"
|
||||
#include "config.cpp"
|
||||
|
||||
using namespace std;
|
||||
#include "const.hpp"
|
||||
#include "logging.hpp"
|
||||
#include "config.hpp"
|
||||
#include "fs/fs.hpp"
|
||||
|
||||
int main(int argc, const char *argv[]) {
|
||||
try{
|
||||
init_logging();
|
||||
|
||||
init_logging();
|
||||
|
||||
BOOST_LOG_TRIVIAL(info) << "================================";
|
||||
BOOST_LOG_TRIVIAL(info) << " kc";
|
||||
BOOST_LOG_TRIVIAL(info) << "================================";
|
||||
BOOST_LOG_TRIVIAL(info) << "starting up....";
|
||||
BOOST_LOG_TRIVIAL(info) << "================================";
|
||||
BOOST_LOG_TRIVIAL(info) << " kc";
|
||||
BOOST_LOG_TRIVIAL(info) << "================================";
|
||||
BOOST_LOG_TRIVIAL(info) << "starting up....";
|
||||
|
||||
auto config = init_config(argc, argv);
|
||||
auto config = init_config(argc, argv);
|
||||
|
||||
if(config)
|
||||
{
|
||||
BOOST_LOG_TRIVIAL(info) << "hello world";
|
||||
}
|
||||
}
|
||||
catch (const po::error &ex)
|
||||
if(config)
|
||||
{
|
||||
BOOST_LOG_TRIVIAL(error) << ex.what();
|
||||
auto env_path = (*config)["path"].as<std::string>();
|
||||
BOOST_LOG_TRIVIAL(info) << "Loading knowledge base from " << env_path;
|
||||
|
||||
kc::walk_dir(env_path);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user