new: basic info command

This commit is contained in:
ngn
2024-07-19 00:28:55 +03:00
parent 43f93ae797
commit cc6125ac4c
6 changed files with 225 additions and 53 deletions

View File

@@ -13,8 +13,9 @@ typedef struct cmd {
cmd_func_t func;
} cmd_t;
bool cmd_info(lm_ctx_t *ctx, config_t *config, args_t *args);
bool cmd_sync(lm_ctx_t *ctx, config_t *config, args_t *args);
bool cmd_list(lm_ctx_t *ctx, config_t *config, args_t *args);
bool cmd_install(lm_ctx_t *ctx, config_t *config, args_t *args);
bool cmd_remove(lm_ctx_t *ctx, config_t *config, args_t *args);
bool cmd_update(lm_ctx_t *ctx, config_t *config, args_t *args);
bool cmd_list(lm_ctx_t *ctx, config_t *config, args_t *args);

115
src/cmd/info.c Normal file
View File

@@ -0,0 +1,115 @@
#include <libmp/all.h>
#include <libmp/ctx.h>
#include <libmp/error.h>
#include <libmp/package.h>
#include <stdio.h>
#include "../util.h"
#include "../cmd.h"
#include "../log.h"
bool cmd_info(lm_ctx_t *ctx, config_t *config, args_t *args){
if(args_get_bool(args, "help")){
info(_("Listing options for the list command:"));
printf(_(" "FG_BOLD"--grep"FG_RESET":\tmakes the output \"grepable\"\n"));
printf(_(" "FG_BOLD"--changes"FG_RESET":\topen the changes file for the package\n"));
printf(_(" "FG_BOLD"--database"FG_RESET":\tsearch the database instead of the pools\n"));
return true;
}
if(!args_get_bool(args, "database"))
lm_ctx_sync(ctx, false, NULL, NULL);
char *name = NULL, *changes = NULL;
lm_pkg_t pkg, *pkgp = &pkg;
for(size_t i = 0; i < args->count; i++){
if(NULL != args->list[i].name || eq(args->list[i].value, "info"))
continue;
if(NULL == name){
name = args->list[i].value;
continue;
}
if(!args_get_bool(args, "grep"))
error(_("Please specify only a single package name"));
return false;
}
if(NULL == name){
if(!args_get_bool(args, "grep"))
error(_("Please specify a single package"));
return false;
}
if(args_get_bool(args, "database") && !lm_ctx_database_find(ctx, &pkg, name, NULL)){
if(!args_get_bool(args, "grep") && LM_ERR_DbSqlNotFound == lm_error())
error(_("Package "FG_BOLD"%s"FG_RESET" not found"), name);
return false;
}
else if(!args_get_bool(args, "database") && (pkgp = lm_ctx_pool_find(ctx, name, NULL)) == NULL){
if(!args_get_bool(args, "grep") && LM_ERR_PkgNotFound == lm_error())
error(_("Package "FG_BOLD"%s"FG_RESET" not found"), name);
return false;
}
if((changes = lm_ctx_database_changes(ctx, pkgp)) == NULL && LM_ERR_DbChangesNotExists != lm_error()){
error(_("Failed to get the changes file for "FG_BOLD"%s"FG_RESET"_"FG_BOLD FG_GREEN"%s"FG_RESET": %s"), pkgp->name, pkgp->version, lm_strerror());
return false;
}
// TODO: open the changes file with the preferred editor if --changes is set
char depends[lm_package_depend_strlen(pkgp)+1];
lm_package_depend_tostr(pkgp, depends);
if(args_get_bool(args, "grep")){
printf("NAME:%s\n", pkgp->name);
printf("VERSION:%s\n", pkgp->version);
printf("DESC:%s\n", pkgp->desc);
printf("SIZE:%lu\n", pkgp->size);
if(lm_package_depend_count(pkgp) > 0)
printf("DEPENDS:%s\n", depends);
if(NULL != changes)
printf("CHANGES:%s\n", changes);
if(!args_get_bool(args, "database")){
printf("INSTALLED:%d\n", lm_ctx_database_is_installed(ctx, pkgp, false));
printf("UPTODATE:%d\n", lm_ctx_database_is_installed(ctx, pkgp, true));
}
goto end;
}
char ssize[LONGSTR_MAX+1];
size_to_human(ssize, pkgp->size);
printf(_(FG_BOLD"Name "FG_BLUE"=>"FG_RESET" %s\n"), pkgp->name);
printf(_(FG_BOLD"Version "FG_BLUE"=>"FG_RESET" %s\n"), pkgp->version);
printf(_(FG_BOLD"Desc "FG_BLUE"=>"FG_RESET" %s\n"), pkgp->desc);
printf(_(FG_BOLD"Size "FG_BLUE"=>"FG_RESET" %s\n"), ssize);
if(lm_package_depend_count(pkgp) > 0)
printf(_(FG_BOLD"Depends "FG_BLUE"=>"FG_RESET" %s\n"), depends);
if(NULL != changes)
printf(_(FG_BOLD"Changes "FG_BLUE"=>"FG_RESET" %s\n"), changes);
if(!args_get_bool(args, "database")){
printf(lm_ctx_database_is_installed(ctx, pkgp, false) ?
_(FG_BOLD"Installed "FG_BLUE"=>"FG_RESET" "FG_GREEN"yes"FG_RESET"\n") :
_(FG_BOLD"Installed "FG_BLUE"=>"FG_RESET" "FG_RED"no"FG_RESET"\n"));
printf(lm_ctx_database_is_installed(ctx, pkgp, true) ?
_(FG_BOLD"Up-to-date "FG_BLUE"=>"FG_RESET" "FG_GREEN"yes"FG_RESET"\n") :
_(FG_BOLD"Up-to-date "FG_BLUE"=>"FG_RESET" "FG_RED"no"FG_RESET"\n"));
}
end:
if(args_get_bool(args, "database"))
lm_package_free(pkgp);
return true;
}

