[ostree/wip/osname-rework] admin: Take --osname argument instead of explicit OSNAME arg



commit 7de71e3df0266af223e4a054d66a7d41f4c08f48
Author: Colin Walters <walters verbum org>
Date:   Mon Feb 25 10:18:29 2013 -0500

    admin: Take --osname argument instead of explicit OSNAME arg
    
    The primary benefit of this is that when you're booted inside an an
    OSTree system, we can default to that, rather than forcing you to
    explicitly specify each time.

 src/ostree/ot-admin-builtin-deploy.c        |   17 ++++----
 src/ostree/ot-admin-builtin-diff.c          |   17 ++------
 src/ostree/ot-admin-builtin-install.c       |   12 ++++--
 src/ostree/ot-admin-builtin-os-init.c       |   13 +-----
 src/ostree/ot-admin-builtin-prune.c         |   17 ++------
 src/ostree/ot-admin-builtin-pull-deploy.c   |   25 ++++-------
 src/ostree/ot-admin-builtin-update-kernel.c |   10 +----
 src/ostree/ot-admin-builtin-upgrade.c       |   17 ++------
 src/ostree/ot-admin-builtins.h              |    3 +
 src/ostree/ot-admin-functions.c             |   47 ++++++++++++++++++++++
 src/ostree/ot-admin-functions.h             |    4 ++
 src/ostree/ot-builtin-admin.c               |   57 +++++++++++++++++++--------
 12 files changed, 136 insertions(+), 103 deletions(-)
