[sheepdog] [PATCH 1/2] collie: add self options to collie's command

Yunkai Zhang yunkai.me at gmail.com
Mon Aug 20 16:28:09 CEST 2012


From: Yunkai Zhang <qiushu.zyk at taobao.com>

Now, all collie's command share the same global collie_options, it will
lead to option's name conflict among commands if they use the same options
but with different description.

By introducing self options to each command (if necessary) and make
collie_options only contain the common part of all options, we can solve this
issue.

Signed-off-by: Yunkai Zhang <qiushu.zyk at taobao.com>
---
 collie/cluster.c | 24 ++++++++++++++++++------
 collie/collie.c  | 35 ++++++++++++++++++-----------------
 collie/collie.h  |  1 +
 collie/vdi.c     | 53 ++++++++++++++++++++++++++++++++++++++---------------
 4 files changed, 75 insertions(+), 38 deletions(-)

diff --git a/collie/cluster.c b/collie/cluster.c
index 9302b78..2cca3ec 100644
--- a/collie/cluster.c
+++ b/collie/cluster.c
@@ -16,6 +16,17 @@
 
 #include "collie.h"
 
+static struct sd_option cluster_options[] = {
+	{'b', "store", 1, "specify backend store"},
+	{'c', "copies", 1, "specify the data redundancy (number of copies)"},
+	{'m', "mode", 1, "mode (safe, quorum, unsafe)"},
+	{'f', "force", 0, "do not prompt for confirmation"},
+	{'R', "restore", 1, "restore the cluster"},
+	{'l', "list", 0, "list the user epoch information"},
+
+	{ 0, NULL, 0, NULL },
+};
+
 struct cluster_cmd_data {
 	uint32_t epoch;
 	int list;
@@ -501,19 +512,20 @@ static int cluster_recover(int argc, char **argv)
 
 static struct subcommand cluster_cmd[] = {
 	{"info", NULL, "aprh", "show cluster information",
-	 NULL, SUBCMD_FLAG_NEED_NODELIST, cluster_info},
+	 NULL, SUBCMD_FLAG_NEED_NODELIST, cluster_info, cluster_options},
 	{"format", NULL, "bcmaph", "create a Sheepdog store",
-	 NULL, 0, cluster_format},
+	 NULL, 0, cluster_format, cluster_options},
 	{"shutdown", NULL, "aph", "stop Sheepdog",
-	 NULL, SUBCMD_FLAG_NEED_NODELIST, cluster_shutdown},
+	 NULL, SUBCMD_FLAG_NEED_NODELIST, cluster_shutdown, cluster_options},
 	{"snapshot", NULL, "aRlph", "snapshot/restore the cluster",
-	 NULL, 0, cluster_snapshot},
+	 NULL, 0, cluster_snapshot, cluster_options},
 	{"cleanup", NULL, "aph",
 	 "cleanup the useless snapshot data from recovery",
-	 NULL, 0, cluster_cleanup},
+	 NULL, 0, cluster_cleanup, cluster_options},
 	{"recover", NULL, "afph",
 	 "See 'collie cluster recover' for more information\n",
-	 cluster_recover_cmd, SUBCMD_FLAG_NEED_THIRD_ARG, cluster_recover},
+	 cluster_recover_cmd, SUBCMD_FLAG_NEED_THIRD_ARG,
+	 cluster_recover, cluster_options},
 	{NULL,},
 };
 
diff --git a/collie/collie.c b/collie/collie.c
index fcd44f1..442f3fe 100644
--- a/collie/collie.c
+++ b/collie/collie.c
@@ -25,29 +25,13 @@ int raw_output = 0;
 
 static const struct sd_option collie_options[] = {
 
-	/* common options */
+	/* common options for all collie commands */
 	{'a', "address", 1, "specify the daemon address (default: localhost)"},
 	{'p', "port", 1, "specify the daemon port"},
 	{'r', "raw", 0, "raw output mode: omit headers, separate fields with\n\
                           single spaces and print all sizes in decimal bytes"},
 	{'h', "help", 0, "display this help and exit"},
 
-	/* VDI options */
-	{'P', "prealloc", 0, "preallocate all the data objects"},
-	{'i', "index", 1, "specify the index of data objects"},
-	{'s', "snapshot", 1, "specify a snapshot id or tag name"},
-	{'x', "exclusive", 0, "write in an exclusive mode"},
-	{'d', "delete", 0, "delete a key"},
-	{'F', "force_repair", 0, "force repair object's copies (dangerous)"},
-
-	/* cluster options */
-	{'b', "store", 1, "specify backend store"},
-	{'c', "copies", 1, "specify the data redundancy (number of copies)"},
-	{'m', "mode", 1, "mode (safe, quorum, unsafe)"},
-	{'f', "force", 0, "do not prompt for confirmation"},
-	{'R', "restore", 1, "restore the cluster"},
-	{'l', "list", 0, "list the user epoch information"},
-
 	{ 0, NULL, 0, NULL },
 };
 
@@ -130,15 +114,31 @@ static int (*command_fn)(int, char **);
 static const char *command_options;
 static const char *command_arg;
 static const char *command_desc;
+static struct sd_option *command_self_options;
 
 static const struct sd_option *find_opt(int ch)
 {
 	int i;
+	struct sd_option *opt;
 
+	/* search for common options */
 	for (i = 0; i < ARRAY_SIZE(collie_options); i++) {
 		if (collie_options[i].val == ch)
 			return collie_options + i;
 	}
+
+	/* search for self options */
+	if (!command_self_options)
+		goto out;
+
+	opt = command_self_options;
+	while (opt->val) {
+		if (opt->val == ch)
+			return opt;
+		opt++;
+	}
+
+out:
 	fprintf(stderr, "Internal error\n");
 	exit(EXIT_SYSFAIL);
 }
