-
Notifications
You must be signed in to change notification settings - Fork 111
/
appstreamcli.c
1672 lines (1475 loc) · 47.1 KB
/
appstreamcli.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2012-2024 Matthias Klumpp <matthias@tenstral.net>
*
* Licensed under the GNU Lesser General Public License Version 2.1
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the license, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <glib.h>
#include <glib-object.h>
#include <glib/gi18n-lib.h>
#include <locale.h>
#include <stdio.h>
#ifdef G_OS_WIN32
#include <io.h>
#endif
#include "as-profile.h"
#include "as-utils-private.h"
#include "ascli-utils.h"
#include "ascli-actions-mdata.h"
#include "ascli-actions-validate.h"
#include "ascli-actions-pkgmgr.h"
#include "ascli-actions-misc.h"
#define ASCLI_BIN_NAME "appstreamcli"
/* global options which affect all commands */
static gboolean optn_show_version = FALSE;
static gboolean optn_verbose_mode = FALSE;
static gboolean optn_no_color = FALSE;
/*** COMMAND OPTIONS ***/
/* for data_catalog_options */
static gchar *optn_cachepath = NULL;
static gchar *optn_datapath = NULL;
static gboolean optn_no_cache = FALSE;
/**
* General options used for any operations on
* metadata catalogs and the cache.
*/
const GOptionEntry data_catalog_options[] = {
{ "cachepath",
0, 0,
G_OPTION_ARG_STRING, &optn_cachepath,
/* TRANSLATORS: ascli flag description for: --cachepath */
N_ ("Manually selected location of AppStream cache."),
NULL },
{ "datapath",
0, 0,
G_OPTION_ARG_STRING, &optn_datapath,
/* TRANSLATORS: ascli flag description for: --datapath */
N_ ("Manually selected location of AppStream metadata to scan."),
NULL },
{ "no-cache",
0, 0,
G_OPTION_ARG_NONE, &optn_no_cache,
/* TRANSLATORS: ascli flag description for: --no-cache */
N_ ("Ignore cache age and build a fresh cache before performing the query."),
NULL },
{ NULL }
};
/* used by format_options */
static gchar *optn_format = NULL;
/**
* The format option.
*/
const GOptionEntry format_options[] = {
{ "format",
0, 0,
G_OPTION_ARG_STRING, &optn_format,
/* TRANSLATORS: ascli flag description for: --format */
N_ ("Default metadata format (valid values are 'xml' and 'yaml')."),
NULL },
{ NULL }
};
/* used by find_options */
static gboolean optn_details = FALSE;
/**
* General options for finding & displaying data.
*/
const GOptionEntry find_options[] = {
{ "details",
0, 0,
G_OPTION_ARG_NONE, &optn_details,
/* TRANSLATORS: ascli flag description for: --details */
N_ ("Print detailed output about found components."),
NULL },
{ NULL }
};
/* used by validate_options */
static gboolean optn_pedantic = FALSE;
static gboolean optn_explain = FALSE;
static gboolean optn_no_net = FALSE;
static gboolean optn_validate_strict = FALSE;
static gchar *optn_issue_overrides = NULL;
/**
* General options for validation.
*/
const GOptionEntry validate_options[] = {
{ "pedantic",
(gchar) 0,
0, G_OPTION_ARG_NONE,
&optn_pedantic,
/* TRANSLATORS: ascli flag description for: --pedantic (used by the "validate" command) */
N_ ("Also show pedantic hints."),
NULL },
{ "explain",
(gchar) 0,
0, G_OPTION_ARG_NONE,
&optn_explain,
/* TRANSLATORS: ascli flag description for: --explain (used by the "validate" command) */
N_ ("Print detailed explanation for found issues."),
NULL },
{ "no-net",
(gchar) 0,
0, G_OPTION_ARG_NONE,
&optn_no_net,
/* TRANSLATORS: ascli flag description for: --no-net (used by the "validate" command) */
N_ ("Do not use network access."),
NULL },
{ "strict",
(gchar) 0,
0, G_OPTION_ARG_NONE,
&optn_validate_strict,
/* TRANSLATORS: ascli flag description for: --strict (used by the "validate" command) */
N_ ("Fail validation if any issue is emitted that is not of pedantic severity."),
NULL },
{ "format",
0, 0,
G_OPTION_ARG_STRING, &optn_format,
/* TRANSLATORS: ascli flag description for: --format when validating XML files */
N_ ("Format of the generated report (valid values are 'text' and 'yaml')."),
NULL },
{ "override",
0, 0,
G_OPTION_ARG_STRING, &optn_issue_overrides,
/* TRANSLATORS: ascli flag description for: --override when validating XML files */
N_ ("Override the severities of selected issue tags."),
NULL },
{ NULL }
};
/*** HELPER METHODS ***/
/**
* as_client_get_summary_for:
**/
static gchar *
as_client_get_summary_for (const gchar *command)
{
GString *string;
string = g_string_new ("");
/* TRANSLATORS: This is the header to the --help menu for subcommands */
g_string_append_printf (string, "%s\n", _("AppStream command-line interface"));
g_string_append (string, " ");
g_string_append_printf (string, _("'%s' command"), command);
return g_string_free (string, FALSE);
}
/**
* as_client_new_subcommand_option_context:
*
* Create a new option context for an ascli subcommand.
*/
static GOptionContext *
as_client_new_subcommand_option_context (const gchar *command, const GOptionEntry *entries)
{
GOptionContext *opt_context = NULL;
g_autofree gchar *summary = NULL;
opt_context = g_option_context_new ("- AppStream CLI.");
g_option_context_set_help_enabled (opt_context, TRUE);
if (entries != NULL)
g_option_context_add_main_entries (opt_context, entries, NULL);
/* set the summary text */
summary = as_client_get_summary_for (command);
g_option_context_set_summary (opt_context, summary);
return opt_context;
}
/**
* as_client_print_help_hint:
*/
static void
as_client_print_help_hint (const gchar *subcommand, const gchar *unknown_option)
{
if (unknown_option != NULL) {
/* TRANSLATORS: An unknown option was passed to appstreamcli. */
ascli_print_stderr (_("Option '%s' is unknown."), unknown_option);
}
if (subcommand == NULL)
ascli_print_stderr (
_("Run '%s --help' to see a full list of available command line options."),
ASCLI_BIN_NAME);
else
ascli_print_stderr (
_("Run '%s --help' to see a list of available commands and options, and '%s %s --help' to see a list of options specific for this subcommand."),
ASCLI_BIN_NAME,
ASCLI_BIN_NAME,
subcommand);
}
/**
* as_client_option_context_parse:
*
* Parse the options, print errors.
*/
static int
as_client_option_context_parse (GOptionContext *opt_context,
const gchar *subcommand,
int *argc,
char ***argv)
{
g_autoptr(GError) error = NULL;
g_option_context_parse (opt_context, argc, argv, &error);
if (error != NULL) {
gchar *msg;
msg = g_strconcat (error->message, "\n", NULL);
g_print ("%s", msg);
g_free (msg);
as_client_print_help_hint (subcommand, NULL);
return 1;
}
return 0;
}
/*** SUBCOMMANDS ***/
/**
* as_client_run_refresh_cache:
*
* Refresh the AppStream caches.
*/
static int
as_client_run_refresh_cache (const gchar *command, char **argv, int argc)
{
g_autoptr(GOptionContext) opt_context = NULL;
gint ret;
gboolean optn_force = FALSE;
g_auto(GStrv) optn_sources = NULL;
g_auto(GStrv) optn_sources_real = NULL;
const GOptionEntry refresh_options[] = {
{ "force",
(gchar) 0,
0, G_OPTION_ARG_NONE,
&optn_force,
/* TRANSLATORS: ascli flag description for: --force */
_("Enforce a cache refresh."), NULL },
{ "source",
(gchar) 0,
0, G_OPTION_ARG_STRING_ARRAY,
&optn_sources,
/* TRANSLATORS: ascli flag description for: --source in a refresh action. Don't translate strings in backticks: `name` */
_("Limit cache refresh to data from a specific source, e.g. `os` or `flatpak`. May be specified multiple times."),
NULL },
{ NULL }
};
opt_context = as_client_new_subcommand_option_context (command, refresh_options);
g_option_context_add_main_entries (opt_context, data_catalog_options, NULL);
ret = as_client_option_context_parse (opt_context, command, &argc, &argv);
if (ret != 0)
return ret;
if (optn_sources != NULL) {
if (g_strv_length (optn_sources) == 1)
optn_sources_real = g_strsplit (optn_sources[0], ",", -1);
else
optn_sources_real = g_steal_pointer (&optn_sources);
}
return ascli_refresh_cache (optn_cachepath,
optn_datapath,
(const gchar *const *) optn_sources_real,
optn_force);
}
/**
* as_client_run_search:
*
* Search for AppStream metadata.
*/
static int
as_client_run_search (const gchar *command, char **argv, int argc)
{
g_autoptr(GOptionContext) opt_context = NULL;
g_autoptr(GString) search = NULL;
gint ret;
opt_context = as_client_new_subcommand_option_context (command, find_options);
g_option_context_add_main_entries (opt_context, data_catalog_options, NULL);
ret = as_client_option_context_parse (opt_context, command, &argc, &argv);
if (ret != 0)
return ret;
search = g_string_new ("");
if (argc > 2) {
for (gint i = 2; i < argc; i++) {
g_string_append (search, argv[i]);
g_string_append_c (search, ' ');
}
/* drop trailing space */
if (search->len > 0)
g_string_truncate (search, search->len - 1);
}
return ascli_search_component (optn_cachepath,
(search->len == 0) ? NULL : search->str,
optn_details,
optn_no_cache);
}
/**
* as_client_run_get:
*
* Get components by its ID.
*/
static int
as_client_run_get (const gchar *command, char **argv, int argc)
{
g_autoptr(GOptionContext) opt_context = NULL;
gint ret;
const gchar *value = NULL;
opt_context = as_client_new_subcommand_option_context (command, find_options);
g_option_context_add_main_entries (opt_context, data_catalog_options, NULL);
ret = as_client_option_context_parse (opt_context, command, &argc, &argv);
if (ret != 0)
return ret;
if (argc > 2)
value = argv[2];
return ascli_get_component (optn_cachepath, value, optn_details, optn_no_cache);
}
/**
* as_client_run_dump:
*
* Dump the raw component metadata to the console.
*/
static int
as_client_run_dump (const gchar *command, char **argv, int argc)
{
g_autoptr(GOptionContext) opt_context = NULL;
gint ret;
const gchar *value = NULL;
AsFormatKind mformat;
opt_context = as_client_new_subcommand_option_context (command, data_catalog_options);
g_option_context_add_main_entries (opt_context, format_options, NULL);
ret = as_client_option_context_parse (opt_context, command, &argc, &argv);
if (ret != 0)
return ret;
if (argc > 2)
value = argv[2];
mformat = as_format_kind_from_string (optn_format);
return ascli_dump_component (optn_cachepath, value, mformat, optn_no_cache);
}
/**
* as_client_run_what_provides:
*
* Find components that provide a certain item.
*/
static int
as_client_run_what_provides (const gchar *command, char **argv, int argc)
{
g_autoptr(GOptionContext) opt_context = NULL;
gint ret;
const gchar *vtype = NULL;
const gchar *vvalue = NULL;
opt_context = as_client_new_subcommand_option_context (command, find_options);
g_option_context_add_main_entries (opt_context, data_catalog_options, NULL);
ret = as_client_option_context_parse (opt_context, command, &argc, &argv);
if (ret != 0)
return ret;
if (argc > 2)
vtype = argv[2];
if (argc > 3)
vvalue = argv[3];
return ascli_what_provides (optn_cachepath, vtype, vvalue, optn_details);
}
/**
* as_client_run_list_categories:
*
* Find components that are in the listed categories.
*/
static int
as_client_run_list_categories (const gchar *command, char **argv, int argc)
{
g_autoptr(GOptionContext) opt_context = NULL;
gint ret;
g_auto(GStrv) categories = NULL;
opt_context = as_client_new_subcommand_option_context (command, find_options);
g_option_context_add_main_entries (opt_context, data_catalog_options, NULL);
ret = as_client_option_context_parse (opt_context, command, &argc, &argv);
if (ret != 0)
return ret;
if (argc > 2) {
categories = g_new0 (gchar *, argc - 1);
for (gint i = 0; i < (argc - 2); i++)
categories[i] = g_strdup (argv[i + 2]);
}
return ascli_list_categories (optn_cachepath, categories, optn_details, optn_no_cache);
}
/**
* as_client_run_validate:
*
* Validate single metadata files.
*/
static int
as_client_run_validate (const gchar *command, char **argv, int argc)
{
g_autoptr(GOptionContext) opt_context = NULL;
gint ret;
opt_context = as_client_new_subcommand_option_context (command, validate_options);
ret = as_client_option_context_parse (opt_context, command, &argc, &argv);
if (ret != 0)
return ret;
if (optn_format == NULL) {
return ascli_validate_files (&argv[2],
argc - 2,
optn_pedantic,
optn_explain,
optn_validate_strict,
!optn_no_net,
optn_issue_overrides);
} else {
return ascli_validate_files_format (&argv[2],
argc - 2,
optn_format,
optn_validate_strict,
!optn_no_net,
optn_issue_overrides);
}
}
/**
* as_client_run_validate_tree:
*
* Validate an installed filesystem tree for correct AppStream metadata
* and .desktop files.
*/
static int
as_client_run_validate_tree (const gchar *command, char **argv, int argc)
{
g_autoptr(GOptionContext) opt_context = NULL;
gint ret;
const gchar *value = NULL;
opt_context = as_client_new_subcommand_option_context (command, validate_options);
ret = as_client_option_context_parse (opt_context, command, &argc, &argv);
if (ret != 0)
return ret;
if (argc > 2)
value = argv[2];
if (optn_format == NULL) {
return ascli_validate_tree (value,
optn_pedantic,
optn_explain,
optn_validate_strict,
!optn_no_net,
optn_issue_overrides);
} else {
return ascli_validate_tree_format (value,
optn_format,
optn_validate_strict,
!optn_no_net,
optn_issue_overrides);
}
}
/**
* as_client_run_check_license:
*
* Print license information.
*/
static int
as_client_run_check_license (const gchar *command, char **argv, int argc)
{
g_autoptr(GOptionContext) opt_context = NULL;
gint ret;
opt_context = as_client_new_subcommand_option_context (command, NULL);
ret = as_client_option_context_parse (opt_context, command, &argc, &argv);
if (ret != 0)
return ret;
if (argc != 3) {
/* TRANSLATORS: ascli check-license is missing its parameter */
ascli_print_stderr (
_("No license, license expression or license exception string was provided."));
return 4;
}
return ascli_check_license (argv[2]);
}
/**
* as_client_run_is_satisfied:
*
* Test if a component has its relations satisfied on the current system.
*/
static int
as_client_run_is_satisfied (const gchar *command, char **argv, int argc)
{
g_autoptr(GOptionContext) opt_context = NULL;
gint ret;
const gchar *fname_or_cid = NULL;
opt_context = as_client_new_subcommand_option_context (command, find_options);
g_option_context_add_main_entries (opt_context, data_catalog_options, NULL);
ret = as_client_option_context_parse (opt_context, command, &argc, &argv);
if (ret != 0)
return ret;
if (argc > 2)
fname_or_cid = argv[2];
return ascli_check_is_satisfied (fname_or_cid, optn_cachepath, optn_no_cache);
}
/**
* as_client_run_check_syscompat:
*
* Check component against a variety of system types.
*/
static int
as_client_run_check_syscompat (const gchar *command, char **argv, int argc)
{
g_autoptr(GOptionContext) opt_context = NULL;
gint ret;
const gchar *fname_or_cid = NULL;
gboolean optn_sc_details = FALSE;
const GOptionEntry check_syscompat_options[] = {
{ "details",
0, 0,
G_OPTION_ARG_NONE, &optn_sc_details,
/* TRANSLATORS: ascli flag description for: --details (part of the "check-syscompat" subcommand) */
N_ ("Print more detailed output on why incompatibilities exist."),
NULL },
{ NULL }
};
opt_context = as_client_new_subcommand_option_context (command, check_syscompat_options);
g_option_context_add_main_entries (opt_context, data_catalog_options, NULL);
ret = as_client_option_context_parse (opt_context, command, &argc, &argv);
if (ret != 0)
return ret;
if (argc > 2)
fname_or_cid = argv[2];
return ascli_check_syscompat (fname_or_cid, optn_cachepath, optn_no_cache, optn_sc_details);
}
/**
* as_client_run_put:
*
* Place a metadata file in the right directory.
*/
static int
as_client_run_put (const gchar *command, char **argv, int argc)
{
g_autoptr(GOptionContext) opt_context = NULL;
const gchar *fname = NULL;
const gchar *optn_origin = NULL;
gboolean optn_usermode = FALSE;
gint ret;
const GOptionEntry put_file_options[] = {
{ "origin",
0, 0,
G_OPTION_ARG_STRING, &optn_origin,
/* TRANSLATORS: ascli flag description for: --origin (part of the "put" subcommand) */
N_ ("Set the data origin for the installed metadata catalog file."),
NULL },
{ "user",
0, 0,
G_OPTION_ARG_NONE, &optn_usermode,
/* TRANSLATORS: ascli flag description for: --user (part of the "put" subcommand) */
N_ ("Install the file for the current user, instead of globally."),
NULL },
{ NULL }
};
opt_context = as_client_new_subcommand_option_context (command, put_file_options);
ret = as_client_option_context_parse (opt_context, command, &argc, &argv);
if (ret != 0)
return ret;
if (argc > 2)
fname = argv[2];
if (argc > 3) {
as_client_print_help_hint (command, argv[3]);
return 1;
}
return ascli_put_metainfo (fname, optn_origin, optn_usermode);
}
static const gchar *optn_bundle_type = NULL;
static gboolean optn_choose_first = FALSE;
const GOptionEntry pkgmanage_options[] = {
{ "bundle-type",
0, 0,
G_OPTION_ARG_STRING, &optn_bundle_type,
/* TRANSLATORS: ascli flag description for: --bundle-type (part of the "remove" and "install" subcommands) */
N_ ("Limit the command to use only components from the given bundling system (`flatpak` "
"or `package`)."),
NULL },
{ "first",
0, 0,
G_OPTION_ARG_NONE, &optn_choose_first,
/* TRANSLATORS: ascli flag description for: --first (part of the "remove" and "install" subcommands) */
N_ ("Do not ask for which software component should be used and always choose the first "
"entry."),
NULL },
{ NULL }
};
/**
* as_client_run_install:
*
* Install a component by its ID.
*/
static int
as_client_run_install (const gchar *command, char **argv, int argc)
{
g_autoptr(GOptionContext) opt_context = NULL;
const gchar *value = NULL;
AsBundleKind bundle_kind;
gint ret;
opt_context = as_client_new_subcommand_option_context (command, pkgmanage_options);
ret = as_client_option_context_parse (opt_context, command, &argc, &argv);
if (ret != 0)
return ret;
if (argc > 2)
value = argv[2];
if (argc > 3) {
as_client_print_help_hint (command, argv[3]);
return 1;
}
bundle_kind = as_bundle_kind_from_string (optn_bundle_type);
if (optn_bundle_type != NULL && bundle_kind == AS_BUNDLE_KIND_UNKNOWN) {
/* TRANSLATORS: ascli install currently only supports two values for --bundle-type. */
ascli_print_stderr (_("No valid bundle kind was specified. Only `package` and `flatpak` are currently recognized."));
return ASCLI_EXIT_CODE_BAD_INPUT;
}
return ascli_install_component (value, bundle_kind, optn_choose_first);
}
/**
* as_client_run_remove:
*
* Uninstall a component by its ID.
*/
static int
as_client_run_remove (const gchar *command, char **argv, int argc)
{
g_autoptr(GOptionContext) opt_context = NULL;
const gchar *value = NULL;
AsBundleKind bundle_kind;
gint ret;
opt_context = as_client_new_subcommand_option_context (command, pkgmanage_options);
ret = as_client_option_context_parse (opt_context, command, &argc, &argv);
if (ret != 0)
return ret;
if (argc > 2)
value = argv[2];
if (argc > 3) {
as_client_print_help_hint (command, argv[3]);
return 1;
}
bundle_kind = as_bundle_kind_from_string (optn_bundle_type);
if (optn_bundle_type != NULL && bundle_kind == AS_BUNDLE_KIND_UNKNOWN) {
/* TRANSLATORS: ascli install currently only supports two values for --bundle-type. */
ascli_print_stderr (_("No valid bundle kind was specified. Only `package` and `flatpak` are currently recognized."));
return ASCLI_EXIT_CODE_BAD_INPUT;
}
return ascli_remove_component (value, bundle_kind, optn_choose_first);
}
/**
* as_client_run_status:
*
* Show diagnostic information.
*/
static int
as_client_run_status (const gchar *command, char **argv, int argc)
{
if (argc > 2) {
as_client_print_help_hint (command, argv[3]);
return 1;
}
return ascli_show_status ();
}
/**
* as_client_run_sysinfo:
*
* Show information about the current operating system and device.
*/
static int
as_client_run_sysinfo (const gchar *command, char **argv, int argc)
{
g_autoptr(GOptionContext) opt_context = NULL;
gint ret;
opt_context = as_client_new_subcommand_option_context (command, find_options);
g_option_context_add_main_entries (opt_context, data_catalog_options, NULL);
ret = as_client_option_context_parse (opt_context, command, &argc, &argv);
if (ret != 0)
return ret;
if (argc > 2) {
as_client_print_help_hint (command, argv[3]);
return 1;
}
return ascli_show_sysinfo (optn_cachepath, optn_no_cache, optn_details);
}
/**
* as_client_run_convert:
*
* Convert metadata.
*/
static int
as_client_run_convert (const gchar *command, char **argv, int argc)
{
g_autoptr(GOptionContext) opt_context = NULL;
gint ret;
const gchar *fname1 = NULL;
const gchar *fname2 = NULL;
AsFormatKind mformat;
opt_context = as_client_new_subcommand_option_context (command, format_options);
ret = as_client_option_context_parse (opt_context, command, &argc, &argv);
if (ret != 0)
return ret;
if (argc > 2)
fname1 = argv[2];
if (argc > 3)
fname2 = argv[3];
mformat = as_format_kind_from_string (optn_format);
return ascli_convert_data (fname1, fname2, mformat);
}
/**
* as_client_run_compare_versions:
*
* Compare versions using AppStream's version comparison algorithm.
*/
static int
as_client_run_compare_versions (const gchar *command, char **argv, int argc)
{
g_autoptr(GOptionContext) opt_context = NULL;
gint ret;
opt_context = as_client_new_subcommand_option_context (command, format_options);
ret = as_client_option_context_parse (opt_context, command, &argc, &argv);
if (ret != 0)
return ret;
if (argc < 4) {
ascli_print_stderr (_("You need to provide at least two version numbers to compare as parameters."));
return 2;
}
if (argc == 4) {
const gchar *ver1 = argv[2];
const gchar *ver2 = argv[3];
gint comp_res = as_vercmp_simple (ver1, ver2);
if (comp_res == 0)
g_print ("%s == %s\n", ver1, ver2);
else if (comp_res > 0)
g_print ("%s >> %s\n", ver1, ver2);
else if (comp_res < 0)
g_print ("%s << %s\n", ver1, ver2);
return 0;
} else if (argc == 5) {
AsRelationCompare compare;
gint rc;
gboolean res;
const gchar *ver1 = argv[2];
const gchar *comp_str = argv[3];
const gchar *ver2 = argv[4];
compare = as_relation_compare_from_string (comp_str);
if (compare == AS_RELATION_COMPARE_UNKNOWN) {
guint i;
/** TRANSLATORS: The user tried to compare version numbers, but the comparison operator (greater-then, equal, etc.) was invalid. */
ascli_print_stderr (_("Unknown compare relation '%s'. Valid values are:"),
comp_str);
for (i = 1; i < AS_RELATION_COMPARE_LAST; i++)
g_printerr (" • %s\n", as_relation_compare_to_string (i));
return 2;
}
rc = as_vercmp_simple (ver1, ver2);
switch (compare) {
case AS_RELATION_COMPARE_EQ:
res = rc == 0;
break;
case AS_RELATION_COMPARE_NE:
res = rc != 0;
break;
case AS_RELATION_COMPARE_LT:
res = rc < 0;
break;
case AS_RELATION_COMPARE_GT:
res = rc > 0;
break;
case AS_RELATION_COMPARE_LE:
res = rc <= 0;
break;
case AS_RELATION_COMPARE_GE:
res = rc >= 0;
break;
default:
res = FALSE;
}
g_print ("%s: ", res ? "true" : "false");
if (rc == 0)
g_print ("%s == %s\n", ver1, ver2);
else if (rc > 0)
g_print ("%s >> %s\n", ver1, ver2);
else if (rc < 0)
g_print ("%s << %s\n", ver1, ver2);
return res ? 0 : 1;
} else {
ascli_print_stderr (_("Too many parameters: Need two version numbers or version numbers and a comparison operator."));
return 2;
}
}
/**
* as_client_run_new_template:
*
* Convert metadata.
*/
static int
as_client_run_new_template (const gchar *command, char **argv, int argc)
{
g_autoptr(GOptionContext) opt_context = NULL;
g_autoptr(GString) desc_str = NULL;
guint i;
gint ret;
const gchar *out_fname = NULL;
const gchar *cpt_kind_str = NULL;
const gchar *optn_desktop_file = NULL;
const GOptionEntry newtemplate_options[] = {
{ "from-desktop",
0, 0,
G_OPTION_ARG_STRING, &optn_desktop_file,
/* TRANSLATORS: ascli flag description for: --from-desktop (part of the new-template subcommand) */
N_ ("Use the given .desktop file to fill in the basic values of the metainfo "
"file."),
NULL },
{ NULL }
};
/* TRANSLATORS: Additional help text for the 'new-template' ascli subcommand */
desc_str = g_string_new (
_("This command takes optional TYPE and FILE positional arguments, FILE being a file to write to (or \"-\" for standard output)."));
g_string_append (desc_str, "\n");
/* TRANSLATORS: Additional help text for the 'new-template' ascli subcommand, a bullet-pointed list of types follows */
g_string_append_printf (desc_str,
_("The TYPE must be a valid component-type, such as: %s"), "\n");
for (i = 1; i < AS_COMPONENT_KIND_LAST; i++)
g_string_append_printf (desc_str, " • %s\n", as_component_kind_to_string (i));
opt_context = as_client_new_subcommand_option_context (command, newtemplate_options);
g_option_context_set_description (opt_context, desc_str->str);
ret = as_client_option_context_parse (opt_context, command, &argc, &argv);
if (ret != 0)
return ret;
if (argc > 2)
cpt_kind_str = argv[2];
if (argc > 3)
out_fname = argv[3];
return ascli_create_metainfo_template (out_fname, cpt_kind_str, optn_desktop_file);
}
/**
* as_client_run_make_desktop_file:
*
* Create desktop-entry file from metainfo file.
*/
static int
as_client_run_make_desktop_file (const gchar *command, char **argv, int argc)
{
g_autoptr(GOptionContext) opt_context = NULL;
const gchar *optn_exec_command = NULL;
const gchar *mi_fname = NULL;
const gchar *de_fname = NULL;
gint ret;
const GOptionEntry make_desktop_file_options[] = {
{ "exec",
0, 0,
G_OPTION_ARG_STRING, &optn_exec_command,
/* TRANSLATORS: ascli flag description for: --exec (part of the make-desktop-file subcommand) */
N_ ("Use the specified line for the 'Exec=' key of the desktop-entry file."),
NULL },
{ NULL }
};
opt_context = as_client_new_subcommand_option_context (command, make_desktop_file_options);
ret = as_client_option_context_parse (opt_context, command, &argc, &argv);
if (ret != 0)
return ret;
if (argc > 2)
mi_fname = argv[2];
if (argc > 3)
de_fname = argv[3];
return ascli_make_desktop_entry_file (mi_fname, de_fname, optn_exec_command);
}