diff --git a/Makefile.am b/Makefile.am index f303e462..f2ac4aed 100644 --- a/Makefile.am +++ b/Makefile.am @@ -16,6 +16,7 @@ xdg_app_SOURCES = \ xdg-app-builtins.h \ xdg-app-builtins-add-repo.c \ xdg-app-builtins-install.c \ + xdg-app-builtins-update.c \ xdg-app-dir.c \ xdg-app-dir.h \ xdg-app-utils.h \ diff --git a/xdg-app-builtins-update.c b/xdg-app-builtins-update.c new file mode 100644 index 00000000..91af946c --- /dev/null +++ b/xdg-app-builtins-update.c @@ -0,0 +1,91 @@ +#include "config.h" + +#include +#include +#include +#include + +#include "libgsystem.h" + +#include "xdg-app-builtins.h" +#include "xdg-app-utils.h" + +static char *opt_arch; + +static GOptionEntry options[] = { + { "arch", 0, 0, G_OPTION_ARG_STRING, &opt_arch, "Arch to update for", NULL }, + { NULL } +}; + +static void +usage_error (GOptionContext *context, const char *message, GError **error) +{ + gs_free gchar *help = g_option_context_get_help (context, TRUE, NULL); + g_printerr ("%s", help); + g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, message); +} + +gboolean +xdg_app_builtin_update_runtime (int argc, char **argv, GCancellable *cancellable, GError **error) +{ + gboolean ret = FALSE; + GOptionContext *context; + gs_unref_object XdgAppDir *dir = NULL; + gs_unref_object GFile *deploy_base = NULL; + gs_unref_object GFile *origin = NULL; + const char *runtime; + const char *branch = "master"; + gs_free char *ref = NULL; + gs_free char *repository = NULL; + GError *my_error; + + context = g_option_context_new ("RUNTIME [BRANCH] - Update a runtime"); + + if (!xdg_app_option_context_parse (context, options, &argc, &argv, 0, &dir, cancellable, error)) + goto out; + + if (argc < 3) + { + usage_error (context, "REPOSITORY and RUNTIME must be specified", error); + goto out; + } + + runtime = argv[1]; + if (argc >= 3) + branch = argv[2]; + + ref = xdg_app_build_runtime_ref (runtime, branch, opt_arch); + + deploy_base = xdg_app_dir_get_deploy_dir (dir, ref); + if (!g_file_query_exists (deploy_base, cancellable)) + { + g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Runtime %s branch %s not installed", runtime, branch); + goto out; + } + + origin = g_file_get_child (deploy_base, "origin"); + if (!g_file_load_contents (origin, cancellable, &repository, NULL, NULL, error)) + goto out; + + if (!xdg_app_dir_pull (dir, repository, ref, + cancellable, error)) + goto out; + + my_error = NULL; + if (!xdg_app_dir_deploy (dir, ref, NULL, cancellable, &my_error)) + { + if (g_error_matches (my_error, XDG_APP_DIR_ERROR, XDG_APP_DIR_ERROR_ALREADY_DEPLOYED)) + g_error_free (my_error); + else + { + g_propagate_error (error, my_error); + goto out; + } + } + + ret = TRUE; + out: + if (context) + g_option_context_free (context); + return ret; +} diff --git a/xdg-app-builtins.h b/xdg-app-builtins.h index 66e2a6fb..bc8ea34c 100644 --- a/xdg-app-builtins.h +++ b/xdg-app-builtins.h @@ -26,6 +26,7 @@ gboolean xdg_app_option_context_parse (GOptionContext *context, BUILTINPROTO(add_repo); BUILTINPROTO(install_runtime); +BUILTINPROTO(update_runtime); #undef BUILTINPROTO diff --git a/xdg-app-main.c b/xdg-app-main.c index 24a7f721..9258f45c 100644 --- a/xdg-app-main.c +++ b/xdg-app-main.c @@ -21,6 +21,7 @@ typedef struct { static XdgAppCommand commands[] = { { "add-repo", xdg_app_builtin_add_repo }, { "install-runtime", xdg_app_builtin_install_runtime }, + { "update-runtime", xdg_app_builtin_update_runtime }, { NULL } };