@@ -249,6 +249,7 @@ static unsigned long setup_commands(const struct command *commands,
 			command_options = s->opts;
 			command_arg = s->arg;
 			command_desc = s->desc;
+			command_self_options = s->self_options;
 			flags = s->flags;
 			break;
 		}
diff --git a/collie/collie.h b/collie/collie.h
index ba2d859..c02fc76 100644
--- a/collie/collie.h
+++ b/collie/collie.h
@@ -49,6 +49,7 @@ struct subcommand {
 	struct subcommand *sub;
 	unsigned long flags;
 	int (*fn)(int, char **);
+	struct sd_option *self_options;
 };
 void subcommand_usage(char *cmd, char *subcmd, int status);
 
diff --git a/collie/vdi.c b/collie/vdi.c
index dbebc23..4d686a5 100644
--- a/collie/vdi.c
+++ b/collie/vdi.c
@@ -16,6 +16,17 @@
 #include "collie.h"
 #include "treeview.h"
 
+static struct sd_option vdi_options[] = {
+	{'P', "prealloc", 0, "preallocate all the data objects"},
+	{'i', "index", 1, "specify the index of data objects"},
+	{'s', "snapshot", 1, "specify a snapshot id or tag name"},
+	{'x', "exclusive", 0, "write in an exclusive mode"},
+	{'d', "delete", 0, "delete a key"},
+	{'F', "force_repair", 0, "force repair object's copies (dangerous)"},
+
+	{ 0, NULL, 0, NULL },
+};
+
 struct vdi_cmd_data {
 	unsigned int index;
 	int snapshot_id;
@@ -1490,35 +1501,47 @@ out:
 
 static struct subcommand vdi_cmd[] = {
 	{"check", "<vdiname>", "Fsaph", "check and repair image's consistency",
-	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_THIRD_ARG, vdi_check},
+	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_THIRD_ARG,
+	 vdi_check, vdi_options},
 	{"create", "<vdiname> <size>", "Paph", "create an image",
-	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_THIRD_ARG, vdi_create},
+	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_THIRD_ARG,
+	 vdi_create, vdi_options},
 	{"snapshot", "<vdiname>", "saph", "create a snapshot",
-	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_THIRD_ARG, vdi_snapshot},
+	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_THIRD_ARG,
+	 vdi_snapshot, vdi_options},
 	{"clone", "<src vdi> <dst vdi>", "sPaph", "clone an image",
-	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_THIRD_ARG, vdi_clone},
+	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_THIRD_ARG,
+	 vdi_clone, vdi_options},
 	{"delete", "<vdiname>", "saph", "delete an image",
-	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_THIRD_ARG, vdi_delete},
+	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_THIRD_ARG,
+	 vdi_delete, vdi_options},
 	{"list", "[vdiname]", "aprh", "list images",
-	 NULL, SUBCMD_FLAG_NEED_NODELIST, vdi_list},
+	 NULL, SUBCMD_FLAG_NEED_NODELIST, vdi_list, vdi_options},
 	{"tree", NULL, "aph", "show images in tree view format",
-	 NULL, SUBCMD_FLAG_NEED_NODELIST, vdi_tree},
+	 NULL, SUBCMD_FLAG_NEED_NODELIST, vdi_tree, vdi_options},
 	{"graph", NULL, "aph", "show images in Graphviz dot format",
-	 NULL, SUBCMD_FLAG_NEED_NODELIST, vdi_graph},
+	 NULL, SUBCMD_FLAG_NEED_NODELIST, vdi_graph, vdi_options},
 	{"object", "<vdiname>", "isaph", "show object information in the image",
-	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_THIRD_ARG, vdi_object},
+	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_THIRD_ARG,
+	 vdi_object, vdi_options},
 	{"track", "<vdiname>", "isaph", "show the object epoch trace in the image",
-	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_THIRD_ARG, vdi_track},
+	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_THIRD_ARG,
+	 vdi_track, vdi_options},
 	{"setattr", "<vdiname> <key> [value]", "dxaph", "set a VDI attribute",
-	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_THIRD_ARG, vdi_setattr},
+	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_THIRD_ARG,
+	 vdi_setattr, vdi_options},
 	{"getattr", "<vdiname> <key>", "aph", "get a VDI attribute",
-	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_THIRD_ARG, vdi_getattr},
+	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_THIRD_ARG,
+	 vdi_getattr, vdi_options},
 	{"resize", "<vdiname> <new size>", "aph", "resize an image",
-	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_THIRD_ARG, vdi_resize},
+	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_THIRD_ARG,
+	 vdi_resize, vdi_options},
 	{"read", "<vdiname> [<offset> [<len>]]", "saph", "read data from an image",
-	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_THIRD_ARG, vdi_read},
+	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_THIRD_ARG,
+	 vdi_read, vdi_options},
 	{"write", "<vdiname> [<offset> [<len>]]", "aph", "write data to an image",
-	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_THIRD_ARG, vdi_write},
+	 NULL, SUBCMD_FLAG_NEED_NODELIST|SUBCMD_FLAG_NEED_THIRD_ARG,
+	 vdi_write, vdi_options},
 	{NULL,},
 };
 
-- 
1.7.11.2




More information about the sheepdog mailing list