|
| 1 | +/** |
| 2 | + * Copyright 2025 Yunze Xu |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#pragma once |
| 17 | + |
| 18 | +#include "snctl-cpp/subcommand.h" |
| 19 | +#include <SimpleIni.h> |
| 20 | +#include <argparse/argparse.hpp> |
| 21 | +#include <filesystem> |
| 22 | +#include <iostream> |
| 23 | +#include <optional> |
| 24 | +#include <stdexcept> |
| 25 | +#include <string> |
| 26 | + |
| 27 | +struct KafkaConfigs { |
| 28 | + std::string bootstrap_servers = "localhost:9092"; |
| 29 | + std::string token = ""; |
| 30 | +}; |
| 31 | + |
| 32 | +struct LogConfigs { |
| 33 | + // Whether to enable logging for rdkafka |
| 34 | + bool enabled = true; |
| 35 | + // The file to store logs from rdkafka. If it's empty, the logs will be |
| 36 | + // written to the standard output. |
| 37 | + std::string path = "/tmp/rdkafka.log"; |
| 38 | +}; |
| 39 | + |
| 40 | +class Configs : public SubCommand { |
| 41 | +public: |
| 42 | + Configs(argparse::ArgumentParser &parent) : SubCommand("configs") { |
| 43 | + update_command_.add_description("Update key-value from the INI section"); |
| 44 | + update_command_.add_argument("--kafka-url") |
| 45 | + .help("The Kafka bootstrap.servers"); |
| 46 | + update_command_.add_argument("--kafka-token").help("The Kafka token"); |
| 47 | + |
| 48 | + add_child(update_command_); |
| 49 | + attach_parent(parent); |
| 50 | + } |
| 51 | + |
| 52 | + // This method must be called after parent.parse_args() is called |
| 53 | + void init(argparse::ArgumentParser &parent) { |
| 54 | + for (auto &&file : parent.get<std::vector<std::string>>("config")) { |
| 55 | + if (!std::filesystem::exists(file)) { |
| 56 | + continue; |
| 57 | + } |
| 58 | + if (loadFile(file)) { |
| 59 | + break; |
| 60 | + } |
| 61 | + } |
| 62 | + if (config_file_.empty()) { |
| 63 | + config_file_ = std::filesystem::current_path() / "sncloud.ini"; |
| 64 | + std::cout << "No config file found. Creating " << config_file_ |
| 65 | + << " with the default configs" << std::endl; |
| 66 | + saveFile(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + void run() { |
| 71 | + if (is_subcommand_used(update_command_)) { |
| 72 | + bool updated = false; |
| 73 | + if (update_command_.present("--kafka-url")) { |
| 74 | + if (auto value = update_command_.get("--kafka-url"); |
| 75 | + value != kafka_configs_.bootstrap_servers) { |
| 76 | + |
| 77 | + kafka_configs_.bootstrap_servers = value; |
| 78 | + std::cout << "Updated bootstrap.servers to " << value << std::endl; |
| 79 | + updated = true; |
| 80 | + } else { |
| 81 | + std::cout << "The provided bootstrap.servers is the same with the " |
| 82 | + "config in " |
| 83 | + << config_file_ << std::endl; |
| 84 | + } |
| 85 | + } |
| 86 | + if (update_command_.present("--kafka-token")) { |
| 87 | + auto value = update_command_.get("--kafka-token"); |
| 88 | + if (value.empty()) { |
| 89 | + throw std::invalid_argument("The token cannot be empty"); |
| 90 | + } |
| 91 | + if (value != kafka_configs_.token) { |
| 92 | + kafka_configs_.token = value; |
| 93 | + updated = true; |
| 94 | + std::cout << "Updated token" << std::endl; |
| 95 | + } else { |
| 96 | + std::cout << "The provided token is the same with the config in " |
| 97 | + << config_file_ << std::endl; |
| 98 | + } |
| 99 | + } |
| 100 | + if (updated) { |
| 101 | + saveFile(); |
| 102 | + std::cout << "Updated config file " << config_file_ << std::endl; |
| 103 | + } else { |
| 104 | + std::cout << "No config updated" << std::endl; |
| 105 | + } |
| 106 | + } else { |
| 107 | + fail(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + const auto &config_file() const noexcept { return config_file_; } |
| 112 | + |
| 113 | + const auto &kafka_configs() const noexcept { return kafka_configs_; } |
| 114 | + |
| 115 | + const auto &log_configs() const noexcept { return log_configs_; } |
| 116 | + |
| 117 | +private: |
| 118 | + argparse::ArgumentParser update_command_{"update"}; |
| 119 | + |
| 120 | + CSimpleIni ini_; |
| 121 | + std::string config_file_; |
| 122 | + KafkaConfigs kafka_configs_; |
| 123 | + LogConfigs log_configs_; |
| 124 | + |
| 125 | + std::optional<std::string> getValue(const std::string §ion, |
| 126 | + const std::string &key) { |
| 127 | + auto value = ini_.GetValue(section.c_str(), key.c_str()); |
| 128 | + if (value == nullptr) { |
| 129 | + return std::nullopt; |
| 130 | + } |
| 131 | + return std::optional(value); |
| 132 | + } |
| 133 | + |
| 134 | + bool loadFile(const std::string &file) { |
| 135 | + if (auto rc = ini_.LoadFile(file.c_str()); rc != SI_OK) { |
| 136 | + std::cerr << "Failed to load existing file " << file << ": " << rc |
| 137 | + << std::endl; |
| 138 | + return false; |
| 139 | + } |
| 140 | + |
| 141 | + // reset configs |
| 142 | + kafka_configs_ = {}; |
| 143 | + log_configs_ = {}; |
| 144 | + |
| 145 | + // load configs from the INI file |
| 146 | + if (auto value = getValue("kafka", "bootstrap.servers"); value) { |
| 147 | + kafka_configs_.bootstrap_servers = *value; |
| 148 | + } else { |
| 149 | + std::cerr << "No bootstrap.servers found in the kafka section. Use the " |
| 150 | + "default value: " |
| 151 | + << kafka_configs_.bootstrap_servers << std::endl; |
| 152 | + } |
| 153 | + if (auto value = getValue("kafka", "token"); value) { |
| 154 | + kafka_configs_.token = *value; |
| 155 | + } |
| 156 | + if (auto value = getValue("log", "enabled"); value) { |
| 157 | + log_configs_.enabled = std::string(*value) != "false"; |
| 158 | + if (log_configs_.enabled) { |
| 159 | + if (auto value = getValue("log", "path"); value) { |
| 160 | + log_configs_.path = *value; |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + config_file_ = file; |
| 166 | + return true; |
| 167 | + } |
| 168 | + |
| 169 | + void saveFile() { |
| 170 | + ini_.SetValue("kafka", "bootstrap.servers", |
| 171 | + kafka_configs_.bootstrap_servers.c_str()); |
| 172 | + ini_.SetValue("kafka", "token", kafka_configs_.token.c_str()); |
| 173 | + ini_.SetBoolValue("log", "enabled", log_configs_.enabled); |
| 174 | + if (log_configs_.enabled) { |
| 175 | + ini_.SetValue("log", "path", log_configs_.path.c_str()); |
| 176 | + } |
| 177 | + ini_.SaveFile(config_file_.c_str()); |
| 178 | + } |
| 179 | +}; |
0 commit comments