---
diff --git a/src/ostree/ot-admin-builtin-deploy.c b/src/ostree/ot-admin-builtin-deploy.c
index 24bc0da..f4df742 100644
--- a/src/ostree/ot-admin-builtin-deploy.c
+++ b/src/ostree/ot-admin-builtin-deploy.c
@@ -602,9 +602,9 @@ do_update_kernel (OtAdminDeploy     *self,
   args = g_ptr_array_new ();
   ot_ptrarray_add_many (args, "ostree", "admin",
                         "--ostree-dir", gs_file_get_path_cached (self->ostree_dir),
+                        "--osname", self->admin_opts->osname,
                         "--boot-dir", gs_file_get_path_cached (self->admin_opts->boot_dir),
                         "update-kernel",
-                        self->osname,
                         gs_file_get_path_cached (deploy_path), NULL);
   g_ptr_array_add (args, NULL);
 
@@ -638,16 +638,16 @@ ot_admin_builtin_deploy (int argc, char **argv, OtAdminBuiltinOpts *admin_opts,
 
   memset (self, 0, sizeof (*self));
 
-  context = g_option_context_new ("OSNAME TREENAME [REVISION] - In operating system OS, check out revision 
TREENAME (or REVISION as TREENAME)");
+  context = g_option_context_new ("TREENAME [REVISION] - Deploy TREENAME (or REVISION as TREENAME)");
 
   g_option_context_add_main_entries (context, options, NULL);
 
   if (!g_option_context_parse (context, &argc, &argv, error))
     goto out;
 
-  if (argc < 3)
+  if (argc < 2)
     {
-      ot_util_usage_error (context, "OSNAME and TREENAME must be specified", error);
+      ot_util_usage_error (context, "TREENAME must be specified", error);
       goto out;
     }
 
@@ -662,12 +662,11 @@ ot_admin_builtin_deploy (int argc, char **argv, OtAdminBuiltinOpts *admin_opts,
   if (!ostree_repo_check (self->repo, error))
     goto out;
 
-  osname = argv[1];
-  deploy_target = argv[2];
-  if (argc > 3)
-    revision = argv[3];
+  deploy_target = argv[1];
+  if (argc > 2)
+    revision = argv[2];
 
-  self->osname = g_strdup (osname);
+  self->osname = g_strdup (admin_opts->osname);
   self->osname_dir = ot_gfile_get_child_build_path (self->ostree_dir, "deploy", osname, NULL);
   if (!deploy_tree (self, deploy_target, revision, &deploy_path,
                     cancellable, error))
diff --git a/src/ostree/ot-admin-builtin-diff.c b/src/ostree/ot-admin-builtin-diff.c
index af2b288..be9486c 100644
--- a/src/ostree/ot-admin-builtin-diff.c
+++ b/src/ostree/ot-admin-builtin-diff.c
@@ -37,7 +37,6 @@ ot_admin_builtin_diff (int argc, char **argv, OtAdminBuiltinOpts *admin_opts, GE
 {
   GOptionContext *context;
   gboolean ret = FALSE;
-  const char *osname;
   GFile *ostree_dir = admin_opts->ostree_dir;
   ot_lobj GFile *repo_path = NULL;
   ot_lobj GFile *deployment = NULL;
@@ -49,7 +48,7 @@ ot_admin_builtin_diff (int argc, char **argv, OtAdminBuiltinOpts *admin_opts, GE
   ot_lobj GFile *new_etc_path = NULL;
   __attribute__((unused)) GCancellable *cancellable = NULL;
 
-  context = g_option_context_new ("OSNAME [REVISION] - Diff configuration for OSNAME");
+  context = g_option_context_new ("[REVISION] - Diff configuration versus default");
 
   g_option_context_add_main_entries (context, options, NULL);
 
@@ -58,17 +57,9 @@ ot_admin_builtin_diff (int argc, char **argv, OtAdminBuiltinOpts *admin_opts, GE
   
   repo_path = g_file_get_child (ostree_dir, "repo");
 
-  if (argc < 2)
+  if (argc > 1)
     {
-      ot_util_usage_error (context, "OSNAME must be specified", error);
-      goto out;
-    }
-
-  osname = argv[1];
-
-  if (argc > 2)
-    {
-      deployment = ot_gfile_get_child_build_path (ostree_dir, "deploy", osname, argv[2], NULL);
+      deployment = ot_gfile_get_child_build_path (ostree_dir, "deploy", admin_opts->osname, argv[2], NULL);
       if (!g_file_query_exists (deployment, NULL))
         {
           g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
@@ -78,7 +69,7 @@ ot_admin_builtin_diff (int argc, char **argv, OtAdminBuiltinOpts *admin_opts, GE
     }
   else
     {
-      if (!ot_admin_get_current_deployment (ostree_dir, osname, &deployment,
+      if (!ot_admin_get_current_deployment (ostree_dir, admin_opts->osname, &deployment,
                                             cancellable, error))
         goto out;
     }
diff --git a/src/ostree/ot-admin-builtin-install.c b/src/ostree/ot-admin-builtin-install.c
index 06249a9..8c0bd58 100644
--- a/src/ostree/ot-admin-builtin-install.c
+++ b/src/ostree/ot-admin-builtin-install.c
@@ -78,6 +78,7 @@ ot_admin_builtin_install (int argc, char **argv, OtAdminBuiltinOpts *admin_opts,
   ot_lfree char *osname = NULL;
   ot_lfree char *repoarg = NULL;
   ot_lfree char *ostree_dir_arg = NULL;
+  ot_lfree char *osname_arg = NULL;
   ot_lfree char *tree_to_deploy = NULL;
   GKeyFile *keyfile = NULL;
   __attribute__((unused)) GCancellable *cancellable = NULL;
@@ -132,11 +133,13 @@ ot_admin_builtin_install (int argc, char **argv, OtAdminBuiltinOpts *admin_opts,
   ostree_dir_arg = g_strconcat ("--ostree-dir=",
                                 gs_file_get_path_cached (ostree_dir),
                                 NULL);
+  osname_arg = g_strconcat ("--osname=", admin_opts->osname, NULL);
 
   if (!gs_subprocess_simple_run_sync (gs_file_get_path_cached (ostree_dir),
                                       GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
                                       cancellable, error,
-                                      "ostree", "admin", ostree_dir_arg, "os-init", osname, NULL))
+                                      "ostree", "admin", ostree_dir_arg, osname_arg,
+                                      "os-init", NULL))
     goto out;
 
   if (treename_arg)
@@ -182,14 +185,15 @@ ot_admin_builtin_install (int argc, char **argv, OtAdminBuiltinOpts *admin_opts,
   if (!gs_subprocess_simple_run_sync (gs_file_get_path_cached (ostree_dir),
                                       GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
                                       cancellable, error,
-                                        "ostree", "pull", repoarg, osname, NULL))
+                                      "ostree", "pull", repoarg, osname, NULL))
     goto out;
 
   if (!gs_subprocess_simple_run_sync (gs_file_get_path_cached (ostree_dir),
                                       GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
                                       cancellable, error,
-                                      "ostree", "admin", ostree_dir_arg, "deploy", osname,
-                                      tree_to_deploy, NULL))
+                                      "ostree", "admin",
+                                      ostree_dir_arg, osname_arg,
+                                      "deploy", tree_to_deploy, NULL))
     goto out;
 
   ret = TRUE;
diff --git a/src/ostree/ot-admin-builtin-os-init.c b/src/ostree/ot-admin-builtin-os-init.c
index 0536167..1d086fa 100644
--- a/src/ostree/ot-admin-builtin-os-init.c
+++ b/src/ostree/ot-admin-builtin-os-init.c
@@ -37,13 +37,12 @@ ot_admin_builtin_os_init (int argc, char **argv, OtAdminBuiltinOpts *admin_opts,
 {
   GOptionContext *context;
   gboolean ret = FALSE;
-  const char *osname = NULL;
   GFile *ostree_dir = admin_opts->ostree_dir;
   ot_lobj GFile *deploy_dir = NULL;
   ot_lobj GFile *dir = NULL;
   __attribute__((unused)) GCancellable *cancellable = NULL;
 
-  context = g_option_context_new ("OSNAME - Initialize empty state for given operating system");
+  context = g_option_context_new ("Initialize empty state for given operating system");
   g_option_context_add_main_entries (context, options, NULL);
 
   if (!g_option_context_parse (context, &argc, &argv, error))
@@ -52,15 +51,7 @@ ot_admin_builtin_os_init (int argc, char **argv, OtAdminBuiltinOpts *admin_opts,
   if (!ot_admin_ensure_initialized (ostree_dir, cancellable, error))
     goto out;
 
-  if (argc < 2)
-    {
-      ot_util_usage_error (context, "OSNAME must be specified", error);
-      goto out;
-    }
-
-  osname = argv[1];
-
-  deploy_dir = ot_gfile_get_child_build_path (ostree_dir, "deploy", osname, NULL);
+  deploy_dir = ot_gfile_get_child_build_path (ostree_dir, "deploy", admin_opts->osname, NULL);
 
   /* Ensure core subdirectories of /var exist, since we need them for
    * dracut generation, and the host will want them too.  Note that at
diff --git a/src/ostree/ot-admin-builtin-prune.c b/src/ostree/ot-admin-builtin-prune.c
index 2a3c42f..42ce252 100644
--- a/src/ostree/ot-admin-builtin-prune.c
+++ b/src/ostree/ot-admin-builtin-prune.c
@@ -103,7 +103,6 @@ ot_admin_builtin_prune (int argc, char **argv, OtAdminBuiltinOpts *admin_opts, G
   GOptionContext *context;
   gboolean ret = FALSE;
   guint i;
-  const char *osname;
   GFile *ostree_dir = admin_opts->ostree_dir;
   ot_lobj GFile *repo_path = NULL;
   ot_lobj GFile *deploy_dir = NULL;
@@ -112,30 +111,22 @@ ot_admin_builtin_prune (int argc, char **argv, OtAdminBuiltinOpts *admin_opts, G
   ot_lptrarray GPtrArray *deployments = NULL;
   __attribute__((unused)) GCancellable *cancellable = NULL;
 
-  context = g_option_context_new ("OSNAME - Delete untagged deployments and repository objects");
+  context = g_option_context_new ("Delete untagged deployments and repository objects");
 
   g_option_context_add_main_entries (context, options, NULL);
 
   if (!g_option_context_parse (context, &argc, &argv, error))
     goto out;
 
-  if (argc < 2)
-    {
-      ot_util_usage_error (context, "OSNAME must be specified", error);
-      goto out;
-    }
-
-  osname = argv[1];
-
-  deploy_dir = ot_gfile_get_child_build_path (ostree_dir, "deploy", osname, NULL);
+  deploy_dir = ot_gfile_get_child_build_path (ostree_dir, "deploy", admin_opts->osname, NULL);
 
   deployments = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
   if (!list_deployments (deploy_dir, deployments, cancellable, error))
     goto out;
 
-  if (!ot_admin_get_current_deployment (ostree_dir, osname, &current_deployment,
+  if (!ot_admin_get_current_deployment (ostree_dir, admin_opts->osname, &current_deployment,
                                         cancellable, error));
-  if (!ot_admin_get_previous_deployment (ostree_dir, osname, &previous_deployment,
+  if (!ot_admin_get_previous_deployment (ostree_dir, admin_opts->osname, &previous_deployment,
                                          cancellable, error));
 
   for (i = 0; i < deployments->len; i++)
diff --git a/src/ostree/ot-admin-builtin-pull-deploy.c b/src/ostree/ot-admin-builtin-pull-deploy.c
index 9e83fdf..a58f07a 100644
--- a/src/ostree/ot-admin-builtin-pull-deploy.c
+++ b/src/ostree/ot-admin-builtin-pull-deploy.c
@@ -108,7 +108,6 @@ ot_admin_builtin_pull_deploy (int argc, char **argv, OtAdminBuiltinOpts *admin_o
 {
   GOptionContext *context;
   gboolean ret = FALSE;
-  const char *osname;
   const char *target;
   GFile *ostree_dir = admin_opts->ostree_dir;
   ot_lobj OstreeRepo *repo = NULL;
@@ -121,21 +120,13 @@ ot_admin_builtin_pull_deploy (int argc, char **argv, OtAdminBuiltinOpts *admin_o
   ot_lptrarray GPtrArray *subproc_args = NULL;
   __attribute__((unused)) GCancellable *cancellable = NULL;
 
-  context = g_option_context_new ("OSNAME - Upgrade and redeploy current tree");
+  context = g_option_context_new ("Upgrade and redeploy current tree");
 
   g_option_context_add_main_entries (context, options, NULL);
 
   if (!g_option_context_parse (context, &argc, &argv, error))
     goto out;
 
-  if (argc < 2)
-    {
-      ot_util_usage_error (context, "OSNAME must be specified", error);
-      goto out;
-    }
-
-  osname = argv[1];
-
   repo_path = g_file_get_child (ostree_dir, "repo");
 
   repo = ostree_repo_new (repo_path);
@@ -143,12 +134,12 @@ ot_admin_builtin_pull_deploy (int argc, char **argv, OtAdminBuiltinOpts *admin_o
     goto out;
 
   deploy_dir = g_file_get_child (ostree_dir, "deploy");
-  os_dir = g_file_get_child (deploy_dir, osname);
+  os_dir = g_file_get_child (deploy_dir, admin_opts->osname);
 
   if (argc > 2)
     {
       target = argv[2];
-      if (!ensure_remote_branch (repo, osname, target,
+      if (!ensure_remote_branch (repo, admin_opts->osname, target,
                                 cancellable, error))
         goto out;
       
@@ -156,7 +147,7 @@ ot_admin_builtin_pull_deploy (int argc, char **argv, OtAdminBuiltinOpts *admin_o
     }
   else
     {
-      if (!ot_admin_get_current_deployment (ostree_dir, osname, &current_deployment,
+      if (!ot_admin_get_current_deployment (ostree_dir, admin_opts->osname, &current_deployment,
                                             cancellable, error))
         goto out;
       
@@ -178,7 +169,7 @@ ot_admin_builtin_pull_deploy (int argc, char **argv, OtAdminBuiltinOpts *admin_o
     if (!gs_subprocess_simple_run_sync (gs_file_get_path_cached (ostree_dir),
                                         GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
                                         cancellable, error,
-                                        "ostree", "pull", repo_arg, osname, NULL))
+                                        "ostree", "pull", repo_arg, admin_opts->osname, NULL))
       goto out;
   }
 
@@ -189,11 +180,13 @@ ot_admin_builtin_pull_deploy (int argc, char **argv, OtAdminBuiltinOpts *admin_o
     ot_lfree char *opt_boot_dir_arg = g_strconcat ("--boot-dir=",
                                                      gs_file_get_path_cached (admin_opts->boot_dir),
                                                      NULL);
+    ot_lfree char *opt_osname_arg = g_strconcat ("--osname=", admin_opts->osname, NULL);
     if (!gs_subprocess_simple_run_sync (gs_file_get_path_cached (ostree_dir),
                                         GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
                                         cancellable, error,
-                                        "ostree", "admin", opt_ostree_dir_arg, opt_boot_dir_arg, "deploy", 
osname,
-                                        deploy_name, NULL))
+                                        "ostree", "admin", opt_ostree_dir_arg,
+                                        opt_boot_dir_arg, opt_osname_arg,
+                                        "deploy", deploy_name, NULL))
       goto out;
   }
 
diff --git a/src/ostree/ot-admin-builtin-update-kernel.c b/src/ostree/ot-admin-builtin-update-kernel.c
index 1d94bbc..e5fcc91 100644
--- a/src/ostree/ot-admin-builtin-update-kernel.c
+++ b/src/ostree/ot-admin-builtin-update-kernel.c
@@ -346,19 +346,13 @@ ot_admin_builtin_update_kernel (int argc, char **argv, OtAdminBuiltinOpts *admin
 
   self->admin_opts = admin_opts;
 
-  context = g_option_context_new ("OSNAME [DEPLOY_PATH] - Update kernel and regenerate initial ramfs");
+  context = g_option_context_new ("[DEPLOY_PATH] - Update kernel and regenerate initial ramfs");
   g_option_context_add_main_entries (context, options, NULL);
 
   if (!g_option_context_parse (context, &argc, &argv, error))
     goto out;
 
-  if (argc < 2)
-    {
-      ot_util_usage_error (context, "OSNAME must be specified", error);
-      goto out;
-    }
-
-  self->osname = g_strdup (argv[1]);
+  self->osname = g_strdup (admin_opts->osname);
 
   if (argc > 2)
     self->deploy_path = g_file_new_for_path (argv[2]);
diff --git a/src/ostree/ot-admin-builtin-upgrade.c b/src/ostree/ot-admin-builtin-upgrade.c
index f8ee8a0..8b7b29b 100644
--- a/src/ostree/ot-admin-builtin-upgrade.c
+++ b/src/ostree/ot-admin-builtin-upgrade.c
@@ -40,38 +40,31 @@ ot_admin_builtin_upgrade (int argc, char **argv, OtAdminBuiltinOpts *admin_opts,
   GOptionContext *context;
   gboolean ret = FALSE;
   GFile *ostree_dir = admin_opts->ostree_dir;
-  const char *osname = NULL;
   gs_free char *ostree_dir_arg = NULL;
+  gs_free char *osname_arg = NULL;
   __attribute__((unused)) GCancellable *cancellable = NULL;
 
-  context = g_option_context_new ("OSNAME - pull, deploy, and prune");
+  context = g_option_context_new ("pull, deploy, and prune");
   g_option_context_add_main_entries (context, options, NULL);
 
   if (!g_option_context_parse (context, &argc, &argv, error))
     goto out;
 
-  if (argc < 2)
-    {
-      ot_util_usage_error (context, "OSNAME must be specified", error);
-      goto out;
-    }
-
-  osname = argv[1];
-
   ostree_dir_arg = g_strconcat ("--ostree-dir=",
                                 gs_file_get_path_cached (ostree_dir),
                                 NULL);
+  osname_arg = g_strconcat ("--osname=", admin_opts->osname, NULL);
 
   if (!gs_subprocess_simple_run_sync (gs_file_get_path_cached (ostree_dir),
                                       GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
                                       cancellable, error,
-                                      "ostree", "admin", ostree_dir_arg, "pull-deploy", osname, NULL))
+                                      "ostree", "admin", ostree_dir_arg, osname_arg, "pull-deploy", NULL))
     goto out;
 
   if (!gs_subprocess_simple_run_sync (gs_file_get_path_cached (ostree_dir),
                                       GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
                                       cancellable, error,
-                                      "ostree", "admin", ostree_dir_arg, "prune", osname, NULL))
+                                      "ostree", "admin", ostree_dir_arg, osname_arg, "prune", NULL))
     goto out;
 
   ret = TRUE;
diff --git a/src/ostree/ot-admin-builtins.h b/src/ostree/ot-admin-builtins.h
index 5fbdacd..cd61594 100644
--- a/src/ostree/ot-admin-builtins.h
+++ b/src/ostree/ot-admin-builtins.h
@@ -30,6 +30,9 @@ G_BEGIN_DECLS
 typedef struct {
   GFile *ostree_dir;
   GFile *boot_dir;
+  char  *osname;
+  char  *booted_osname;
+  char  *booted_tree;
 } OtAdminBuiltinOpts;
 
 gboolean ot_admin_builtin_os_init (int argc, char **argv, OtAdminBuiltinOpts *admin_opts, GError **error);
diff --git a/src/ostree/ot-admin-functions.c b/src/ostree/ot-admin-functions.c
index 6cc4ff0..3b13278 100644
--- a/src/ostree/ot-admin-functions.c
+++ b/src/ostree/ot-admin-functions.c
@@ -182,6 +182,53 @@ ot_admin_get_previous_deployment (GFile           *ostree_dir,
 }
 
 gboolean
+ot_admin_get_booted_os (char   **out_osname,
+                        char   **out_tree,
+                        GError **error)
+{
+  gboolean ret = FALSE;
+  gs_free char *ret_osname = NULL;
+  gs_free char *ret_tree = NULL;
+  gs_free char *cmdline_contents = NULL;
+  const char *iter;
+  gsize len;
+
+  if (!g_file_get_contents ("/proc/cmdline", &cmdline_contents, &len,
+                            error))
+    goto out;
+
+  iter = cmdline_contents;
+  do
+    {
+      const char *next = strchr (iter, ' ');
+      if (next)
+       next += 1;
+      if (g_str_has_prefix (iter, "ostree="))
+        {
+          const char *slash = strchr (iter, '/');
+          if (slash)
+            {
+              const char *start = iter + strlen ("ostree=");
+              ret_osname = g_strndup (start, slash - start);
+              if (next)
+                ret_tree = g_strndup (slash + 1, next - slash - 1);
+              else
+                ret_tree = g_strdup (slash + 1);
+              break;
+            }
+        }
+      iter = next;
+    }
+  while (iter != NULL);
+
+  ret = TRUE;
+ out:
+  ot_transfer_out_value (out_osname, &ret_osname);
+  ot_transfer_out_value (out_tree, &ret_tree);
+  return ret;
+}
+
+gboolean
 ot_admin_get_default_ostree_dir (GFile        **out_ostree_dir,
                                  GCancellable  *cancellable,
                                  GError       **error)
diff --git a/src/ostree/ot-admin-functions.h b/src/ostree/ot-admin-functions.h
index c189b24..7600a93 100644
--- a/src/ostree/ot-admin-functions.h
+++ b/src/ostree/ot-admin-functions.h
@@ -31,6 +31,10 @@ gboolean ot_admin_ensure_initialized (GFile         *ostree_dir,
                                      GCancellable  *cancellable,
                                      GError       **error);
 
+gboolean ot_admin_get_booted_os (char   **out_osname,
+                                 char   **out_tree,
+                                 GError **error);
+
 gboolean ot_admin_get_current_deployment (GFile           *ostree_dir,
                                           const char      *osname,
                                           GFile          **out_deployment,
diff --git a/src/ostree/ot-builtin-admin.c b/src/ostree/ot-builtin-admin.c
index c8f60fb..d8c644a 100644
--- a/src/ostree/ot-builtin-admin.c
+++ b/src/ostree/ot-builtin-admin.c
@@ -33,29 +33,37 @@
 
 static char *opt_ostree_dir = NULL;
 static char *opt_boot_dir = "/boot";
+static char *opt_osname = NULL;
 
 static GOptionEntry options[] = {
   { "ostree-dir", 0, 0, G_OPTION_ARG_STRING, &opt_ostree_dir, "Path to OSTree root directory (default: 
/ostree)", NULL },
   { "boot-dir", 0, 0, G_OPTION_ARG_STRING, &opt_boot_dir, "Path to system boot directory (default: /boot)", 
NULL },
+  { "osname", 0, 0, G_OPTION_ARG_STRING, &opt_osname, "Name of operating system", NULL },
   { NULL }
 };
 
+typedef enum {
+  OSTREE_ADMIN_COMMAND_FLAG_NONE,
+  OSTREE_ADMIN_COMMAND_FLAG_OSNAME_REQUIRED
+} OstreeAdminCommandFlags;
+
 typedef struct {
   const char *name;
   gboolean (*fn) (int argc, char **argv, OtAdminBuiltinOpts *admin_opts, GError **error);
+  OstreeAdminCommandFlags flags;
 } OstreeAdminCommand;
 
 static OstreeAdminCommand admin_subcommands[] = {
-  { "os-init", ot_admin_builtin_os_init },
-  { "init-fs", ot_admin_builtin_init_fs },
-  { "deploy", ot_admin_builtin_deploy },
-  { "install", ot_admin_builtin_install },
-  { "upgrade", ot_admin_builtin_upgrade },
-  { "pull-deploy", ot_admin_builtin_pull_deploy },
-  { "prune", ot_admin_builtin_prune },
-  { "update-kernel", ot_admin_builtin_update_kernel },
-  { "config-diff", ot_admin_builtin_diff },
-  { "run-triggers", ot_admin_builtin_run_triggers },
+  { "os-init", ot_admin_builtin_os_init, OSTREE_ADMIN_COMMAND_FLAG_OSNAME_REQUIRED },
+  { "init-fs", ot_admin_builtin_init_fs, 0 },
+  { "deploy", ot_admin_builtin_deploy, OSTREE_ADMIN_COMMAND_FLAG_OSNAME_REQUIRED },
+  { "install", ot_admin_builtin_install, 0 },
+  { "upgrade", ot_admin_builtin_upgrade, OSTREE_ADMIN_COMMAND_FLAG_OSNAME_REQUIRED },
+  { "pull-deploy", ot_admin_builtin_pull_deploy, OSTREE_ADMIN_COMMAND_FLAG_OSNAME_REQUIRED },
+  { "prune", ot_admin_builtin_prune, OSTREE_ADMIN_COMMAND_FLAG_OSNAME_REQUIRED },
+  { "update-kernel", ot_admin_builtin_update_kernel, OSTREE_ADMIN_COMMAND_FLAG_OSNAME_REQUIRED },
+  { "config-diff", ot_admin_builtin_diff, OSTREE_ADMIN_COMMAND_FLAG_OSNAME_REQUIRED },
+  { "run-triggers", ot_admin_builtin_run_triggers, OSTREE_ADMIN_COMMAND_FLAG_OSNAME_REQUIRED },
   { NULL, NULL }
 };
 
@@ -117,21 +125,36 @@ ostree_builtin_admin (int argc, char **argv, GFile *repo_path, GError **error)
       goto out;
     }
 
+  if (!ot_admin_get_booted_os (&admin_opts.booted_osname, &admin_opts.booted_tree,
+                               error))
+    goto out;
+
   if (opt_ostree_dir != NULL)
-    {
-      ostree_dir = g_file_new_for_path (opt_ostree_dir);
-    }
+    ostree_dir = g_file_new_for_path (opt_ostree_dir);
+  else if (admin_opts.booted_osname != NULL)
+    ostree_dir = g_file_new_for_path ("/sysroot/ostree");
   else
-    {
-      if (!ot_admin_get_default_ostree_dir (&ostree_dir, cancellable, error))
-        goto out;
-    }
+    ostree_dir = g_file_new_for_path ("/ostree");
+  
   boot_dir = g_file_new_for_path (opt_boot_dir);
 
   ostree_prep_builtin_argv (subcommand_name, argc-2, argv+2, &subcmd_argc, &subcmd_argv);
 
   admin_opts.ostree_dir = ostree_dir;
   admin_opts.boot_dir = boot_dir;
+  if (opt_osname != NULL)
+    admin_opts.osname = opt_osname;
+  else
+    {
+      admin_opts.osname = admin_opts.booted_osname;
+      if (admin_opts.osname == NULL &&
+          subcommand->flags & OSTREE_ADMIN_COMMAND_FLAG_OSNAME_REQUIRED)
+        {
+          ot_util_usage_error (context, "--osname must be specified", error);
+          goto out;
+        }
+    }
+  
   if (!subcommand->fn (subcmd_argc, subcmd_argv, &admin_opts, error))
     goto out;
  


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]