Skip to content

Commit

Permalink
Merge pull request #109 from level12/108-protect-destructive-cli
Browse files Browse the repository at this point in the history
Protect Destructive CLI Commands with Prompts
  • Loading branch information
guruofgentoo committed Oct 26, 2022
2 parents 4337461 + 7bd232e commit 0a071d1
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 39 deletions.
18 changes: 14 additions & 4 deletions keg/cli.py
Expand Up @@ -188,8 +188,14 @@ def database_group(ctx):
@click.command('init', short_help=_('Create all db objects, send related events.'))
@click.option('--clear-first', default=False, is_flag=True,
help=_('Clear DB of all data and drop all objects before init.'))
@click.option('--yes', default=False, is_flag=True, help="Force confirmation")
@flask.cli.with_appcontext
def database_init(clear_first):
def database_init(clear_first, yes):
prompt = clear_first and not yes
if prompt and not click.confirm(_('Are you sure? You will delete all the data!')):
click.echo('Database untouched')
return

if clear_first:
current_app.db_manager.db_init_with_clear()
click.echo(_('Database cleared and initialized'))
Expand All @@ -199,10 +205,14 @@ def database_init(clear_first):


@click.command('clear', short_help=_('Clear DB of all data and drop all objects.'))
@click.option('--yes', default=False, is_flag=True, help="Force confirmation")
@flask.cli.with_appcontext
def database_clear():
current_app.db_manager.db_clear()
click.echo(_('Database cleared'))
def database_clear(yes):
if yes or click.confirm(_('Are you sure? You will delete all the data!')):
current_app.db_manager.db_clear()
click.echo(_('Database cleared'))
else:
click.echo(_('Database untouched'))


class CLILoader(object):
Expand Down
Binary file modified keg/i18n/es/LC_MESSAGES/keg.mo
Binary file not shown.
38 changes: 23 additions & 15 deletions keg/i18n/es/LC_MESSAGES/keg.po
Expand Up @@ -7,16 +7,16 @@ msgid ""
msgstr ""
"Project-Id-Version: Keg 0.6.5\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2021-06-09 16:34-0400\n"
"POT-Creation-Date: 2022-10-25 09:50-0700\n"
"PO-Revision-Date: 2018-08-16 18:25-0400\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: es\n"
"Language-Team: es <LL@li.org>\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.9.1\n"
"Generated-By: Babel 2.10.3\n"

#: keg/app.py:53
msgid ""
Expand Down Expand Up @@ -102,39 +102,47 @@ msgstr "Crea todos los objetos db, envía eventos relacionados."
msgid "Clear DB of all data and drop all objects before init."
msgstr "Borrar DB de todos los datos y soltar todos los objetos antes de init."

#: keg/cli.py:195
#: keg/cli.py:195 keg/cli.py:211
msgid "Are you sure? You will delete all the data!"
msgstr "¿Está seguro? ¡Borrarás todos los datos!"

#: keg/cli.py:201
msgid "Database cleared and initialized"
msgstr "Base de datos borrada e inicializada"

#: keg/cli.py:198
#: keg/cli.py:204
msgid "Database initialized"
msgstr "Base de datos inicializada"

#: keg/cli.py:201
#: keg/cli.py:207
msgid "Clear DB of all data and drop all objects."
msgstr "Borre la DB de todos los datos y suelte todos los objetos."

#: keg/cli.py:205
#: keg/cli.py:213
msgid "Database cleared"
msgstr "Base de datos limpia"

#: keg/cli.py:274
#: keg/cli.py:215
msgid "Database untouched"
msgstr "Base de datos intacta"

#: keg/cli.py:284
msgid "Name of the configuration profile to use."
msgstr "Nombre del perfil de configuración a usar."

#: keg/cli.py:277
#: keg/cli.py:287
msgid "Set default logging level to logging.WARNING."
msgstr "Establecer el nivel de registro predeterminado en logging.WARNING."

#: keg/cli.py:280
#: keg/cli.py:290
msgid "Show all commands with subcommands."
msgstr "Mostrar todos los comandos con subcomandos."

#: keg/cli.py:309
#: keg/cli.py:319
msgid "Show help message."
msgstr "Mostrar mensaje de ayuda."

#: keg/cli.py:311
#: keg/cli.py:321
msgid "Show this message and exit."
msgstr "Mostrar este mensaje y salir."

Expand All @@ -146,7 +154,7 @@ msgstr "Usando la dirección syslog: {address}"
msgid "did not find syslog address"
msgstr "no encontró la dirección de syslog"

#: keg/templating.py:43
#: keg/templating.py:57
msgid "asset_include does not yet support parameters"
msgstr "asset_include aún no admite parámetros"

Expand All @@ -162,7 +170,7 @@ msgstr ""
msgid "can't set attribute"
msgstr "no se puede establecer el atributo"

#: keg/web.py:73
#: keg/web.py:77
msgid ""
"Argument mismatch occured: method={method}, missing={missing}, "
"extra_keys={extra_keys}, extra_pos={extra_pos}. Arguments available: "
Expand All @@ -172,7 +180,7 @@ msgstr ""
"{missing}, extra_keys = {extra_keys}, extra_pos = {extra_pos}. Argumentos"
" disponibles: {calling_args}"

#: keg/web.py:136
#: keg/web.py:152
msgid "Unimplemented method {name}"
msgstr "Método sin implementar {nombre}"

