feat(landerctl): add option to use custom config file
ci/woodpecker/push/build Pipeline was successful Details
ci/woodpecker/push/docker Pipeline was successful Details

new-lnm-integration
Jef Roosens 2023-12-09 10:37:10 +01:00
parent a0954e8d07
commit cda61f5433
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 45 additions and 36 deletions

View File

@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Better API for adding routes
* State machine HTTP loop
* Auomatically support HEAD requests for all GET requests
* Landerctl
* `-c` flag to use custom config file (useful for testing)
## [0.2.0](https://git.rustybever.be/Chewing_Bever/lander/src/tag/0.2.0)

View File

@ -0,0 +1,3 @@
api_key = test
server_url = http://localhost:18080
ca_certs_bundle = /etc/ssl/certs/ca-certificates.crt

View File

@ -10,25 +10,56 @@
#include "landerctl.h"
const char *cfg_file_name = ".landerrc";
const char *usage = "%s [-SPFsv] arg [key]\n";
const char *usage = "%s [-SPFsv] [-c CONFIG_FILE] arg [key]\n";
int main(int argc, char **argv) {
landerctl_ctx ctx = {0};
char *err_msg = NULL;
landerctl_cfg_err parse_res;
const char *home_dir = getenv("HOME");
const char *cfg_path;
if (home_dir == NULL) {
parse_res = landerctl_cfg_parse(&ctx.cfg, cfg_file_name);
cfg_path = cfg_file_name;
} else {
char cfg_path[strlen(home_dir) + strlen(cfg_file_name) + 2];
sprintf(cfg_path, "%s/%s", home_dir, cfg_file_name);
parse_res = landerctl_cfg_parse(&ctx.cfg, cfg_path);
// This is a blatant memleak if a custom config file is set, but it really
// doesn't matter for a short-lived CLI tool
char *buf = malloc(strlen(home_dir) + strlen(cfg_file_name) + 2);
sprintf(buf, "%s/%s", home_dir, cfg_file_name);
cfg_path = buf;
}
opterr = 0;
int c;
while ((c = getopt(argc, argv, "SPFsvc:")) != -1) {
switch (c) {
case 'S':
ctx.mode = landerctl_mode_short;
break;
case 'P':
ctx.mode = landerctl_mode_paste;
break;
case 'F':
ctx.mode = landerctl_mode_file;
break;
case 's':
ctx.secure = true;
break;
case 'v':
ctx.verbose = true;
break;
case 'c':
cfg_path = optarg;
break;
case '?':
printf(usage, argv[0]);
exit(2);
}
}
char *err_msg = NULL;
landerctl_cfg_err parse_res = landerctl_cfg_parse(&ctx.cfg, cfg_path);
switch (parse_res) {
case landerctl_cfg_err_ok:
break;
@ -48,33 +79,6 @@ int main(int argc, char **argv) {
exit(1);
}
opterr = 0;
int c;
while ((c = getopt(argc, argv, "SPFsv")) != -1) {
switch (c) {
case 'S':
ctx.mode = landerctl_mode_short;
break;
case 'P':
ctx.mode = landerctl_mode_paste;
break;
case 'F':
ctx.mode = landerctl_mode_file;
break;
case 's':
ctx.secure = true;
break;
case 'v':
ctx.verbose = true;
break;
case '?':
printf(usage, argv[0]);
exit(2);
}
}
if (ctx.mode == landerctl_mode_none) {
printf("No mode specified.\n\n");
printf(usage, argv[0]);