Skip to content

Commit b816d30

Browse files
committed
feat: add ~/.snctl-cpp as the config directory
1 parent 312a655 commit b816d30

File tree

4 files changed

+62
-7
lines changed

4 files changed

+62
-7
lines changed

.github/workflows/release.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ jobs:
4444
cmake -B build
4545
cmake --build build
4646
cp ./build/snctl-cpp .
47+
mkdir
4748
4849
- name: upload
4950
uses: actions/upload-artifact@master
5051
with:
5152
name: snctl-cpp-macos-14-arm64
52-
path: snctl-cpp
53+
path: |
54+
snctl-cpp
55+
sncloud.ini
56+
install.sh

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ Then you can find the executable in `build/snctl-cpp`.
1616

1717
## How to use
1818

19-
You only need to fill the following fields in `sncloud.ini` (make sure this file exists in the current work directory):
19+
**Please make sure the `sncloud.ini` file is in the current working directory or `~/.snctl-cpp` directory if you don't specify the `--config` option.**
20+
21+
You only need to fill the following fields in `sncloud.ini`:
2022
- `bootstrap.servers`: the URL of the Kafka service, e.g. `pc-xxx:9093` on StreamNative cloud.
2123
- (optional) `token`: the token generated by the API key. It's allowed to be empty for quickly testing against a local Kafka cluster without any authentication.
2224

install.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
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+
set -e
17+
cd `dirname $0`
18+
19+
if [[ ! -f snctl-cpp ]]; then
20+
echo "No snctl-cpp found in current directory"
21+
exit 1
22+
fi
23+
if [[ ! -f sncloud.ini ]]; then
24+
echo "No sncloud.ini found in current directory"
25+
exit 1
26+
fi
27+
28+
mkdir -p ~/.snctl-cpp
29+
cp -f snctl-cpp ~/.snctl-cpp/snctl-cpp
30+
cp -f sncloud.ini ~/.snctl-cpp/sncloud.ini

main.cc

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,27 @@
2020
#include "list_topics.h"
2121
#include <argparse/argparse.hpp>
2222
#include <array>
23+
#include <cstdlib>
2324
#include <cstring>
25+
#include <filesystem>
2426
#include <librdkafka/rdkafka.h>
2527
#include <memory>
2628
#include <stdexcept>
2729
#include <string>
2830
#include <type_traits>
31+
#include <vector>
2932

3033
int main(int argc, char *argv[]) {
3134
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+
3241
argparse::ArgumentParser program("snctl-cpp", version);
3342
program.add_argument("--config")
34-
.default_value("sncloud.ini")
43+
.default_value(default_config_paths)
3544
.help("Path to the config file");
3645
program.add_argument("--client-id").help("client id");
3746

@@ -67,11 +76,22 @@ int main(int argc, char *argv[]) {
6776
throw std::runtime_error("Failed to " + action + ": " + errstr.data());
6877
};
6978

70-
const auto config_file = program.get("--config");
79+
const auto config_files = program.get<std::vector<std::string>>("--config");
7180
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+
}
7490
}
91+
if (!loaded) {
92+
throw std::runtime_error("No config file found");
93+
}
94+
7595
auto get_value = [&ini](const auto &key, bool required) {
7696
auto value = ini.GetValue("kafka", key, "");
7797
if (strlen(value) == 0 && required) {
@@ -112,7 +132,6 @@ int main(int argc, char *argv[]) {
112132
rd_kafka_conf_set_opaque(rk_conf, stdout);
113133
} else {
114134
file.reset(fopen(log_file, "a"));
115-
std::cout << "Opened log file " << log_file << std::endl;
116135
rd_kafka_conf_set_opaque(rk_conf, file.get());
117136
rd_kafka_conf_set_log_cb(
118137
rk_conf, +[](const rd_kafka_t *rk, int level, const char *fac,

0 commit comments

Comments
 (0)