Skip to content

Commit 8d4b43c

Browse files
committed
cli: Add "get-latest-version" util command
This gets the latest version specified in an appdata file, which, combined with utilities such as check-news.sh[1], can be used to check whether the date specified in the appdata's release information is correct. See https://gitlab.gnome.org/GNOME/evince/-/issues/1950 [1]: https://gitlab.gnome.org/GNOME/totem/-/blob/master/check-news.sh
1 parent 3ac199f commit 8d4b43c

3 files changed

Lines changed: 93 additions & 0 deletions

File tree

tools/appstreamcli.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,26 @@ as_client_run_is_satisfied (const gchar *command, char **argv, int argc)
564564
optn_no_cache);
565565
}
566566

567+
/**
568+
* as_client_run_get_latest_version:
569+
*
570+
* Get the latest version specified in an AppData file.
571+
*/
572+
static int
573+
as_client_run_get_latest_version (const gchar *command, char **argv, int argc)
574+
{
575+
const gchar *fname = NULL;
576+
577+
if (argc > 2)
578+
fname = argv[2];
579+
if (argc > 3) {
580+
as_client_print_help_hint (command, argv[3]);
581+
return 1;
582+
}
583+
584+
return ascli_get_latest_version_file (fname);
585+
}
586+
567587
/**
568588
* as_client_run_put:
569589
*
@@ -1343,6 +1363,11 @@ as_client_run (char **argv, int argc)
13431363
/* TRANSLATORS: `appstreamcli `is-satisfied` command description. */
13441364
_("Check if requirements of a component (via its ID or MetaInfo file) are satisfied on this system."),
13451365
as_client_run_is_satisfied);
1366+
ascli_add_cmd (commands,
1367+
2, "get-latest-version", NULL, "FILE",
1368+
/* TRANSLATORS: `appstreamcli get-latest-version command description. */
1369+
_("Get the latest version from the AppData file"),
1370+
as_client_run_get_latest_version);
13461371

13471372
ascli_add_cmd (commands,
13481373
3, "install", NULL, "COMPONENT-ID",

tools/ascli-actions-mdata.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,3 +739,69 @@ ascli_check_is_satisfied (const gchar *fname_or_cid, const gchar *cachepath, gbo
739739

740740
return res? ASCLI_EXIT_CODE_SUCCESS : ASCLI_EXIT_CODE_FAILED;
741741
}
742+
743+
gint
744+
ascli_get_latest_version_file (const gchar *fname)
745+
{
746+
g_autoptr(AsMetadata) metad = NULL;
747+
g_autoptr(AsComponent) cpt = NULL;
748+
g_autoptr(GFile) infile = NULL;
749+
g_autoptr(GError) error = NULL;
750+
751+
if (fname == NULL) {
752+
ascli_print_stderr (_("You need to specify an input file."));
753+
return 1;
754+
}
755+
756+
/* load input file */
757+
infile = g_file_new_for_path (fname);
758+
if (!g_file_query_exists (infile, NULL)) {
759+
ascli_print_stderr (_("Metadata file '%s' does not exist."), fname);
760+
return 2;
761+
}
762+
763+
metad = as_metadata_new ();
764+
as_metadata_set_locale (metad, "ALL");
765+
766+
if ((g_str_has_suffix (fname, ".yml.gz")) ||
767+
(g_str_has_suffix (fname, ".yaml.gz")) ||
768+
(g_str_has_suffix (fname, ".yml")) ||
769+
(g_str_has_suffix (fname, ".yaml"))) {
770+
/* if we have YAML, we also automatically assume a catalog style */
771+
as_metadata_set_format_style (metad, AS_FORMAT_STYLE_CATALOG);
772+
} else if (g_str_has_suffix (fname, ".metainfo.xml") || g_str_has_suffix (fname, ".appdata.xml")) {
773+
as_metadata_set_format_style (metad, AS_FORMAT_STYLE_METAINFO);
774+
} else {
775+
as_metadata_set_format_style (metad, AS_FORMAT_STYLE_CATALOG);
776+
}
777+
778+
as_metadata_parse_file (metad,
779+
infile,
780+
AS_FORMAT_KIND_UNKNOWN,
781+
&error);
782+
if (error != NULL) {
783+
g_printerr ("%s\n", error->message);
784+
return 3;
785+
}
786+
787+
cpt = g_object_ref (as_metadata_get_component (metad));
788+
789+
if (as_component_get_releases (cpt)->len > 0) {
790+
GPtrArray *releases = as_component_get_releases (cpt);
791+
AsRelease *release_newest = NULL;
792+
793+
for (guint i = 0; i < releases->len; i++) {
794+
AsRelease *release_tmp = g_ptr_array_index (releases, i);
795+
if (release_newest == NULL ||
796+
as_release_get_timestamp (release_tmp) > as_release_get_timestamp (release_newest))
797+
release_newest = release_tmp;
798+
}
799+
800+
g_print ("%s\n", as_release_get_version (release_newest));
801+
} else {
802+
ascli_print_stderr (_("No releases information available in '%s'."), fname);
803+
return 4;
804+
}
805+
806+
return ASCLI_EXIT_CODE_SUCCESS;
807+
}

tools/ascli-actions-mdata.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ gint ascli_check_is_satisfied (const gchar *fname_or_cid,
7272
const gchar *cachepath,
7373
gboolean no_cache);
7474

75+
gint ascli_get_latest_version_file (const gchar *fname);
76+
7577

7678
G_END_DECLS
7779

0 commit comments

Comments
 (0)