new: implement the search command

This commit is contained in:
ngn 2024-08-22 02:16:11 +03:00
parent a7370423a6
commit 86b1481774
6 changed files with 296 additions and 56 deletions

View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-08-22 01:00+0300\n"
"POT-Creation-Date: 2024-08-22 02:15+0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -90,93 +90,98 @@ msgid "list all the installed packages"
msgstr "list all the installed packages"
#: src/main.c:66
msgid "search for packages by name/desc/version"
msgstr ""
#: src/main.c:67
msgid "update the pool info and package lists"
msgstr "update the pool info and package lists"
#: src/main.c:67
#: src/main.c:68
msgid "install package(s) from remote pools"
msgstr "install package(s) from remote pools"
#: src/main.c:68
#: src/main.c:69
msgid "remove installed package(s)"
msgstr "remove installed package(s)"
#: src/main.c:69
#: src/main.c:70
msgid "update installed package(s)"
msgstr "update installed package(s)"
#: src/main.c:118
#: src/main.c:119
msgid "Failed to access the root directory, are you running as root?"
msgstr "Failed to access the root directory, are you running as root?"
#: src/main.c:122
#: src/main.c:123
msgid "Failed to access the data directory ("
msgstr "Failed to access the data directory ("
#: src/main.c:126
#: src/main.c:127
msgid "Failed to access the temp directory ("
msgstr "Failed to access the temp directory ("
#: src/main.c:130
#: src/main.c:131
#, c-format
msgid "Failed to initialize libmp context: %s"
msgstr "Failed to initialize libmp context: %s"
#: src/main.c:141
#: src/main.c:142
msgid "Failed to create the pools directory ("
msgstr "Failed to create the pools directory ("
#: src/main.c:151
#: src/main.c:152
msgid "Failed to add pool "
msgstr "Failed to add pool "
#: src/main.c:165
#: src/main.c:166
msgid "Failed to lock, matt is already running"
msgstr "Failed to lock, matt is already running"
#: src/main.c:169
#: src/main.c:170
msgid "Failed to lock, are you root?"
msgstr "Failed to lock, are you root?"
#: src/main.c:182
#: src/main.c:183
msgid "Command not found: "
msgstr "Command not found: "
#: src/main.c:183
#: src/main.c:184
msgid "Displaying help information"
msgstr "Displaying help information"
#: src/main.c:186
#: src/main.c:187
#, c-format
msgid "MatterLinux package manager %s (libmp %s)"
msgstr "MatterLinux package manager %s (libmp %s)"
#: src/main.c:187
#: src/main.c:188
msgid "Usage: "
msgstr "Usage: "
#: src/main.c:189
#: src/main.c:190
msgid "Here is a list of available commands:"
msgstr "Here is a list of available commands:"
#: src/main.c:195
#: src/main.c:196
msgid "To list different options, use commands with "
msgstr "To list different options, use commands with "
#: src/main.c:196
#: src/main.c:197
msgid "Here is a list of available global options:"
msgstr "Here is a list of available global options:"
#: src/main.c:199 src/main.c:200 src/cmd/info.c:52 src/cmd/info.c:53
#: src/cmd/info.c:54 src/cmd/install.c:34 src/cmd/install.c:35
#: src/main.c:200 src/main.c:201 src/cmd/info.c:48 src/cmd/info.c:49
#: src/cmd/info.c:50 src/cmd/install.c:34 src/cmd/install.c:35
#: src/cmd/install.c:36 src/cmd/install.c:37 src/cmd/install.c:38
#: src/cmd/list.c:24 src/cmd/list.c:25 src/cmd/remove.c:28 src/cmd/remove.c:29
#: src/cmd/update.c:23
#: src/cmd/search.c:170 src/cmd/search.c:171 src/cmd/search.c:172
#: src/cmd/search.c:173 src/cmd/update.c:23
#, c-format
msgid " "
msgstr " "
#: src/main.c:202
#: src/main.c:203
msgid ""
"Licensed under GPLv3, see https://www.gnu.org/licenses/ for more information"
msgstr ""
@ -196,80 +201,80 @@ msgstr "Failed to run the editor: %s"
msgid "Failed to open the editor: %s"
msgstr "Failed to open the editor: %s"
#: src/cmd/info.c:17 src/cmd/info.c:27 src/cmd/install.c:91 src/cmd/remove.c:52
#: src/cmd/info.c:13 src/cmd/info.c:23 src/cmd/install.c:91 src/cmd/remove.c:52
msgid "Package "
msgstr "Package "
#: src/cmd/info.c:46 src/cmd/install.c:28 src/cmd/list.c:18 src/cmd/remove.c:22
#: src/cmd/sync.c:15 src/cmd/update.c:17
#: src/cmd/info.c:42 src/cmd/install.c:28 src/cmd/list.c:18 src/cmd/remove.c:22
#: src/cmd/search.c:164 src/cmd/sync.c:15 src/cmd/update.c:17
msgid "Unknown argument: "
msgstr "Unknown argument: "
#: src/cmd/info.c:51 src/cmd/list.c:23
#: src/cmd/info.c:47 src/cmd/list.c:23 src/cmd/search.c:169
msgid "Listing options for the list command:"
msgstr "Listing options for the list command:"
#: src/cmd/info.c:75
#: src/cmd/info.c:71
msgid "Please specify only a single package name"
msgstr "Please specify only a single package name"
#: src/cmd/info.c:82
#: src/cmd/info.c:78
msgid "Please specify a single package"
msgstr "Please specify a single package"
#: src/cmd/info.c:97
#: src/cmd/info.c:93
msgid "Failed to get the changes file for "
msgstr "Failed to get the changes file for "
#: src/cmd/info.c:103
#: src/cmd/info.c:99
msgid "Failed to access the changes file, is the package installed?"
msgstr "Failed to access the changes file, is the package installed?"
#: src/cmd/info.c:110
#: src/cmd/info.c:106
msgid "Failed to get the depends list for "
msgstr "Failed to get the depends list for "
#: src/cmd/info.c:137
#: src/cmd/info.c:134
#, c-format
msgid "Name "
msgstr "Name "
#: src/cmd/info.c:138
#: src/cmd/info.c:135
#, c-format
msgid "Version "
msgstr "Version "
#: src/cmd/info.c:139
#: src/cmd/info.c:136
#, c-format
msgid "Desc "
msgstr "Desc "
#: src/cmd/info.c:140
#: src/cmd/info.c:137
#, c-format
msgid "Size "
msgstr "Size "
#: src/cmd/info.c:143
#: src/cmd/info.c:140
#, c-format
msgid "Depends "
msgstr "Depends "
#: src/cmd/info.c:146
#: src/cmd/info.c:143
#, c-format
msgid "Changes "
msgstr "Changes "
#: src/cmd/info.c:149
#: src/cmd/info.c:146
#, fuzzy, c-format
msgid "Pool "
msgstr " "
#: src/cmd/info.c:152 src/cmd/info.c:153
#: src/cmd/info.c:149 src/cmd/info.c:150
#, c-format
msgid "Installed "
msgstr "Installed "
#: src/cmd/info.c:156 src/cmd/info.c:157
#: src/cmd/info.c:153 src/cmd/info.c:154
#, c-format
msgid "Up-to-date "
msgstr "Up-to-date "
@ -351,7 +356,8 @@ msgid "Installed all of the "
msgstr "Installed all of the "
#: src/cmd/list.c:38
msgid "There no installed packages"
#, fuzzy
msgid "There are no installed packages"
msgstr "There no installed packages"
#: src/cmd/list.c:43
@ -379,6 +385,29 @@ msgstr "Removed "
msgid "Removed all of the "
msgstr "Removed all of the "
#: src/cmd/search.c:49 src/cmd/search.c:120
msgid "No results"
msgstr ""
#: src/cmd/search.c:54 src/cmd/search.c:125
msgid "Found "
msgstr ""
#: src/cmd/search.c:90
#, fuzzy, c-format
msgid "Failed to get package list: %s"
msgstr "Failed to get the package update list: %s"
#: src/cmd/search.c:189
#, fuzzy
msgid "Please specify only a single value for searching"
msgstr "Please specify only a single package name"
#: src/cmd/search.c:196
#, fuzzy
msgid "Please specify a value for searching"
msgstr "Please specify a single package"
#: src/cmd/sync.c:20
msgid "There are no pools specified in the configuration"
msgstr "There are no pools specified in the configuration"

