aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2012-12-07 14:57:32 +0200
committerSergey Poznyakoff <gray@gnu.org.ua>2012-12-07 15:16:14 +0200
commit5a7b73860974384d8e00065105435403b0842ab0 (patch)
tree3f029c22f0a29a842002279bb4b5561af61a8aa8 /src
parentc12cd5695cf1a6c2c44100a68762ab66356f43b8 (diff)
downloadeclat-5a7b73860974384d8e00065105435403b0842ab0.tar.gz
eclat-5a7b73860974384d8e00065105435403b0842ab0.tar.bz2
Re-implement confirmation support.
* doc/eclat-delete-volume.1: Update. * doc/eclat-release-address.1: Update. * doc/eclat.1: Update. * doc/eclat.conf.5: New section "CONFIRMATION" * lib/getyn.c (eclat_vgetyn): Negative default stands for no default at all. * lib/confirm.c (eclat_confirm_mode): Remove. (eclat_confirm): Change signature. Act according to the first argument. * lib/libeclat.h (eclat_confirm_mode): New enum. (eclat_confirm): Change signature. * src/cmdline.opt: Change handling of -Y and -N options. * src/config.c: New statement "confirm". * src/cretags.c: Remove call to eclat_confirm. This is done by the caller. * src/delvol.c: Likewise. * src/reladdr.c: Likewise. * src/eclat.c (confirm_mode): New variable. (command) <flags>: New member. (cmdtab): Mark commands with appropriate flags. (main): Call eclat_confirm to confirm the command. * src/eclat.h (confirm_mode): New extern. (set_command_confirmation): New proto. * etc/eclat.cfin: Set a reasonably safe confirmation default. * lib/forlan.c (strtots): Remove unused variable.
Diffstat (limited to 'src')
-rw-r--r--src/cmdline.opt4
-rw-r--r--src/config.c74
-rw-r--r--src/cretags.c1
-rw-r--r--src/delvol.c2
-rw-r--r--src/eclat.c66
-rw-r--r--src/eclat.h4
-rw-r--r--src/reladdr.c3
7 files changed, 135 insertions, 19 deletions
diff --git a/src/cmdline.opt b/src/cmdline.opt
index 2d75f24..54ec85f 100644
--- a/src/cmdline.opt
+++ b/src/cmdline.opt
@@ -156,13 +156,13 @@ END
OPTION(yes,Y,,
[<assume `yes' to all questions>])
BEGIN
- eclat_confirm_mode = 1;
+ confirm_mode = eclat_confirm_positive;
END
OPTION(no,N,,
[<assume `no' to all questions>])
BEGIN
- eclat_confirm_mode = 0;
+ confirm_mode = eclat_confirm_negative;
END
GROUP(Identifier translation)
diff --git a/src/config.c b/src/config.c
index f58747c..48bece7 100644
--- a/src/config.c
+++ b/src/config.c
@@ -149,6 +149,76 @@ cb_define_format(enum grecs_callback_command cmd,
return 0;
}
+static int
+cb_confirm(enum grecs_callback_command cmd,
+ grecs_locus_t *locus,
+ void *varptr,
+ grecs_value_t *value,
+ void *cb_data)
+{
+ struct grecs_list_entry *ep;
+ grecs_value_t *argval;
+ enum eclat_confirm_mode cfmode;
+ char *s;
+
+ if (cmd != grecs_callback_set_value) {
+ grecs_error(locus, 0, "Unexpected block statement");
+ return 1;
+ }
+ if (!value || value->type != GRECS_TYPE_ARRAY || value->v.arg.c != 2) {
+ grecs_error(locus, 0, "expected two values");
+ return 1;
+ }
+
+ if (value->v.arg.v[0]->type != GRECS_TYPE_STRING) {
+ grecs_error(locus, 0, "first argument not a string");
+ return 1;
+ }
+
+ switch (value->v.arg.v[1]->type) {
+ case GRECS_TYPE_STRING:
+ set_command_confirmation(value->v.arg.v[1]->v.string, cfmode,
+ &value->v.arg.v[1]->locus);
+ return 0;
+
+ case GRECS_TYPE_LIST:
+ break;
+
+ default:
+ grecs_error(locus, 0, "second argument not a list");
+ return 1;
+ }
+
+ s = value->v.arg.v[0]->v.string;
+ if (strcmp(s, "positive") == 0)
+ cfmode = eclat_confirm_positive;
+ else if (strcmp(s, "negative") == 0)
+ cfmode = eclat_confirm_negative;
+ else if (strcmp(s, "tty") == 0)
+ cfmode = eclat_confirm_tty;
+ else if (strcmp(s, "always") == 0)
+ cfmode = eclat_confirm_always;
+ else {
+ grecs_error(&value->v.arg.v[0]->locus, 0,
+ "unrecognized confirmation mode");
+ return 1;
+ }
+
+ for (ep = value->v.arg.v[1]->v.list->head; ep; ep = ep->next) {
+ argval = ep->data;
+ if (argval->type != GRECS_TYPE_STRING) {
+ grecs_error(&argval->locus, 0,
+ "list element not a string");
+ continue;
+ }
+
+ set_command_confirmation(argval->v.string, cfmode,
+ &argval->locus);
+ }
+
+ return 0;
+}
+
static struct grecs_keyword ssl_kw[] = {
{ "enable", NULL,
"Use SSL",
@@ -218,6 +288,10 @@ static struct grecs_keyword eclat_kw[] = {
grecs_type_string, GRECS_DFLT, &format_file },
{ "map", "name: string", "Configure a map",
grecs_type_section, GRECS_INAC },
+ { "confirm",
+ "<mode: { positive | negative | tty | always }> <commands: list>",
+ "Set confirmation mode",
+ grecs_type_string, GRECS_LIST, NULL, 0, cb_confirm },
{ NULL }
};
diff --git a/src/cretags.c b/src/cretags.c
index f0d3e1f..3554442 100644
--- a/src/cretags.c
+++ b/src/cretags.c
@@ -107,6 +107,7 @@ process_tags(CURL *curl, int argc, char **argv, const char *action)
}
}
free(bufptr);
+
return eclat_send_query(curl, q);
}
diff --git a/src/delvol.c b/src/delvol.c
index 675bd56..5180042 100644
--- a/src/delvol.c
+++ b/src/delvol.c
@@ -32,8 +32,6 @@ eclat_delete_volume(CURL *curl, int argc, char **argv)
if (argc != 1)
die(EX_USAGE, "bad number of arguments");
translate_ids(argc, argv, "VolumeId");
- if (!eclat_confirm("Really delete volume %s", argv[0]))
- exit(EX_CANCELLED);
q = eclat_query_create(use_ssl ? EC2_QF_HTTPS : 0, endpoint, "/");
eclat_query_add_param(q, "Action", "DeleteVolume");
eclat_query_add_param(q, "VolumeId", argv[0]);
diff --git a/src/eclat.c b/src/eclat.c
index 8eb2402..ef9dfd5 100644
--- a/src/eclat.c
+++ b/src/eclat.c
@@ -36,6 +36,7 @@ char *format_name_option;
int sort_option;
char *format_file;
char *test_map_name;
+enum eclat_confirm_mode confirm_mode;
FILE *xml_dump_file;
@@ -197,18 +198,26 @@ node_ident_cmp(struct grecs_node const *a, struct grecs_node const *b)
#include "cmdline.h"
+#define CMD_MOD 0x01
+#define CMD_DESTR 0x02
+
struct command {
const char *ident;
const char *tag;
eclat_command_handler_t handler;
+ int flags;
+ enum eclat_confirm_mode confirm;
char *fmt;
struct grecs_locus locus;
};
struct command cmdtab[] = {
- { "start-instances", "StartInstances", eclat_start_instance },
- { "stop-instances", "StopInstances", eclat_stop_instance },
- { "reboot-instances", "RebootInstances", eclat_reboot_instance },
+ { "start-instances", "StartInstances", eclat_start_instance,
+ CMD_MOD },
+ { "stop-instances", "StopInstances", eclat_stop_instance,
+ CMD_MOD },
+ { "reboot-instances", "RebootInstances", eclat_reboot_instance,
+ CMD_MOD },
{ "describe-addresses", "DescribeAddresses",
eclat_describe_addresses },
{ "describe-tags", "DescribeTags", eclat_describe_tags },
@@ -221,17 +230,17 @@ struct command cmdtab[] = {
{ "describe-volumes", "DescribeVolumes",
eclat_describe_volumes },
{ "allocate-address", "AllocateAddress",
- eclat_allocate_address },
+ eclat_allocate_address, CMD_MOD },
{ "release-address", "ReleaseAddress",
- eclat_release_address },
+ eclat_release_address, CMD_MOD|CMD_DESTR },
{ "associate-address", "AssociateAddress",
- eclat_associate_address },
+ eclat_associate_address, CMD_MOD },
{ "disassociate-address", "DisassociateAddress",
- eclat_disassociate_address },
+ eclat_disassociate_address, CMD_MOD },
{ "create-tags", "CreateTags",
- eclat_create_tags },
+ eclat_create_tags, CMD_MOD },
{ "delete-tags", "DeleteTags",
- eclat_delete_tags },
+ eclat_delete_tags, CMD_MOD|CMD_DESTR },
{ "get-console-output", "GetConsoleOutput",
eclat_get_console_output },
{ "describe-security-groups", "DescribeSecurityGroups",
@@ -241,15 +250,15 @@ struct command cmdtab[] = {
{ "describe-snapshots", "DescribeSnapshots",
eclat_describe_snapshots },
{ "delete-snapshot", "DeleteSnapshot",
- eclat_delete_snapshot },
+ eclat_delete_snapshot, CMD_MOD|CMD_DESTR },
{ "describe-avaialbility-zones", "DescribeAvaialbilityZones",
eclat_describe_avaialbility_zones },
{ "describe-regions", "DescribeRegions",
eclat_describe_regions },
{ "create-volume", "CreateVolume",
- eclat_create_volume },
+ eclat_create_volume, CMD_MOD },
{ "delete-volume", "DeleteVolume",
- eclat_delete_volume },
+ eclat_delete_volume, CMD_MOD|CMD_DESTR },
};
size_t cmdcnt = sizeof(cmdtab) / sizeof(cmdtab[0]);
@@ -362,6 +371,33 @@ find_command_tag(const char *tag)
}
void
+set_command_confirmation(const char *name, enum eclat_confirm_mode cfmode,
+ grecs_locus_t *locus)
+{
+ struct command *cp;
+ int flag = 0;
+
+ if (strcmp(name, "all") == 0)
+ flag = CMD_MOD;
+ else if (strcmp(name, "destructive") == 0)
+ flag = CMD_DESTR;
+ else {
+ cp = find_command_tag(name);
+
+ if (!cp)
+ grecs_error(locus, 0, "unknown command or class");
+ else
+ cp->confirm = cfmode;
+ return;
+ }
+
+ for (cp = cmdtab; cp < cmdtab + cmdcnt; cp++) {
+ if (cp->flags & flag)
+ cp->confirm = cfmode;
+ }
+}
+
+void
set_command_format(const char *name, const char *format, grecs_locus_t *locus)
{
struct command *cp = find_command_tag(name);
@@ -725,6 +761,12 @@ main(int argc, char **argv)
}
}
+ if (confirm_mode == eclat_confirm_unspecified)
+ confirm_mode = command->confirm;
+ if (!eclat_confirm(confirm_mode,
+ "Proceed with %s", command->ident))
+ die(EX_CANCELLED, "command not confirmed");
+
rc = command->handler(curl, argc, argv);
if (rc)
exit(rc);
diff --git a/src/eclat.h b/src/eclat.h
index ceca4d4..40c4014 100644
--- a/src/eclat.h
+++ b/src/eclat.h
@@ -52,6 +52,7 @@ extern char *secret_key;
extern char *format_file;
extern int translate_option;
extern char *custom_map;
+extern enum eclat_confirm_mode confirm_mode;
typedef int (*config_finish_hook_t) (void*);
@@ -130,3 +131,6 @@ forlan_eval_env_t find_format(const char *name);
void generic_parse_options(const char *pname, const char *docstring,
int argc, char *argv[], int *index);
extern struct grecs_proginfo *generic_proginfo;
+
+void set_command_confirmation(const char *name, enum eclat_confirm_mode cfmode,
+ grecs_locus_t *locus);
diff --git a/src/reladdr.c b/src/reladdr.c
index 5dc4ead..a98c0fb 100644
--- a/src/reladdr.c
+++ b/src/reladdr.c
@@ -30,9 +30,6 @@ eclat_release_address(CURL *curl, int argc, char **argv)
if (argc != 1)
die(EX_USAGE, "wrong number of arguments to release-address");
- if (!eclat_confirm("Really release %s %s",
- vpc ? "allocation ID" : "IP", argv[0]))
- exit(EX_CANCELLED);
q = eclat_query_create(use_ssl ? EC2_QF_HTTPS : 0, endpoint, "/");
eclat_query_add_param(q, "Action", "ReleaseAddress");

Return to:

Send suggestions and report system problems to the System administrator.