View File

@@ -1,5 +1,4 @@
#include <libmp/all.h>
#include <libmp/ctx.h>
#include <stdio.h>
#include "../util.h"

View File

@@ -5,9 +5,6 @@
#include "../log.h"
bool cmd_list(lm_ctx_t *ctx, config_t *config, args_t *args){
ssize_t count = 0;
lm_pkg_t pkg;
if(args_get_bool(args, "help")){
info(_("Listing options for the list command:"));
printf(_(" "FG_BOLD"--grep"FG_RESET":\tmakes the output \"grepable\"\n"));
@@ -15,12 +12,16 @@ bool cmd_list(lm_ctx_t *ctx, config_t *config, args_t *args){
return true;
}
ssize_t count = 0;
lm_pkg_t pkg;
while(lm_ctx_database_next(ctx, &pkg))
count++;
lm_ctx_database_next_free(ctx, &pkg);
if(count <= 0 && !args_get_bool(args, "grep")){
info(_("There no installed packages"));
if(count <= 0){
if(!args_get_bool(args, "grep"))
info(_("There no installed packages"));
return true;
}

View File

@@ -37,11 +37,12 @@
int main(int argc, char *argv[]) {
cmd_t commands[] = {
{.name = "list", .desc = "list all the installed packages", .func = cmd_list },
{.name = "sync", .desc = "update the pool info and package lists", .func = cmd_sync },
{.name = "install", .desc = "install package(s) from remote pools", .func = cmd_install},
{.name = "remove", .desc = "remove installed package(s)", .func = cmd_remove },
{.name = "update", .desc = "update installed package(s)", .func = cmd_update },
{.name = "info", .desc = "show information about a single package", .func = cmd_info },
{.name = "list", .desc = "list all the installed packages", .func = cmd_list },
{.name = "sync", .desc = "update the pool info and package lists", .func = cmd_sync },
{.name = "install", .desc = "install package(s) from remote pools", .func = cmd_install},
{.name = "remove", .desc = "remove installed package(s)", .func = cmd_remove },
{.name = "update", .desc = "update installed package(s)", .func = cmd_update },
};
char *config_file = NULL, *root_dir = NULL;