ostree admin: Add a --print-current-dir option

Add an option --print-current-dir that prints the current deployment
directory to stdout and exits.

https://bugzilla.gnome.org/show_bug.cgi?id=731051
This commit is contained in:
Owen W. Taylor 2014-09-23 11:32:44 -04:00
parent 262cba09c0
commit d64d003af0
2 changed files with 43 additions and 4 deletions

View File

@ -97,6 +97,13 @@ Boston, MA 02111-1307, USA.
<term><option>--sysroot</option>="PATH"</term> <term><option>--sysroot</option>="PATH"</term>
<listitem><para>Creates a new OSTree sysroot at PATH</para></listitem> <listitem><para>Creates a new OSTree sysroot at PATH</para></listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term><option>--print-current-dir</option></term>
<listitem><para>
Prints the full path to the deployment directory for the currently active deployment in the specified sysroot to standard out. This is incompatible with specifying a subcommand.
</para></listitem>
</varlistentry>
</variablelist> </variablelist>
</refsect1> </refsect1>
</refentry> </refentry>

View File

@ -61,6 +61,7 @@ ostree_builtin_admin (int argc, char **argv, OstreeRepo *repo, GCancellable *can
gs_unref_object GFile *sysroot_path = NULL; gs_unref_object GFile *sysroot_path = NULL;
gs_unref_object OstreeSysroot *sysroot = NULL; gs_unref_object OstreeSysroot *sysroot = NULL;
gboolean want_help = FALSE; gboolean want_help = FALSE;
gboolean want_current_dir = FALSE;
int in, out, i; int in, out, i;
gboolean skip; gboolean skip;
@ -93,6 +94,10 @@ ostree_builtin_admin (int argc, char **argv, OstreeRepo *repo, GCancellable *can
{ {
want_help = TRUE; want_help = TRUE;
} }
else if (g_str_equal (argv[in], "--print-current-dir"))
{
want_current_dir = TRUE;
}
else if (g_str_equal (argv[in], "--sysroot") && in + 1 < argc) else if (g_str_equal (argv[in], "--sysroot") && in + 1 < argc)
{ {
opt_sysroot = argv[in + 1]; opt_sysroot = argv[in + 1];
@ -145,12 +150,12 @@ ostree_builtin_admin (int argc, char **argv, OstreeRepo *repo, GCancellable *can
argc = out; argc = out;
if (subcommand_name == NULL) if (subcommand_name == NULL && (want_help || !want_current_dir))
{ {
void (*print_func) (const gchar *format, ...) = want_help ? g_print : g_printerr; void (*print_func) (const gchar *format, ...) = want_help ? g_print : g_printerr;
subcommand = admin_subcommands; subcommand = admin_subcommands;
print_func ("usage: ostree admin --sysroot=PATH COMMAND [options]\n"); print_func ("usage: ostree admin [--sysroot=PATH] [--print-current-dir|COMMAND] [options]\n");
print_func ("Builtin commands:\n"); print_func ("Builtin commands:\n");
while (subcommand->name) while (subcommand->name)
{ {
@ -166,6 +171,35 @@ ostree_builtin_admin (int argc, char **argv, OstreeRepo *repo, GCancellable *can
goto out; goto out;
} }
sysroot_path = g_file_new_for_path (opt_sysroot);
sysroot = ostree_sysroot_new (sysroot_path);
if (want_current_dir)
{
gs_unref_ptrarray GPtrArray *deployments = NULL;
OstreeDeployment *first_deployment;
gs_unref_object GFile *deployment_file = NULL;
gs_free char *deployment_path = NULL;
if (!ostree_sysroot_load (sysroot, cancellable, error))
goto out;
deployments = ostree_sysroot_get_deployments (sysroot);
if (deployments->len == 0)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Unable to find a deployment in sysroot");
goto out;
}
first_deployment = deployments->pdata[0];
deployment_file = ostree_sysroot_get_deployment_directory (sysroot, first_deployment);
deployment_path = g_file_get_path (deployment_file);
g_print ("%s\n", deployment_path);
ret = TRUE;
goto out;
}
subcommand = admin_subcommands; subcommand = admin_subcommands;
while (subcommand->name) while (subcommand->name)
{ {
@ -183,8 +217,6 @@ ostree_builtin_admin (int argc, char **argv, OstreeRepo *repo, GCancellable *can
g_set_prgname (g_strdup_printf ("ostree admin %s", subcommand_name)); g_set_prgname (g_strdup_printf ("ostree admin %s", subcommand_name));
sysroot_path = g_file_new_for_path (opt_sysroot);
sysroot = ostree_sysroot_new (sysroot_path);
if (!subcommand->fn (argc, argv, sysroot, cancellable, error)) if (!subcommand->fn (argc, argv, sysroot, cancellable, error))
goto out; goto out;