Skip to content

Commit

Permalink
ostree: move flags into command struct, pass down through builtins
Browse files Browse the repository at this point in the history
This is a similar approach as
coreos/rpm-ostree@12c34bb.

One thing to note is when we parse the admin related functions,
we still keep the old admin related flags, and added a new parameter
to represent the command struct.

This allows us to identify the caller of the function, making it
easier for us to possibly deduplicate the subcommand handling in
the future. A similar approach is done in rpm-ostree:
coreos/rpm-ostree@83aeb01

This also makes it easier for us to change the prototype of the function.
If we want to add something new in the future, we won't need to touch every prototype.

Closes: #1267
Approved by: cgwalters
  • Loading branch information
peterbaouoft authored and rh-atomic-bot committed Oct 20, 2017
1 parent 3c360a7 commit 298c151
Show file tree
Hide file tree
Showing 58 changed files with 316 additions and 236 deletions.
83 changes: 57 additions & 26 deletions src/ostree/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,62 +32,93 @@
#include "ot-builtins.h"

static OstreeCommand commands[] = {
{ "admin", ostree_builtin_admin ,
/* Note: all admin related commands have
* no_repo as their command flag, but each
* admin command may have their own
* admin flag
*/
{ "admin", OSTREE_BUILTIN_FLAG_NO_REPO,
ostree_builtin_admin,
"Commands that needs admin privilege" },
{ "cat", ostree_builtin_cat,
{ "cat", OSTREE_BUILTIN_FLAG_NONE,
ostree_builtin_cat,
"Concatenate contents of files"},
{ "checkout", ostree_builtin_checkout,
{ "checkout", OSTREE_BUILTIN_FLAG_NONE,
ostree_builtin_checkout,
"Check out a commit into a filesystem tree" },
{ "checksum", ostree_builtin_checksum,
{ "checksum", OSTREE_BUILTIN_FLAG_NO_REPO,
ostree_builtin_checksum,
"Checksum a file or directory" },
{ "commit", ostree_builtin_commit,
{ "commit", OSTREE_BUILTIN_FLAG_NONE,
ostree_builtin_commit,
"Commit a new revision" },
{ "config", ostree_builtin_config,
{ "config", OSTREE_BUILTIN_FLAG_NONE,
ostree_builtin_config,
"Change repo configuration settings" },
{ "diff", ostree_builtin_diff,
{ "diff", OSTREE_BUILTIN_FLAG_NONE,
ostree_builtin_diff,
"Compare directory TARGETDIR against revision REV"},
{ "export", ostree_builtin_export,
{ "export", OSTREE_BUILTIN_FLAG_NONE,
ostree_builtin_export,
"Stream COMMIT to stdout in tar format" },
#ifdef OSTREE_ENABLE_EXPERIMENTAL_API
{ "find-remotes", ostree_builtin_find_remotes,
{ "find-remotes", OSTREE_BUILTIN_FLAG_NONE,
ostree_builtin_find_remotes,
"Find remotes to serve the given refs" },
{ "create-usb", ostree_builtin_create_usb,
{ "create-usb", OSTREE_BUILTIN_FLAG_NONE,
ostree_builtin_create_usb,
"Copy the refs to a USB stick" },
#endif
{ "fsck", ostree_builtin_fsck,
{ "fsck", OSTREE_BUILTIN_FLAG_NONE,
ostree_builtin_fsck,
"Check the repository for consistency" },
{ "gpg-sign", ostree_builtin_gpg_sign,
{ "gpg-sign", OSTREE_BUILTIN_FLAG_NONE,
ostree_builtin_gpg_sign,
"Sign a commit" },
{ "init", ostree_builtin_init,
{ "init", OSTREE_BUILTIN_FLAG_NO_CHECK,
ostree_builtin_init,
"Initialize a new empty repository" },
{ "log", ostree_builtin_log,
{ "log", OSTREE_BUILTIN_FLAG_NONE,
ostree_builtin_log,
"Show log starting at commit or ref" },
{ "ls", ostree_builtin_ls,
{ "ls", OSTREE_BUILTIN_FLAG_NONE,
ostree_builtin_ls,
"List file paths" },
{ "prune", ostree_builtin_prune,
{ "prune", OSTREE_BUILTIN_FLAG_NONE,
ostree_builtin_prune,
"Search for unreachable objects" },
{ "pull-local", ostree_builtin_pull_local,
{ "pull-local", OSTREE_BUILTIN_FLAG_NONE,
ostree_builtin_pull_local,
"Copy data from SRC_REPO" },
#ifdef HAVE_LIBCURL_OR_LIBSOUP
{ "pull", ostree_builtin_pull,
{ "pull", OSTREE_BUILTIN_FLAG_NONE,
ostree_builtin_pull,
"Download data from remote repository" },
#endif
{ "refs", ostree_builtin_refs,
{ "refs", OSTREE_BUILTIN_FLAG_NONE,
ostree_builtin_refs,
"List refs" },
{ "remote", ostree_builtin_remote,
{ "remote", OSTREE_BUILTIN_FLAG_NO_REPO,
ostree_builtin_remote,
"Remote commands that may involve internet access" },
{ "reset", ostree_builtin_reset,
{ "reset", OSTREE_BUILTIN_FLAG_NONE,
ostree_builtin_reset,
"Reset a REF to a previous COMMIT" },
{ "rev-parse", ostree_builtin_rev_parse,
{ "rev-parse", OSTREE_BUILTIN_FLAG_NONE,
ostree_builtin_rev_parse,
"Output the target of a rev" },
{ "show", ostree_builtin_show,
{ "show", OSTREE_BUILTIN_FLAG_NONE,
ostree_builtin_show,
"Output a metadata object" },
{ "static-delta", ostree_builtin_static_delta,
{ "static-delta", OSTREE_BUILTIN_FLAG_NONE,
ostree_builtin_static_delta,
"Static delta related commands" },
{ "summary", ostree_builtin_summary,
{ "summary", OSTREE_BUILTIN_FLAG_NONE,
ostree_builtin_summary,
"Manage summary metadata" },
#if defined(HAVE_LIBSOUP) && defined(BUILDOPT_ENABLE_TRIVIAL_HTTPD_CMDLINE)
{ "trivial-httpd", ostree_builtin_trivial_httpd,
{ "trivial-httpd", OSTREE_BUILTIN_FLAG_NONE,
ostree_builtin_trivial_httpd,
NULL },
#endif
{ NULL }
Expand Down
4 changes: 2 additions & 2 deletions src/ostree/ot-admin-builtin-cleanup.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static GOptionEntry options[] = {
};

gboolean
ot_admin_builtin_cleanup (int argc, char **argv, GCancellable *cancellable, GError **error)
ot_admin_builtin_cleanup (int argc, char **argv, OstreeCommandInvocation *invocation, GCancellable *cancellable, GError **error)
{
g_autoptr(GOptionContext) context = NULL;
g_autoptr(OstreeSysroot) sysroot = NULL;
Expand All @@ -48,7 +48,7 @@ ot_admin_builtin_cleanup (int argc, char **argv, GCancellable *cancellable, GErr

if (!ostree_admin_option_context_parse (context, options, &argc, &argv,
OSTREE_ADMIN_BUILTIN_FLAG_SUPERUSER,
&sysroot, cancellable, error))
invocation, &sysroot, cancellable, error))
goto out;

if (!ostree_sysroot_cleanup (sysroot, cancellable, error))
Expand Down
4 changes: 2 additions & 2 deletions src/ostree/ot-admin-builtin-deploy.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static GOptionEntry options[] = {
};

gboolean
ot_admin_builtin_deploy (int argc, char **argv, GCancellable *cancellable, GError **error)
ot_admin_builtin_deploy (int argc, char **argv, OstreeCommandInvocation *invocation, GCancellable *cancellable, GError **error)
{
__attribute__((cleanup(_ostree_kernel_args_cleanup))) OstreeKernelArgs *kargs = NULL;

Expand All @@ -70,7 +70,7 @@ ot_admin_builtin_deploy (int argc, char **argv, GCancellable *cancellable, GErro
g_autoptr(OstreeSysroot) sysroot = NULL;
if (!ostree_admin_option_context_parse (context, options, &argc, &argv,
OSTREE_ADMIN_BUILTIN_FLAG_SUPERUSER,
&sysroot, cancellable, error))
invocation, &sysroot, cancellable, error))
return FALSE;

if (argc < 2)
Expand Down
4 changes: 2 additions & 2 deletions src/ostree/ot-admin-builtin-diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static GOptionEntry options[] = {
};

gboolean
ot_admin_builtin_diff (int argc, char **argv, GCancellable *cancellable, GError **error)
ot_admin_builtin_diff (int argc, char **argv, OstreeCommandInvocation *invocation, GCancellable *cancellable, GError **error)
{
g_autoptr(GOptionContext) context = NULL;
g_autoptr(OstreeSysroot) sysroot = NULL;
Expand All @@ -60,7 +60,7 @@ ot_admin_builtin_diff (int argc, char **argv, GCancellable *cancellable, GError

if (!ostree_admin_option_context_parse (context, options, &argc, &argv,
OSTREE_ADMIN_BUILTIN_FLAG_SUPERUSER | OSTREE_ADMIN_BUILTIN_FLAG_UNLOCKED,
&sysroot, cancellable, error))
invocation, &sysroot, cancellable, error))
goto out;

if (!ot_admin_require_booted_deployment_or_osname (sysroot, opt_osname,
Expand Down
8 changes: 4 additions & 4 deletions src/ostree/ot-admin-builtin-init-fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ static GOptionEntry options[] = {
};

gboolean
ot_admin_builtin_init_fs (int argc, char **argv, GCancellable *cancellable, GError **error)
ot_admin_builtin_init_fs (int argc, char **argv, OstreeCommandInvocation *invocation, GCancellable *cancellable, GError **error)
{
g_autoptr(GOptionContext) context = g_option_context_new ("PATH - Initialize a root filesystem");

if (!ostree_admin_option_context_parse (context, options, &argc, &argv,
OSTREE_ADMIN_BUILTIN_FLAG_SUPERUSER |
OSTREE_ADMIN_BUILTIN_FLAG_UNLOCKED |
OSTREE_ADMIN_BUILTIN_FLAG_NO_SYSROOT,
NULL, cancellable, error))
OSTREE_ADMIN_BUILTIN_FLAG_UNLOCKED |
OSTREE_ADMIN_BUILTIN_FLAG_NO_SYSROOT,
invocation, NULL, cancellable, error))
return FALSE;

if (argc < 2)
Expand Down
30 changes: 14 additions & 16 deletions src/ostree/ot-admin-builtin-instutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,25 @@

#include <glib/gi18n.h>

typedef struct {
const char *name;
gboolean (*fn) (int argc, char **argv, GCancellable *cancellable, GError **error);
const char *description;
} OstreeAdminInstUtilCommand;

static OstreeAdminInstUtilCommand admin_instutil_subcommands[] = {
static OstreeCommand admin_instutil_subcommands[] = {
#ifdef HAVE_SELINUX
{ "selinux-ensure-labeled", ot_admin_instutil_builtin_selinux_ensure_labeled,
{ "selinux-ensure-labeled", OSTREE_BUILTIN_FLAG_NO_REPO,
ot_admin_instutil_builtin_selinux_ensure_labeled,
"Relabel all or part of a deployment" },
#endif
{ "set-kargs", ot_admin_instutil_builtin_set_kargs,
{ "set-kargs", OSTREE_BUILTIN_FLAG_NO_REPO,
ot_admin_instutil_builtin_set_kargs,
"Set new kernel command line arguments(Not stable)" },
{ "grub2-generate", ot_admin_instutil_builtin_grub2_generate,
{ "grub2-generate", OSTREE_BUILTIN_FLAG_NO_REPO,
ot_admin_instutil_builtin_grub2_generate,
"Generate GRUB2 configuration from given BLS entries" },
{ NULL, NULL, NULL }
{ NULL, 0, NULL, NULL }
};

static GOptionContext *
ostree_admin_instutil_option_context_new_with_commands (void)
{
OstreeAdminInstUtilCommand *command = admin_instutil_subcommands;
OstreeCommand *command = admin_instutil_subcommands;
GOptionContext *context = g_option_context_new ("COMMAND");

g_autoptr(GString) summary = g_string_new ("Builtin \"admin instutil\" Commands:");
Expand All @@ -69,7 +66,7 @@ ostree_admin_instutil_option_context_new_with_commands (void)
}

gboolean
ot_admin_builtin_instutil (int argc, char **argv, GCancellable *cancellable, GError **error)
ot_admin_builtin_instutil (int argc, char **argv, OstreeCommandInvocation *invocation, GCancellable *cancellable, GError **error)
{
const char *subcommand_name = NULL;
int in, out;
Expand Down Expand Up @@ -97,7 +94,7 @@ ot_admin_builtin_instutil (int argc, char **argv, GCancellable *cancellable, GEr

argc = out;

OstreeAdminInstUtilCommand *subcommand = admin_instutil_subcommands;
OstreeCommand *subcommand = admin_instutil_subcommands;
while (subcommand->name)
{
if (g_strcmp0 (subcommand_name, subcommand->name) == 0)
Expand All @@ -113,7 +110,7 @@ ot_admin_builtin_instutil (int argc, char **argv, GCancellable *cancellable, GEr
/* This will not return for some options (e.g. --version). */
if (ostree_admin_option_context_parse (context, NULL, &argc, &argv,
OSTREE_ADMIN_BUILTIN_FLAG_NO_SYSROOT,
NULL, cancellable, error))
invocation, NULL, cancellable, error))
{
if (subcommand_name == NULL)
{
Expand All @@ -135,7 +132,8 @@ ot_admin_builtin_instutil (int argc, char **argv, GCancellable *cancellable, GEr
g_autofree char *prgname = g_strdup_printf ("%s %s", g_get_prgname (), subcommand_name);
g_set_prgname (prgname);

if (!subcommand->fn (argc, argv, cancellable, error))
OstreeCommandInvocation sub_invocation = { .command = subcommand };
if (!subcommand->fn (argc, argv, &sub_invocation, cancellable, error))
return FALSE;

return TRUE;
Expand Down
4 changes: 2 additions & 2 deletions src/ostree/ot-admin-builtin-os-init.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static GOptionEntry options[] = {
};

gboolean
ot_admin_builtin_os_init (int argc, char **argv, GCancellable *cancellable, GError **error)
ot_admin_builtin_os_init (int argc, char **argv, OstreeCommandInvocation *invocation, GCancellable *cancellable, GError **error)
{
g_autoptr(GOptionContext) context = NULL;
g_autoptr(OstreeSysroot) sysroot = NULL;
Expand All @@ -49,7 +49,7 @@ ot_admin_builtin_os_init (int argc, char **argv, GCancellable *cancellable, GErr

if (!ostree_admin_option_context_parse (context, options, &argc, &argv,
OSTREE_ADMIN_BUILTIN_FLAG_SUPERUSER | OSTREE_ADMIN_BUILTIN_FLAG_UNLOCKED,
&sysroot, cancellable, error))
invocation, &sysroot, cancellable, error))
goto out;

if (!ostree_sysroot_ensure_initialized (sysroot, cancellable, error))
Expand Down
4 changes: 2 additions & 2 deletions src/ostree/ot-admin-builtin-set-origin.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static GOptionEntry options[] = {
};

gboolean
ot_admin_builtin_set_origin (int argc, char **argv, GCancellable *cancellable, GError **error)
ot_admin_builtin_set_origin (int argc, char **argv, OstreeCommandInvocation *invocation, GCancellable *cancellable, GError **error)
{
gboolean ret = FALSE;
g_autoptr(GOptionContext) context = NULL;
Expand All @@ -60,7 +60,7 @@ ot_admin_builtin_set_origin (int argc, char **argv, GCancellable *cancellable, G

if (!ostree_admin_option_context_parse (context, options, &argc, &argv,
OSTREE_ADMIN_BUILTIN_FLAG_SUPERUSER,
&sysroot, cancellable, error))
invocation, &sysroot, cancellable, error))
goto out;

if (argc < 3)
Expand Down
4 changes: 2 additions & 2 deletions src/ostree/ot-admin-builtin-status.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ deployment_get_gpg_verify (OstreeDeployment *deployment,
}

gboolean
ot_admin_builtin_status (int argc, char **argv, GCancellable *cancellable, GError **error)
ot_admin_builtin_status (int argc, char **argv, OstreeCommandInvocation *invocation, GCancellable *cancellable, GError **error)
{
g_autoptr(GOptionContext) context = NULL;
g_autoptr(OstreeSysroot) sysroot = NULL;
Expand All @@ -104,7 +104,7 @@ ot_admin_builtin_status (int argc, char **argv, GCancellable *cancellable, GErro

if (!ostree_admin_option_context_parse (context, options, &argc, &argv,
OSTREE_ADMIN_BUILTIN_FLAG_UNLOCKED,
&sysroot, cancellable, error))
invocation, &sysroot, cancellable, error))
goto out;

if (!ostree_sysroot_get_repo (sysroot, &repo, cancellable, error))
Expand Down
4 changes: 2 additions & 2 deletions src/ostree/ot-admin-builtin-switch.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ static GOptionEntry options[] = {
};

gboolean
ot_admin_builtin_switch (int argc, char **argv, GCancellable *cancellable, GError **error)
ot_admin_builtin_switch (int argc, char **argv, OstreeCommandInvocation *invocation, GCancellable *cancellable, GError **error)
{
g_autoptr(GOptionContext) context =
g_option_context_new ("REF - Construct new tree from REF and deploy it");
g_autoptr(OstreeSysroot) sysroot = NULL;
if (!ostree_admin_option_context_parse (context, options, &argc, &argv,
OSTREE_ADMIN_BUILTIN_FLAG_SUPERUSER,
&sysroot, cancellable, error))
invocation, &sysroot, cancellable, error))
return FALSE;

if (argc < 2)
Expand Down
4 changes: 2 additions & 2 deletions src/ostree/ot-admin-builtin-undeploy.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static GOptionEntry options[] = {
};

gboolean
ot_admin_builtin_undeploy (int argc, char **argv, GCancellable *cancellable, GError **error)
ot_admin_builtin_undeploy (int argc, char **argv, OstreeCommandInvocation *invocation, GCancellable *cancellable, GError **error)
{
g_autoptr(GOptionContext) context = NULL;
g_autoptr(OstreeSysroot) sysroot = NULL;
Expand All @@ -50,7 +50,7 @@ ot_admin_builtin_undeploy (int argc, char **argv, GCancellable *cancellable, GEr

if (!ostree_admin_option_context_parse (context, options, &argc, &argv,
OSTREE_ADMIN_BUILTIN_FLAG_SUPERUSER,
&sysroot, cancellable, error))
invocation, &sysroot, cancellable, error))
return FALSE;

if (argc < 2)
Expand Down
4 changes: 2 additions & 2 deletions src/ostree/ot-admin-builtin-unlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static GOptionEntry options[] = {
};

gboolean
ot_admin_builtin_unlock (int argc, char **argv, GCancellable *cancellable, GError **error)
ot_admin_builtin_unlock (int argc, char **argv, OstreeCommandInvocation *invocation, GCancellable *cancellable, GError **error)
{
gboolean ret = FALSE;
g_autoptr(GOptionContext) context = NULL;
Expand All @@ -55,7 +55,7 @@ ot_admin_builtin_unlock (int argc, char **argv, GCancellable *cancellable, GErro

if (!ostree_admin_option_context_parse (context, options, &argc, &argv,
OSTREE_ADMIN_BUILTIN_FLAG_SUPERUSER,
&sysroot, cancellable, error))
invocation, &sysroot, cancellable, error))
goto out;

if (argc > 1)
Expand Down
4 changes: 2 additions & 2 deletions src/ostree/ot-admin-builtin-upgrade.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ static GOptionEntry options[] = {
};

gboolean
ot_admin_builtin_upgrade (int argc, char **argv, GCancellable *cancellable, GError **error)
ot_admin_builtin_upgrade (int argc, char **argv, OstreeCommandInvocation *invocation, GCancellable *cancellable, GError **error)
{
g_autoptr(GOptionContext) context = g_option_context_new ("Construct new tree from current origin and deploy it, if it changed");

g_autoptr(OstreeSysroot) sysroot = NULL;
if (!ostree_admin_option_context_parse (context, options, &argc, &argv,
OSTREE_ADMIN_BUILTIN_FLAG_SUPERUSER,
&sysroot, cancellable, error))
invocation, &sysroot, cancellable, error))
return FALSE;

if (opt_pull_only && opt_deploy_only)
Expand Down
Loading

0 comments on commit 298c151

Please sign in to comment.