View File

@ -17,6 +17,7 @@ typedef struct cmd {
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_search(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);

View File

@ -1,10 +1,6 @@
#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 <stdio.h>
#include "../util.h"
#include "../intl.h"

View File

@ -35,7 +35,7 @@ bool cmd_list(lm_ctx_t *ctx, config_t *config, args_t *args){
if(count <= 0){
if(!args_get_bool(args, "grep"))
info(_("There no installed packages"));
info(_("There are no installed packages"));
return true;
}
@ -52,9 +52,11 @@ bool cmd_list(lm_ctx_t *ctx, config_t *config, args_t *args){
}
if(args_get_bool(args, "desc"))
printf(FG_BOLD"%s"FG_RESET"_"FG_BOLD FG_GREEN"%s"FG_RESET": %s\n", ent.name, ent.version, ent.desc);
printf(FG_BOLD"%s"FG_RESET"_"FG_BOLD FG_GREEN"%s"FG_RESET": %s\n",
ent.name, ent.version, ent.desc);
else
printf(FG_BOLD"%s"FG_RESET"_"FG_BOLD FG_GREEN"%s"FG_RESET"\n", ent.name, ent.version);
printf(FG_BOLD"%s"FG_RESET"_"FG_BOLD FG_GREEN"%s"FG_RESET"\n",
ent.name, ent.version);
}
lm_ctx_database_next_free(ctx, &ent);

211
src/cmd/search.c Normal file
View File

@ -0,0 +1,211 @@
#include <libmp/all.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../util.h"
#include "../intl.h"
#include "../cmd.h"
#include "../log.h"
enum field {
FIELD_NAME = 0,
FIELD_DESC = 1,
FIELD_VERSION = 2,
};
bool cmd_search_database(lm_ctx_t *ctx, args_t *args, char *search, enum field field) {
lm_entry_t *list = malloc(sizeof(lm_entry_t));
char *selected = NULL;
size_t indx = 0, i = 0;
bool ret = false;
while(lm_ctx_database_next(ctx, &list[indx])){
selected = NULL;
switch (field) {
case FIELD_NAME:
selected = list[indx].name;
break;
case FIELD_DESC:
selected = list[indx].desc;
break;
case FIELD_VERSION:
selected = list[indx].version;
break;
}
if(NULL == selected || strstr(selected, search) == NULL)
continue;
list = realloc(list, sizeof(lm_entry_t)*((++indx)+1));
lm_entry_init(&list[indx]);
}
if(indx <= 0){
if(!args_get_bool(args, "grep"))
info(_("No results"));
goto end;
}
if(!args_get_bool(args, "grep"))
info(indx > 1 ? _("Found "FG_BOLD"%d"FG_RESET" results") : _("Found "FG_BOLD"%d"FG_RESET" result"), indx);
for(i = 0; i < indx; i++){
if(args_get_bool(args, "grep")){
if(args_get_bool(args, "desc"))
printf("%s:%s:%s\n", list[i].name, list[i].version, list[i].desc);
else
printf("%s:%s\n", list[i].name, list[i].version);
continue;
}
if(args_get_bool(args, "desc"))
printf(FG_BOLD"%s"FG_RESET"_"FG_BOLD FG_GREEN"%s"FG_RESET": %s\n", list[i].name, list[i].version, list[i].desc);
else
printf(FG_BOLD"%s"FG_RESET"_"FG_BOLD FG_GREEN"%s"FG_RESET"\n", list[i].name, list[i].version);
}
ret = true;
end:
for(i = 0; i < (indx+1); i++)
lm_ctx_database_next_free(ctx, &list[indx]);
free(list);
return ret;
}
bool cmd_search_pools(lm_ctx_t *ctx, args_t *args, char *search, enum field field) {
lm_pkg_t **list = malloc(sizeof(lm_pkg_t*));
char *selected = NULL;
size_t indx = 0, i = 0;
lm_ctx_list_t plist;
bool ret = false;
list[indx] = NULL;
if(!lm_ctx_list(ctx, &plist)){
if(!args_get_bool(args, "grep"))
info(_("Failed to get package list: %s"), lm_strerror());
goto end;
}
while((list[indx] = lm_ctx_list_next(&plist)) != NULL){
selected = NULL;
switch (field) {
case FIELD_NAME:
selected = list[indx]->data.name;
break;
case FIELD_DESC:
selected = list[indx]->data.desc;
break;
case FIELD_VERSION:
selected = list[indx]->data.version;
break;
}
if(NULL == selected || strstr(selected, search) == NULL)
continue;
list = realloc(list, sizeof(lm_pkg_t*)*((++indx)+1));
list[indx] = NULL;
}
if(indx <= 0){
if(!args_get_bool(args, "grep"))
info(_("No results"));
goto end;
}
if(!args_get_bool(args, "grep"))
info(indx > 1 ? _("Found "FG_BOLD"%d"FG_RESET" results") : _("Found "FG_BOLD"%d"FG_RESET" result"), indx);
for(i = 0; i < indx; i++){
if(args_get_bool(args, "grep")){
if(args_get_bool(args, "desc"))
printf("%s:%s:%s\n", list[i]->data.name, list[i]->data.version, list[i]->data.desc);
else
printf("%s:%s\n", list[i]->data.name, list[i]->data.version);
continue;
}
if(args_get_bool(args, "desc"))
printf(FG_BOLD"%s"FG_RESET"_"FG_BOLD FG_GREEN"%s"FG_RESET": %s\n",
list[i]->data.name, list[i]->data.version, list[i]->data.desc);
else
printf(FG_BOLD"%s"FG_RESET"_"FG_BOLD FG_GREEN"%s"FG_RESET"\n",
list[i]->data.name, list[i]->data.version);
}
end:
lm_ctx_list_free(&plist);
free(list);
return ret;
}
bool cmd_search(lm_ctx_t *ctx, config_t *config, args_t *args){
char *arg = NULL;
args_split(args, "grep");
args_split(args, "desc");
args_split(args, "field");
args_split(args, "database");
args_register(args, "grep");
args_register(args, "desc");
args_register(args, "field");
args_register(args, "database");
if((arg = args_register_check(args)) != NULL){
error(_("Unknown argument: "FG_BOLD"--%s"FG_RESET", run with "FG_BOLD"--help"FG_RESET" to list the avaliable arguments"), arg);
return false;
}
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"--desc"FG_RESET":\tshow package description of the results\n"));
printf(_(" "FG_BOLD"--field"FG_RESET":\tpackage field to search by (\"name\", \"desc\" or \"version\")\n"));
printf(_(" "FG_BOLD"--database"FG_RESET":\tsearch the database instead of the pools\n"));
return true;
}
enum field field = FIELD_NAME;
char *search = NULL, *field_val = NULL;
for(size_t i = 1; i < args->count; i++){
if(NULL != args->list[i].name)
continue;
if(NULL == search && NULL != (search = args->list[i].value))
continue;
else if(NULL != search && NULL != (search = args->list[i].value)){
if(!args_get_bool(args, "grep"))
error(_("Please specify only a single value for searching"));
return false;
}
}
if(NULL == search){
if(!args_get_bool(args, "grep"))
error(_("Please specify a value for searching"));
return false;
}
if(NULL == (field_val = args_get_string(args, "field")))
field = FIELD_NAME;
else if(eq(field_val, "desc"))
field = FIELD_DESC;
else if(eq(field_val, "version"))
field = FIELD_VERSION;
if(args_get_bool(args, "database"))
return cmd_search_database(ctx, args, search, field);
return cmd_search_pools(ctx, args, search, field);
}

