Skip to content

Commit

Permalink
cli: Add "get-latest-version" util command
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hadess committed Jun 19, 2023
1 parent 3ac199f commit 8d4b43c
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tools/appstreamcli.c
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,26 @@ as_client_run_is_satisfied (const gchar *command, char **argv, int argc)
optn_no_cache);
}

/**
* as_client_run_get_latest_version:
*
* Get the latest version specified in an AppData file.
*/
static int
as_client_run_get_latest_version (const gchar *command, char **argv, int argc)
{
const gchar *fname = NULL;

if (argc > 2)
fname = argv[2];
if (argc > 3) {
as_client_print_help_hint (command, argv[3]);
return 1;
}

return ascli_get_latest_version_file (fname);
}

/**
* as_client_run_put:
*
Expand Down Expand Up @@ -1343,6 +1363,11 @@ as_client_run (char **argv, int argc)
/* TRANSLATORS: `appstreamcli `is-satisfied` command description. */
_("Check if requirements of a component (via its ID or MetaInfo file) are satisfied on this system."),
as_client_run_is_satisfied);
ascli_add_cmd (commands,
2, "get-latest-version", NULL, "FILE",
/* TRANSLATORS: `appstreamcli get-latest-version command description. */
_("Get the latest version from the AppData file"),
as_client_run_get_latest_version);

ascli_add_cmd (commands,
3, "install", NULL, "COMPONENT-ID",
Expand Down
66 changes: 66 additions & 0 deletions tools/ascli-actions-mdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -739,3 +739,69 @@ ascli_check_is_satisfied (const gchar *fname_or_cid, const gchar *cachepath, gbo

return res? ASCLI_EXIT_CODE_SUCCESS : ASCLI_EXIT_CODE_FAILED;
}

gint
ascli_get_latest_version_file (const gchar *fname)
{
g_autoptr(AsMetadata) metad = NULL;
g_autoptr(AsComponent) cpt = NULL;
g_autoptr(GFile) infile = NULL;
g_autoptr(GError) error = NULL;

if (fname == NULL) {
ascli_print_stderr (_("You need to specify an input file."));
return 1;
}

/* load input file */
infile = g_file_new_for_path (fname);
if (!g_file_query_exists (infile, NULL)) {
ascli_print_stderr (_("Metadata file '%s' does not exist."), fname);
return 2;
}

metad = as_metadata_new ();
as_metadata_set_locale (metad, "ALL");

if ((g_str_has_suffix (fname, ".yml.gz")) ||
(g_str_has_suffix (fname, ".yaml.gz")) ||
(g_str_has_suffix (fname, ".yml")) ||
(g_str_has_suffix (fname, ".yaml"))) {
/* if we have YAML, we also automatically assume a catalog style */
as_metadata_set_format_style (metad, AS_FORMAT_STYLE_CATALOG);
} else if (g_str_has_suffix (fname, ".metainfo.xml") || g_str_has_suffix (fname, ".appdata.xml")) {
as_metadata_set_format_style (metad, AS_FORMAT_STYLE_METAINFO);
} else {
as_metadata_set_format_style (metad, AS_FORMAT_STYLE_CATALOG);
}

as_metadata_parse_file (metad,
infile,
AS_FORMAT_KIND_UNKNOWN,
&error);
if (error != NULL) {
g_printerr ("%s\n", error->message);
return 3;
}

cpt = g_object_ref (as_metadata_get_component (metad));

if (as_component_get_releases (cpt)->len > 0) {
GPtrArray *releases = as_component_get_releases (cpt);
AsRelease *release_newest = NULL;

for (guint i = 0; i < releases->len; i++) {
AsRelease *release_tmp = g_ptr_array_index (releases, i);
if (release_newest == NULL ||
as_release_get_timestamp (release_tmp) > as_release_get_timestamp (release_newest))
release_newest = release_tmp;
}

g_print ("%s\n", as_release_get_version (release_newest));
} else {
ascli_print_stderr (_("No releases information available in '%s'."), fname);
return 4;
}

return ASCLI_EXIT_CODE_SUCCESS;
}
2 changes: 2 additions & 0 deletions tools/ascli-actions-mdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ gint ascli_check_is_satisfied (const gchar *fname_or_cid,
const gchar *cachepath,
gboolean no_cache);

gint ascli_get_latest_version_file (const gchar *fname);


G_END_DECLS

Expand Down

0 comments on commit 8d4b43c

Please sign in to comment.