]> git.ipfire.org Git - people/ms/pakfire.git/blame - src/cli/lib/install.c
cli: install: Migrate the command to the new system
[people/ms/pakfire.git] / src / cli / lib / install.c
CommitLineData
c9abb903
MT
1/*#############################################################################
2# #
3# Pakfire - The IPFire package management system #
4# Copyright (C) 2023 Pakfire development team #
5# #
6# This program is free software: you can redistribute it and/or modify #
7# it under the terms of the GNU General Public License as published by #
8# the Free Software Foundation, either version 3 of the License, or #
9# (at your option) any later version. #
10# #
11# This program is distributed in the hope that it will be useful, #
12# but WITHOUT ANY WARRANTY; without even the implied warranty of #
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14# GNU General Public License for more details. #
15# #
16# You should have received a copy of the GNU General Public License #
17# along with this program. If not, see <http://www.gnu.org/licenses/>. #
18# #
19#############################################################################*/
20
8b881a9b 21#include <argp.h>
c9abb903 22#include <errno.h>
c9abb903
MT
23#include <stdlib.h>
24
25#include <pakfire/pakfire.h>
26#include <pakfire/transaction.h>
27
8b881a9b 28#include "command.h"
c9abb903 29#include "install.h"
8b881a9b 30#include "pakfire.h"
976ce512 31#include "transaction.h"
c9abb903 32
8b881a9b
MT
33static const char* args_doc = "install [OPTIONS...] PACKAGES...";
34
35static const char* doc = "Install packages";
36
37#define MAX_PACKAGES 128
38
976ce512 39struct config {
c9abb903
MT
40 int transaction_flags;
41 int job_flags;
8b881a9b
MT
42
43 // Packages
44 char* packages[MAX_PACKAGES];
45 unsigned int num_packages;
976ce512 46};
c9abb903 47
8b881a9b
MT
48enum {
49 OPT_BEST = 1,
50 OPT_ALLOW_DOWNGRADE = 2,
51 OPT_ALLOW_UNINSTALL = 3,
52 OPT_WITHOUT_RECOMMENDED = 4,
53};
c9abb903 54
8b881a9b
MT
55static struct argp_option options[] = {
56 { "allow-downgrade", OPT_ALLOW_DOWNGRADE, NULL, 0, "Allow downgrading packages", 0 },
57 { "allow-uninstall", OPT_ALLOW_UNINSTALL, NULL, 0, "Allow uninstalling packages", 0 },
58 { "best", OPT_BEST, NULL, 0, "Only install the best package(s)", 0 },
59 { "without-recommended", OPT_ALLOW_DOWNGRADE, NULL, 0, "Do not install recommended packages", 0 },
60 { NULL },
61};
c9abb903 62
8b881a9b
MT
63static error_t parse(int key, char* arg, struct argp_state* state, void* data) {
64 struct config* config = data;
65
66 switch (key) {
67 case OPT_BEST:
68 config->job_flags |= PAKFIRE_JOB_BEST;
69 break;
c9abb903 70
8b881a9b
MT
71 case OPT_ALLOW_DOWNGRADE:
72 config->transaction_flags |= PAKFIRE_TRANSACTION_ALLOW_DOWNGRADE;
73 break;
c9abb903 74
8b881a9b
MT
75 case OPT_ALLOW_UNINSTALL:
76 config->transaction_flags |= PAKFIRE_TRANSACTION_ALLOW_UNINSTALL;
77 break;
c9abb903 78
8b881a9b
MT
79 case OPT_WITHOUT_RECOMMENDED:
80 config->transaction_flags |= PAKFIRE_TRANSACTION_WITHOUT_RECOMMENDED;
81 break;
c9abb903 82
8b881a9b
MT
83 case ARGP_KEY_ARG:
84 if (config->num_packages >= MAX_PACKAGES)
85 return -ENOBUFS;
c9abb903 86
8b881a9b
MT
87 config->packages[config->num_packages++] = arg;
88 break;
c9abb903 89
8b881a9b
MT
90 default:
91 return ARGP_ERR_UNKNOWN;
c9abb903
MT
92 }
93
94 return 0;
95}
96
976ce512
MT
97static int __cli_install(struct pakfire_transaction* transaction, int argc, char* argv[], void* data) {
98 struct config* config = (struct config*)data;
c9abb903
MT
99 int r;
100
c9abb903 101 // Add the remaining command line options as packages
8b881a9b 102 for (unsigned int i = 0; i < config->num_packages; i++) {
c9abb903 103 r = pakfire_transaction_request(transaction,
8b881a9b 104 PAKFIRE_JOB_INSTALL, config->packages[i], config->job_flags);
c9abb903
MT
105 if (r) {
106 fprintf(stderr, "Could not find '%s': %m\n", argv[i]);
976ce512 107 return r;
c9abb903
MT
108 }
109 }
110
976ce512
MT
111 return 0;
112}
c9abb903 113
8b881a9b
MT
114int cli_install(void* data, int argc, char* argv[]) {
115 struct pakfire* pakfire = NULL;
116 struct config config = {};
976ce512 117 int r;
c9abb903 118
8b881a9b
MT
119 struct cli_config* cli_config = data;
120
121 // Parse the command line
122 r = cli_parse(options, NULL, args_doc, doc, parse, argc, argv, &config);
123 if (r)
124 goto ERROR;
125
126 // Setup Pakfire
127 r = cli_setup_pakfire(&pakfire, cli_config);
c9abb903 128 if (r)
8b881a9b
MT
129 goto ERROR;
130
131 r = cli_transaction(pakfire, argc, argv, config.transaction_flags, __cli_install, &config);
132
133ERROR:
134 if (pakfire)
135 pakfire_unref(pakfire);
c9abb903 136
8b881a9b 137 return r;
c9abb903 138}