View File

@ -61,12 +61,13 @@ int main(int argc, char *argv[], char *envp[]) {
textdomain("matt");
cmd_t commands[] = {
{.name = "info", .desc = _("show information about a single package"), .root = false, .func = cmd_info },
{.name = "list", .desc = _("list all the installed packages"), .root = false, .func = cmd_list },
{.name = "sync", .desc = _("update the pool info and package lists"), .root = true, .func = cmd_sync },
{.name = "install", .desc = _("install package(s) from remote pools"), .root = true, .func = cmd_install},
{.name = "remove", .desc = _("remove installed package(s)"), .root = true, .func = cmd_remove },
{.name = "update", .desc = _("update installed package(s)"), .root = true, .func = cmd_update },
{.name = "info", .desc = _("show information about a single package"), .root = false, .func = cmd_info },
{.name = "list", .desc = _("list all the installed packages"), .root = false, .func = cmd_list },
{.name = "search", .desc = _("search for packages by name/desc/version"), .root = false, .func = cmd_search },
{.name = "sync", .desc = _("update the pool info and package lists"), .root = true, .func = cmd_sync },
{.name = "install", .desc = _("install package(s) from remote pools"), .root = true, .func = cmd_install},
{.name = "remove", .desc = _("remove installed package(s)"), .root = true, .func = cmd_remove },
{.name = "update", .desc = _("update installed package(s)"), .root = true, .func = cmd_update },
};
char *full_datadir = NULL, *full_tmpdir = NULL, *full_pooldir = NULL;