Expand Down
42 changes: 25 additions & 17 deletions keg/i18n/keg.pot
@@ -1,21 +1,21 @@
# Translations template for Keg.
# Copyright (C) 2021 ORGANIZATION
# Copyright (C) 2022 ORGANIZATION
# This file is distributed under the same license as the Keg project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2021.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2022.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: Keg 0.9.0\n"
"Project-Id-Version: Keg 0.9.1\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2021-06-09 16:34-0400\n"
"POT-Creation-Date: 2022-10-25 09:50-0700\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.9.1\n"
"Generated-By: Babel 2.10.3\n"

#: keg/app.py:53
msgid ""
Expand Down Expand Up @@ -91,39 +91,47 @@ msgstr ""
msgid "Clear DB of all data and drop all objects before init."
msgstr ""

#: keg/cli.py:195
#: keg/cli.py:195 keg/cli.py:211
msgid "Are you sure? You will delete all the data!"
msgstr ""

#: keg/cli.py:201
msgid "Database cleared and initialized"
msgstr ""

#: keg/cli.py:198
#: keg/cli.py:204
msgid "Database initialized"
msgstr ""

#: keg/cli.py:201
#: keg/cli.py:207
msgid "Clear DB of all data and drop all objects."
msgstr ""

#: keg/cli.py:205
#: keg/cli.py:213
msgid "Database cleared"
msgstr ""

#: keg/cli.py:274
#: keg/cli.py:215
msgid "Database untouched"
msgstr ""

#: keg/cli.py:284
msgid "Name of the configuration profile to use."
msgstr ""

#: keg/cli.py:277
#: keg/cli.py:287
msgid "Set default logging level to logging.WARNING."
msgstr ""

#: keg/cli.py:280
#: keg/cli.py:290
msgid "Show all commands with subcommands."
msgstr ""

#: keg/cli.py:309
#: keg/cli.py:319
msgid "Show help message."
msgstr ""

#: keg/cli.py:311
#: keg/cli.py:321
msgid "Show this message and exit."
msgstr ""

Expand All @@ -135,7 +143,7 @@ msgstr ""
msgid "did not find syslog address"
msgstr ""

#: keg/templating.py:43
#: keg/templating.py:57
msgid "asset_include does not yet support parameters"
msgstr ""

Expand All @@ -149,14 +157,14 @@ msgstr ""
msgid "can't set attribute"
msgstr ""

#: keg/web.py:73
#: keg/web.py:77
msgid ""
"Argument mismatch occured: method={method}, missing={missing}, "
"extra_keys={extra_keys}, extra_pos={extra_pos}. Arguments available: "
"{calling_args}"
msgstr ""

#: keg/web.py:136
#: keg/web.py:152
msgid "Unimplemented method {name}"
msgstr ""

Expand Down
4 changes: 3 additions & 1 deletion keg/static/i18n/es/LC_MESSAGES/keg.json
@@ -1,9 +1,10 @@
{
"": {
"language": "es",
"plural-forms": "nplurals=2; plural=(n != 1)"
"plural-forms": "nplurals=2; plural=(n != 1);"
},
"A file with the same name as the desired directory, '{dpath}', already exists.": "Ya existe un archivo con el mismo nombre que el directorio deseado, '{dpath}'.",
"Are you sure? You will delete all the data!": "\u00bfEst\u00e1 seguro? \u00a1Borrar\u00e1s todos los datos!",
"Argument mismatch occured: method={method}, missing={missing}, extra_keys={extra_keys}, extra_pos={extra_pos}. Arguments available: {calling_args}": "La discrepancia en el argumento ocurri\u00f3: method = {method}, missing = {missing}, extra_keys = {extra_keys}, extra_pos = {extra_pos}. Argumentos disponibles: {calling_args}",
"Clear DB of all data and drop all objects before init.": "Borrar DB de todos los datos y soltar todos los objetos antes de init.",
"Clear DB of all data and drop all objects.": "Borre la DB de todos los datos y suelte todos los objetos.",
Expand All @@ -16,6 +17,7 @@
"Database cleared and initialized": "Base de datos borrada e inicializada",
"Database initialized": "Base de datos inicializada",
"Database not enabled for this app. No subcommands available.": "La base de datos no est\u00e1 habilitada para esta aplicaci\u00f3n. No hay subcomandos disponibles.",
"Database untouched": "Base de datos intacta",
"Default config objects:": "Objetos de configuraci\u00f3n predeterminados:",
"Deliberate exception for testing purposes": "Excepci\u00f3n deliberada para fines de prueba",
"Developer info and utils.": "Informaci\u00f3n y utilidades del desarrollador.",
Expand Down
4 changes: 2 additions & 2 deletions keg/tests/test_cli.py
Expand Up @@ -173,12 +173,12 @@ def test_init(self, m_db_init):

@mock.patch('keg.db.DatabaseManager.db_init_with_clear')
def test_init_with_clear(self, m_db_init_wc):
result = self.invoke('init', '--clear-first')
result = self.invoke('init', '--clear-first', '--yes')
assert 'Database cleared and initialized' in result.output
m_db_init_wc.assert_called_once_with()

@mock.patch('keg.db.DatabaseManager.db_clear')
def test_clear(self, m_db_clear):
result = self.invoke('clear')
result = self.invoke('clear', '--yes')
assert 'Database cleared' in result.output
m_db_clear.assert_called_once_with()

0 comments on commit 0a071d1

Please sign in to comment.