lander/landerctl/src/main.c

215 lines
5.1 KiB
C

#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <curl/curl.h>
#include <magic.h>
#include "landerctl.h"
const char *cfg_file_name = ".landerrc";
const char *usage = "%s [-SPFsv] 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");
if (home_dir == NULL) {
parse_res = landerctl_cfg_parse(&ctx.cfg, 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);
}
switch (parse_res) {
case landerctl_cfg_err_ok:
break;
case landerctl_cfg_err_not_found:
err_msg = "Config file not found";
break;
case landerctl_cfg_err_invalid:
err_msg = "Invalid config file";
break;
case landerctl_cfg_err_incomplete:
err_msg = "Incomplete config file";
break;
}
if (err_msg != NULL) {
fprintf(stderr, "%s\n", err_msg);
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]);
exit(2);
}
if (optind == argc || (argc - optind > 2)) {
printf(usage, argv[0]);
exit(2);
}
ctx.arg = argv[optind];
ctx.key = argc - optind == 2 ? argv[optind + 1] : NULL;
curl_global_init(CURL_GLOBAL_ALL);
ctx.curl = curl_easy_init();
if (ctx.curl == NULL) {
exit(255);
}
landerctl_set_common(&ctx);
landerctl_err res;
switch (ctx.mode) {
case landerctl_mode_short:
res = landerctl_post_short(&ctx);
break;
case landerctl_mode_paste:
res = landerctl_post_paste(&ctx);
break;
case landerctl_mode_file:
res = landerctl_post_file(&ctx);
break;
default:
return 7;
}
if (res != landerctl_err_ok) {
printf("%s\n", landerctl_err_msg(res));
exit(6);
}
if (ctx.verbose) {
curl_easy_setopt(ctx.curl, CURLOPT_VERBOSE, 1L);
}
curl_easy_setopt(ctx.curl, CURLOPT_HTTPHEADER, ctx.headers);
curl_easy_setopt(ctx.curl, CURLOPT_CAINFO, ctx.cfg.ca_certs_bundle);
char curl_err_msg[CURL_ERROR_SIZE];
curl_easy_setopt(ctx.curl, CURLOPT_ERRORBUFFER, curl_err_msg);
int exit_code = 0;
if (curl_easy_perform(ctx.curl) == CURLE_OK) {
long response_code;
curl_easy_getinfo(ctx.curl, CURLINFO_RESPONSE_CODE, &response_code);
if (response_code < 200 || response_code > 299) {
fprintf(stderr, "HTTP status code %li\n", response_code);
exit_code = 3;
} else {
struct curl_header *location_header;
if (curl_easy_header(ctx.curl, "Location", 0, CURLH_HEADER, -1,
&location_header) == CURLHE_OK) {
printf("%s%s\n", ctx.cfg.server_url, location_header->value);
} else {
fprintf(stderr, "Server returned a 2xx without a Location header.\n");
exit_code = 5;
}
}
} else {
fprintf(stderr, "Libcurl encountered an error: %s\n", curl_err_msg);
exit_code = 4;
}
curl_easy_cleanup(ctx.curl);
curl_slist_free_all(ctx.headers);
if (ctx.data_file != NULL) {
fclose(ctx.data_file);
}
return exit_code;
/* struct stat sb; */
/* stat(argv[1], &sb); */
/* printf("file size: %lu\n", sb.st_size); */
/* FILE *f = fopen(argv[1], "rb"); */
/* if (f == NULL) { */
/* printf("Couldn't open file.\n"); */
/* exit(1); */
/* } */
/* CURL *curl = curl_easy_init(); */
/* if (curl == NULL) { */
/* exit(1); */
/* } */
/* curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:18080/f/"); */
/* curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); */
/* curl_easy_setopt(curl, CURLOPT_READDATA, f); */
/* curl_off_t file_size = sb.st_size; */
/* /1* curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, file_size); *1/ */
/* curl_easy_setopt(curl, CURLOPT_POST, 1L); */
/* curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, file_size); */
/* magic_t cookie = magic_open(MAGIC_MIME_TYPE); */
/* magic_load(cookie, NULL); */
/* const char *mime_type = magic_file(cookie, argv[1]); */
/* char content_type_header[strlen(mime_type) + 24]; */
/* sprintf(content_type_header, "X-Lander-Content-Type: %s", mime_type); */
/* char content_length_header[32]; */
/* sprintf(content_length_header, "Content-Length: %lu", sb.st_size); */
/* struct curl_slist *list = NULL; */
/* list = curl_slist_append(list, content_type_header); */
/* list = curl_slist_append(list, content_length_header); */
/* list = curl_slist_append(list, "X-Api-Key: test"); */
/* curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); */
/* curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); */
/* curl_easy_perform(curl); */
/* curl_slist_free_all(list); */
}