|
20 | 20 | #include "list_topics.h" |
21 | 21 | #include <argparse/argparse.hpp> |
22 | 22 | #include <array> |
| 23 | +#include <cstdlib> |
23 | 24 | #include <cstring> |
| 25 | +#include <filesystem> |
24 | 26 | #include <librdkafka/rdkafka.h> |
25 | 27 | #include <memory> |
26 | 28 | #include <stdexcept> |
27 | 29 | #include <string> |
28 | 30 | #include <type_traits> |
| 31 | +#include <vector> |
29 | 32 |
|
30 | 33 | int main(int argc, char *argv[]) { |
31 | 34 | const auto version = "0.1.0"; |
| 35 | + |
| 36 | + std::vector<std::string> default_config_paths{ |
| 37 | + std::filesystem::current_path() / "sncloud.ini", |
| 38 | + std::filesystem::path(std::getenv("HOME")) / ".snctl-cpp" / |
| 39 | + "sncloud.ini"}; |
| 40 | + |
32 | 41 | argparse::ArgumentParser program("snctl-cpp", version); |
33 | 42 | program.add_argument("--config") |
34 | | - .default_value("sncloud.ini") |
| 43 | + .default_value(default_config_paths) |
35 | 44 | .help("Path to the config file"); |
36 | 45 | program.add_argument("--client-id").help("client id"); |
37 | 46 |
|
@@ -67,11 +76,22 @@ int main(int argc, char *argv[]) { |
67 | 76 | throw std::runtime_error("Failed to " + action + ": " + errstr.data()); |
68 | 77 | }; |
69 | 78 |
|
70 | | - const auto config_file = program.get("--config"); |
| 79 | + const auto config_files = program.get<std::vector<std::string>>("--config"); |
71 | 80 | CSimpleIni ini; |
72 | | - if (auto rc = ini.LoadFile(config_file.c_str()); rc < 0) { |
73 | | - throw std::runtime_error("Error loading config file " + config_file); |
| 81 | + bool loaded = false; |
| 82 | + for (auto &&config_file : config_files) { |
| 83 | + if (std::filesystem::exists(config_file)) { |
| 84 | + if (auto rc = ini.LoadFile(config_file.c_str()); rc < 0) { |
| 85 | + throw std::runtime_error("Error loading config file " + config_file); |
| 86 | + } |
| 87 | + loaded = true; |
| 88 | + break; |
| 89 | + } |
74 | 90 | } |
| 91 | + if (!loaded) { |
| 92 | + throw std::runtime_error("No config file found"); |
| 93 | + } |
| 94 | + |
75 | 95 | auto get_value = [&ini](const auto &key, bool required) { |
76 | 96 | auto value = ini.GetValue("kafka", key, ""); |
77 | 97 | if (strlen(value) == 0 && required) { |
@@ -112,7 +132,6 @@ int main(int argc, char *argv[]) { |
112 | 132 | rd_kafka_conf_set_opaque(rk_conf, stdout); |
113 | 133 | } else { |
114 | 134 | file.reset(fopen(log_file, "a")); |
115 | | - std::cout << "Opened log file " << log_file << std::endl; |
116 | 135 | rd_kafka_conf_set_opaque(rk_conf, file.get()); |
117 | 136 | rd_kafka_conf_set_log_cb( |
118 | 137 | rk_conf, +[](const rd_kafka_t *rk, int level, const char *fac, |
|
0 commit comments