diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2013-04-20 22:11:19 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2013-04-20 22:58:21 +0300 |
commit | 59849e3be117458f687e5e540dbc870aa4dfd920 (patch) | |
tree | e18dc445beb1a3c1162bdbed3c10dfccdaf3f8b8 /src | |
parent | 0320e20133d5f606dc65dac164b0e3731bca9b46 (diff) | |
download | eclat-59849e3be117458f687e5e540dbc870aa4dfd920.tar.gz eclat-59849e3be117458f687e5e540dbc870aa4dfd920.tar.bz2 |
Implement new command - lsattr. Improve docs.
The lsattr command combines the functionality of lsiattr and lssattr.
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/cmdline.opt | 14 | ||||
-rw-r--r-- | src/eclat.c | 89 | ||||
-rw-r--r-- | src/eclat.h | 2 | ||||
-rw-r--r-- | src/lsattr-cl.opt | 44 | ||||
-rw-r--r-- | src/lsattr.c | 55 |
6 files changed, 166 insertions, 40 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index ff13ecf..ed0a5b9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -32,6 +32,7 @@ eclat_SOURCES=\ genericcl.c\ getconout.c\ lsaddr.c\ + lsattr.c\ lsiattr.c\ lsimg.c\ lsinst.c\ @@ -70,6 +71,7 @@ OPTFILES=\ freeaddr-cl.opt\ generic-cl.opt\ lsaddr-cl.opt\ + lsattr-cl.opt\ lsimg-cl.opt\ lsistat-cl.opt\ lssg-cl.opt\ diff --git a/src/cmdline.opt b/src/cmdline.opt index 05e3e8a..cd8c280 100644 --- a/src/cmdline.opt +++ b/src/cmdline.opt @@ -81,6 +81,13 @@ BEGIN match_command_mode = 1; END +OPTION(list-commands,l,FMT, + [<list commands using the supplied format>]) +BEGIN + listcmd(optarg); + exit(0); +END + GROUP(Modifiers) OPTION(config-file,c,FILE, @@ -187,13 +194,6 @@ BEGIN translate_option = 1; END -OPTION(list-commands,l,FMT, - [<list commands using the supplied format>]) -BEGIN - listcmd(optarg); - exit(0); -END - GROUP(Preprocessor control) OPTION(include-directory,I,DIR, diff --git a/src/eclat.c b/src/eclat.c index 13b8e74..30f0b70 100644 --- a/src/eclat.c +++ b/src/eclat.c @@ -271,7 +271,9 @@ struct eclat_command cmdtab[] = { { "mkimg", "create-image", "CreateImage", eclat_create_image, CMD_MOD }, { "deimg", "deregister-image", "DeregisterImage", - eclat_deregister_image, CMD_MOD|CMD_DESTR } + eclat_deregister_image, CMD_MOD|CMD_DESTR }, + { "lsattr", NULL, NULL, + eclat_lsattr, CMD_NOQRY }, }; size_t cmdcnt = sizeof(cmdtab) / sizeof(cmdtab[0]); @@ -280,7 +282,15 @@ cmdcmp(const void *a, const void *b) { struct eclat_command const *cmda = a; struct eclat_command const *cmdb = b; - return strcmp(cmda->ident, cmdb->ident); + if (cmda->name && cmdb->name) + return strcmp(cmda->name, cmdb->name); + else if (cmda->ident && cmdb->ident) + return strcmp(cmda->ident, cmdb->ident); + else if (cmda->ident || cmda->name) + return 127; + else if (cmdb->ident || cmdb->name) + return -127; + return 0; } static void @@ -370,11 +380,13 @@ listcmd(char *fmt) continue; } - if (p != start) - fmtstr(start, p - start); - fmtstr(s[0], l[0]); - printf("%s", str); - fmtstr(s[1], l[1]); + if (str) { + if (p != start) + fmtstr(start, p - start); + fmtstr(s[0], l[0]); + printf("%s", str); + fmtstr(s[1], l[1]); + } start = p = end; } else { p++; @@ -393,11 +405,12 @@ listcmdhook() printf("Available commands\n"); printf("Eclat name EC2 Name\n"); printf("-----------+---------\n"); - for (cp = cmdtab; cp < cmdtab + cmdcnt; cp++) - if (cp->name) - printf(" %-10s %s\n", cp->name, cp->ident); - else - printf(" %10s %s\n", "", cp->ident); + for (cp = cmdtab; cp < cmdtab + cmdcnt; cp++) { + printf(" %-10s", cp->name ? cp->name : ""); + if (cp->ident) + printf (" %s", cp->ident); + putchar ('\n'); + } printf("\nRun \"%s COMMAND --help\" to get help on a particular command.\n", program_name); @@ -439,8 +452,10 @@ find_command_name(const char *name) struct eclat_command *cp, *match = NULL; for (cp = cmdtab; cp < cmdtab + cmdcnt; cp++) { - if (strcmp(cp->name, name) == 0) + if (cp->name && strcmp(cp->name, name) == 0) return cp; + if (!cp->ident) + continue; switch (ident_matches(cp->ident, name)) { case NO_MATCH: break; @@ -472,7 +487,8 @@ print_matching_commands(const char *pat) size_t patlen = strlen (pat); for (cp = cmdtab; cp < cmdtab + cmdcnt; cp++) { - if (cp->name && strlen (cp->name) >= patlen && memcmp (cp->name, pat, patlen) == 0) + if (cp->name && strlen (cp->name) >= patlen && + memcmp (cp->name, pat, patlen) == 0) printf("%s\n", cp->name); if (ident_matches(cp->ident, pat) != NO_MATCH) printf("%s\n", cp->ident); @@ -701,7 +717,7 @@ read_format(struct eclat_command *cmd) char *filename; kwe[0] = "command"; - kwe[1] = cmd->ident; + kwe[1] = cmd->ident ? cmd->ident : cmd->name; kwe[2] = "action"; kwe[3] = cmd->tag; kwe[4] = NULL; @@ -735,7 +751,29 @@ read_format(struct eclat_command *cmd) exit(EX_UNAVAILABLE); return env; } - + +int +eclat_do_command(eclat_command_env_t *env, struct eclat_command *command, + int argc, char **argv) +{ + int rc; + + if (!(command->flags & CMD_NOQRY)) { + env->query = eclat_query_create(use_ssl ? EC2_QF_HTTPS : 0, + endpoint, "/"); + eclat_query_add_param(env->query, "Action", command->tag); + } + + rc = command->handler(env, argc, argv); + + if (rc == 0 && !(command->flags & CMD_NOQRY)) { + if (!eclat_confirm(confirm_mode, + "Proceed with %s", command->ident)) + die(EX_CANCELLED, "command not confirmed"); + rc = eclat_send_query(env->curl, env->query); + } + return rc; +} int main(int argc, char **argv) @@ -888,25 +926,10 @@ main(int argc, char **argv) memset(&cmdenv, 0, sizeof(cmdenv)); cmdenv.cmd = command; cmdenv.curl = curl; - - if (!(command->flags & CMD_NOQRY)) { - cmdenv.query = eclat_query_create(use_ssl ? EC2_QF_HTTPS : 0, - endpoint, "/"); - eclat_query_add_param(cmdenv.query, "Action", command->tag); - } - - rc = command->handler(&cmdenv, argc, argv); + + rc = eclat_do_command(&cmdenv, command, argc, argv); if (rc) exit(rc); - - if (!(command->flags & CMD_NOQRY)) { - if (!eclat_confirm(confirm_mode, - "Proceed with %s", command->ident)) - die(EX_CANCELLED, "command not confirmed"); - rc = eclat_send_query(cmdenv.curl, cmdenv.query); - if (rc) - exit(rc); - } curl_easy_cleanup(curl); XML_Parse(parser, "", 0, 1); diff --git a/src/eclat.h b/src/eclat.h index 1848cd0..0ffd127 100644 --- a/src/eclat.h +++ b/src/eclat.h @@ -124,6 +124,8 @@ int eclat_describe_images(eclat_command_env_t *env, int argc, char **argv); int eclat_create_image(eclat_command_env_t *env, int argc, char **argv); int eclat_deregister_image(eclat_command_env_t *env, int argc, char **argv); +int eclat_lsattr(eclat_command_env_t *env, int argc, char **argv); + char *region_to_endpoint(const char *region); void define_format(const char *name, const char *format, grecs_locus_t *locus); diff --git a/src/lsattr-cl.opt b/src/lsattr-cl.opt new file mode 100644 index 0000000..3a51377 --- /dev/null +++ b/src/lsattr-cl.opt @@ -0,0 +1,44 @@ +/* This file is part of Eclat. + Copyright (C) 2012, 2013 Sergey Poznyakoff. + + Eclat is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + Eclat is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Eclat. If not, see <http://www.gnu.org/licenses/>. */ + +OPTIONS_BEGIN("eclat lsattr", + [<List EC2 resource attributes>], + [<ID [ATTR]>], + [<gnu>], + [<nousage>], + [<noversion>]) + +OPTION(instance,i,, + [<instance ID>]) +BEGIN + cmdname = "lsiattr"; + idname = "InstanceId"; +END + +OPTION(snapshot,s,, + [<snapshot ID>]) +BEGIN + cmdname = "lssattr"; + idname = "SnapshotId"; +END + +OPTIONS_END + +static void +parse_options(int argc, char *argv[], int *index) +{ + GETOPT(argc, argv, *index, exit(EX_USAGE)) +} diff --git a/src/lsattr.c b/src/lsattr.c new file mode 100644 index 0000000..706be6b --- /dev/null +++ b/src/lsattr.c @@ -0,0 +1,55 @@ +/* This file is part of Eclat. + Copyright (C) 2012, 2013 Sergey Poznyakoff. + + Eclat is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + Eclat is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Eclat. If not, see <http://www.gnu.org/licenses/>. */ + +#include "eclat.h" +char *idname; +char *cmdname; +#include "lsattr-cl.h" + +int +eclat_lsattr(eclat_command_env_t *env, int argc, char **argv) +{ + int i; + struct eclat_command *command; + + parse_options(argc, argv, &i); + + argc -= i; + argv += i; + + if (argc < 1) + die(EX_USAGE, "wrong number of arguments"); + + if (cmdname) { + translate_ids(1, argv, idname); + } else { + if (strncmp (argv[0], "i-", 2) == 0) + cmdname = "lsiattr"; + else if (strncmp (argv[0], "snap-", 5) == 0) + cmdname = "lssattr"; + else + die(EX_USAGE, "unrecognized resource type: %s", argv[0]); + } + + command = find_command_name(cmdname); + if (!command) + abort(); + translate_option = 0; + argv--; + argc++; + argv[0] = "lsattr"; + return eclat_do_command(env, command, argc, argv); +} |