matt/src/cmd/info.c

156 lines
4.8 KiB
C

#include <libmp/all.h>
#include <libmp/ctx.h>
#include <libmp/database.h>
#include <libmp/error.h>
#include <libmp/package.h>
#include <stdio.h>
#include <stdlib.h>
#include "../util.h"
#include "../intl.h"
#include "../cmd.h"
#include "../log.h"
lm_pkg_data_t *cmd_info_from_pool(lm_ctx_t *ctx, args_t *args, char *name){
lm_pkg_t *pkg = NULL;
if((pkg = 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 NULL;
}
return &pkg->data;
}
lm_pkg_data_t *cmd_info_from_database(lm_ctx_t *ctx, lm_entry_t *entry, args_t *args, char *name){
if(!lm_ctx_database_find(ctx, entry, name, NULL)){
if(!args_get_bool(args, "grep") && LM_ERR_DbSqlNotFound == lm_error())
error(_("Package "FG_BOLD"%s"FG_RESET" not found"), name);
return NULL;
}
return entry;
}
bool cmd_info(lm_ctx_t *ctx, config_t *config, args_t *args){
args_split(args, "grep");
args_split(args, "changes");
args_split(args, "database");
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, *depends = NULL, *changes = NULL;
lm_pkg_data_t *data = NULL;
lm_entry_t ent;
for(size_t i = 1; i < args->count; i++){
if(NULL != args->list[i].name)
continue;
if(NULL == name && NULL != (name = args->list[i].value))
continue;
else if(NULL != name && NULL != (name = args->list[i].value)){
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")){
if((data = cmd_info_from_database(ctx, &ent, args, name)) == NULL)
return false;
}
else if(!args_get_bool(args, "database")){
if((data = cmd_info_from_pool(ctx, args, name)) == NULL)
return false;
}
if((changes = lm_ctx_database_changes(ctx, data)) == 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"), data->name, data->version, lm_strerror());
goto end;
}
if(args_get_bool(args, "changes")){
if(NULL == changes)
error(_("Failed to access the changes file, is the package installed?"));
else
editor_open(changes);
goto end;
}
if((depends = lm_package_data_depend_tostr(data, NULL)) == NULL){
error(_("Failed to get the depends list for "FG_BOLD"%s"FG_RESET"_"FG_BOLD FG_GREEN"%s"FG_RESET": %s"), data->name, data->version, lm_strerror());
goto end;
}
if(args_get_bool(args, "grep")){
printf("NAME:%s\n", data->name);
printf("VERSION:%s\n", data->version);
printf("DESC:%s\n", data->desc);
printf("SIZE:%lu\n", data->size);
if(lm_package_data_depend_count(data) > 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, data->name, NULL));
printf("UPTODATE:%d\n", lm_ctx_database_is_installed(ctx, data->name, data->version));
}
goto end;
}
char ssize[LONGSTR_MAX+1];
size_to_human(ssize, data->size);
printf(_(FG_BOLD"Name "FG_BLUE"=>"FG_RESET" %s\n"), data->name);
printf(_(FG_BOLD"Version "FG_BLUE"=>"FG_RESET" %s\n"), data->version);
printf(_(FG_BOLD"Desc "FG_BLUE"=>"FG_RESET" %s\n"), data->desc);
printf(_(FG_BOLD"Size "FG_BLUE"=>"FG_RESET" %s\n"), ssize);
if(lm_package_data_depend_count(data) > 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, data->name, NULL) ?
_(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, data->name, data->version) ?
_(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_entry_free(&ent);
free(changes);
free(depends);
return true;
}