initial mod subcommands work

This commit is contained in:
jacekpoz 2024-02-12 00:49:52 +01:00
parent a6a1d0c65e
commit e3bdb80e91
No known key found for this signature in database
GPG key ID: 94E812A8B12AAE3C

View file

@ -4,6 +4,7 @@
#include <semver.h>
#include <util.h>
#include <dlfcn.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
@ -21,6 +22,14 @@ static const semver VERSION = {
.patch = 0,
};
void print_mod_help(FILE *stream) {
fprintf(stream, "%s mod: module information\n", PNAME);
fprintf(stream, "\n");
fprintf(stream, "SUBCOMMANDS\n");
fprintf(stream, "\tlist\t\t\t\tlists all available modules\n");
fprintf(stream, "\tinfo <module>\t\t\tprints all info about module\n");
}
int main(int argc, char *argv[]) {
char *config_path = default_config_path();
@ -37,8 +46,11 @@ int main(int argc, char *argv[]) {
fclose(config_file);
free(config_path);
bool hit_mod_cmd = false;
for (size_t i = 1; i < (size_t)argc; ++i) {
if (strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--config") == 0) {
// no more args
if (i == (size_t)argc - 1) {
fprintf(stderr, "error: no config path passed\n");
exit(EXIT_FAILURE);
@ -52,9 +64,48 @@ int main(int argc, char *argv[]) {
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
printf("%s: modular fetch [%s]\n", PNAME, svtoa(VERSION));
printf("\n");
printf("SUBCOMMANDS\n");
printf("\tmod\t\t\t\tmodule information\n");
printf("\n");
printf("OPTIONS\n");
printf("\t-h, --help\t\t\tdisplays this help text\n");
printf("\t-c, --config /path/to/config\tchanges config path from the default ($XDG_CONFIG_HOME/%s.conf)\n", PNAME);
printf("\t-c, --config </path/to/config>\tchanges config path from the default ($XDG_CONFIG_HOME/%s.conf)\n", PNAME);
exit(EXIT_SUCCESS);
}
if (strncmp(argv[i], "mod", 3) == 0 || hit_mod_cmd) {
// no more args
if (i == (size_t)argc - 1) {
print_mod_help(stderr);
exit(EXIT_FAILURE);
}
i += 1;
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
print_mod_help(stdout);
exit(EXIT_SUCCESS);
}
// some other -/-- option in between the mod and the specific mod command
if (argv[i][0] == '-') {
hit_mod_cmd = true;
continue;
}
if (strcmp(argv[i], "list") == 0) {
for (size_t ii = 0; ii < config.module_count; ++ii) {
Module current_module = config.modules[ii];
void *handle = dlopen(current_module.path, RTLD_NOW);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
semver (*mod_version)(void) = dlsym(handle, "version");
const char *(*mod_name)(void) = dlsym(handle, "name");
#pragma GCC diagnostic pop
printf("%s-%s\n", mod_name(), svtoa(mod_version()));
}
}
exit(EXIT_SUCCESS);
}
}