From 7147e1cafed1cf250992ee4693ffdfeb17551573 Mon Sep 17 00:00:00 2001 From: Stavros Sachtouris Date: Tue, 22 Jan 2013 12:42:50 +0200 Subject: [PATCH] store-delete asks for permission In cli.utils: new method ask_user prompts for permission In cli.commands.pithos_cli: store_delete/purge ask for permission they also feature a yes flag argument to avoid prompting --- kamaki/cli/commands/pithos_cli.py | 37 ++++++++++++++++++++++++++----------- kamaki/cli/utils.py | 13 +++++++++++++ 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/kamaki/cli/commands/pithos_cli.py b/kamaki/cli/commands/pithos_cli.py index d95cb86..7f8c575 100644 --- a/kamaki/cli/commands/pithos_cli.py +++ b/kamaki/cli/commands/pithos_cli.py @@ -34,7 +34,12 @@ from kamaki.cli import command from kamaki.cli.command_tree import CommandTree from kamaki.cli.errors import raiseCLIError, CLISyntaxError -from kamaki.cli.utils import format_size, print_dict, pretty_keys, page_hold +from kamaki.cli.utils import ( + format_size, + print_dict, + pretty_keys, + page_hold, + ask_user) from kamaki.cli.argument import FlagArgument, ValueArgument, IntArgument from kamaki.cli.argument import KeyValueArgument, DateArgument from kamaki.cli.argument import ProgressBarArgument @@ -1302,6 +1307,7 @@ class store_delete(_store_container_command): arguments = dict( until=DateArgument('remove history until that date', '--until'), + yes=FlagArgument('Do not prompt for permission', '--yes'), recursive=FlagArgument( 'empty dir or container and delete (if dir)', ('-r', '--recursive')) @@ -1317,16 +1323,19 @@ class store_delete(_store_container_command): def main(self, container____path__): super(self.__class__, self).main(container____path__) try: - if self.path is None: - self.client.del_container( - until=self['until'], - delimiter=self['delimiter']) + if (not self.path): + if self['yes'] or ask_user( + 'Delete container %s ?' % self.container): + self.client.del_container( + until=self['until'], + delimiter=self['delimiter']) else: - # self.client.delete_object(self.path) - self.client.del_object( - self.path, - until=self['until'], - delimiter=self['delimiter']) + if self['yes'] or ask_user( + 'Delete %s:%s ?' % (self.container, self.path)): + self.client.del_object( + self.path, + until=self['until'], + delimiter=self['delimiter']) except ClientError as err: if err.status == 404: if 'container' in ('%s' % err).lower(): @@ -1358,10 +1367,16 @@ class store_purge(_store_container_command): . container and data blocks are released and deleted """ + arguments = dict( + yes=FlagArgument('Do not prompt for permission', '--yes'), + ) + def main(self, container): super(self.__class__, self).main(container) try: - self.client.purge_container() + if self['yes'] or ask_user( + 'Purge container %s?' % self.container): + self.client.purge_container() except ClientError as err: if err.status == 404: if 'container' in ('%s' % err).lower(): diff --git a/kamaki/cli/utils.py b/kamaki/cli/utils.py index bc11f56..9f2dc50 100644 --- a/kamaki/cli/utils.py +++ b/kamaki/cli/utils.py @@ -323,3 +323,16 @@ def split_input(line): terms.append(ipart[2:-2]) terms += _sub_split(trivial_parts[-1]) return terms + + +def ask_user(msg, true_responses=('Y', 'y')): + """Print msg and read user response + + :param true_responses: (tuple of chars) + + :returns: (bool) True if reponse in true responses, False otherwise + """ + stdout.write('%s (%s for yes):' % (msg, true_responses)) + stdout.flush() + user_response = stdin.read(1) + return user_response[0] in true_responses -- 1.7.10.4