mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
[enh] Allow to pass the password as argument in the cli
This commit is contained in:
parent
79a10628a4
commit
db924ea911
2 changed files with 15 additions and 3 deletions
|
@ -108,7 +108,8 @@ def api(namespaces, host='localhost', port=80, routes={},
|
|||
logging.getLogger(namespaces[0]).info(m18n.g('operation_interrupted'))
|
||||
return 0
|
||||
|
||||
def cli(namespaces, args, use_cache=True, output_as=None, parser_kwargs={}):
|
||||
def cli(namespaces, args, use_cache=True, output_as=None,
|
||||
password=None, parser_kwargs={}):
|
||||
"""Command line interface
|
||||
|
||||
Execute an action with the moulinette from the CLI and print its
|
||||
|
@ -121,6 +122,7 @@ def cli(namespaces, args, use_cache=True, output_as=None, parser_kwargs={}):
|
|||
instead of using the cached one
|
||||
- output_as -- Output result in another format, see
|
||||
moulinette.interfaces.cli.Interface for possible values
|
||||
- password -- The password to use in case of authentication
|
||||
- parser_kwargs -- A dict of arguments to pass to the parser
|
||||
class at construction
|
||||
|
||||
|
@ -133,7 +135,7 @@ def cli(namespaces, args, use_cache=True, output_as=None, parser_kwargs={}):
|
|||
'parser_kwargs': parser_kwargs,
|
||||
},
|
||||
)
|
||||
moulinette.run(args, output_as=output_as)
|
||||
moulinette.run(args, output_as=output_as, password=password)
|
||||
except MoulinetteError as e:
|
||||
import logging
|
||||
logging.getLogger(namespaces[0]).error(e.strerror)
|
||||
|
|
|
@ -309,7 +309,7 @@ class Interface(BaseInterface):
|
|||
|
||||
self.actionsmap = actionsmap
|
||||
|
||||
def run(self, args, output_as=None):
|
||||
def run(self, args, output_as=None, password=None):
|
||||
"""Run the moulinette
|
||||
|
||||
Process the action corresponding to the given arguments 'args'
|
||||
|
@ -320,6 +320,7 @@ class Interface(BaseInterface):
|
|||
- output_as -- Output result in another format. Possible values:
|
||||
- json: return a JSON encoded string
|
||||
- plain: return a script-readable output
|
||||
- password -- The password to use in case of authentication
|
||||
|
||||
"""
|
||||
if output_as and output_as not in ['json', 'plain']:
|
||||
|
@ -328,6 +329,10 @@ class Interface(BaseInterface):
|
|||
# auto-complete
|
||||
argcomplete.autocomplete(self.actionsmap.parser._parser)
|
||||
|
||||
# Store the given password
|
||||
# FIXME: improve security
|
||||
self._password = password
|
||||
|
||||
try:
|
||||
ret = self.actionsmap.process(args, timeout=5)
|
||||
except KeyboardInterrupt, EOFError:
|
||||
|
@ -358,6 +363,11 @@ class Interface(BaseInterface):
|
|||
Handle the core.MoulinetteSignals.authenticate signal.
|
||||
|
||||
"""
|
||||
# Try to use given password if any
|
||||
if self._password is not None:
|
||||
logger.info('using given password to authenticate')
|
||||
return authenticator(password=self._password)
|
||||
|
||||
# TODO: Allow token authentication?
|
||||
msg = m18n.n(help) if help else m18n.g('password')
|
||||
return authenticator(password=self._do_prompt(msg, True, False,
|
||||
|
|
Loading…
Reference in a new issue