summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/cli
diff options
context:
space:
mode:
authorRuben Pollan <meskio@sindominio.net>2017-02-16 13:44:14 +0100
committerRuben Pollan <meskio@sindominio.net>2017-02-23 00:27:09 +0100
commitff5ec25029db7669163854886be254fccde90e80 (patch)
treecd0098ed9c4d3c840c053f9c794b687fcd3243d5 /src/leap/bitmask/cli
parent59cd23bd3e23bf2b439ad26271733a1b5c8edf68 (diff)
[feat] add json print to cli
To improve scriptability we add '--json' param to the cli. - Resolves: #8771
Diffstat (limited to 'src/leap/bitmask/cli')
-rwxr-xr-xsrc/leap/bitmask/cli/bitmask_cli.py30
-rw-r--r--src/leap/bitmask/cli/command.py7
-rw-r--r--src/leap/bitmask/cli/user.py8
3 files changed, 32 insertions, 13 deletions
diff --git a/src/leap/bitmask/cli/bitmask_cli.py b/src/leap/bitmask/cli/bitmask_cli.py
index 782a52e..67c6d2c 100755
--- a/src/leap/bitmask/cli/bitmask_cli.py
+++ b/src/leap/bitmask/cli/bitmask_cli.py
@@ -58,32 +58,36 @@ GENERAL COMMANDS:
stats show some debug info about bitmask-core
help show this help message
+OPTIONAL ARGUMENTS:
+
+ --json print the raw json (useful for scripting)
+
'''
epilog = ("Use 'bitmaskctl <command> help' to learn more "
"about each command.")
def user(self, raw_args):
- user = User(self.cfg)
+ user = User(self.cfg, self.print_json)
return user.execute(raw_args)
def mail(self, raw_args):
- mail = Mail(self.cfg)
+ mail = Mail(self.cfg, self.print_json)
return mail.execute(raw_args)
def eip(self, raw_args):
- eip = Eip(self.cfg)
+ eip = Eip(self.cfg, self.print_json)
return eip.execute(raw_args)
def keys(self, raw_args):
- keys = Keys(self.cfg)
+ keys = Keys(self.cfg, self.print_json)
return keys.execute(raw_args)
def ui(self, raw_args):
- webui = WebUI(self.cfg)
+ webui = WebUI(self.cfg, self.print_json)
return webui.execute(raw_args)
def logs(self, raw_args):
- logs = Logs(self.cfg)
+ logs = Logs(self.cfg, self.print_json)
return logs.execute(raw_args)
# Single commands
@@ -96,6 +100,7 @@ GENERAL COMMANDS:
if raw_args and ('--verbose' in raw_args or '-v' in raw_args):
cmd += ' --verbose'
commands.getoutput(cmd)
+ self.cfg.set('bonafide', 'active', "")
return defer.succeed(None)
def version(self, raw_args):
@@ -121,6 +126,7 @@ GENERAL COMMANDS:
def stop(self, raw_args):
self.data = ['core', 'stop']
+ self.cfg.set('bonafide', 'active', "")
return self._send(printer=command.default_dict_printer)
def stats(self, raw_args):
@@ -130,7 +136,9 @@ GENERAL COMMANDS:
@defer.inlineCallbacks
def execute():
- cfg = Configuration("bitmaskctl.cfg")
+ cfg = Configuration(".bitmaskctl")
+ print_json = '--json' in sys.argv
+
cli = BitmaskCLI(cfg)
cli.data = ['core', 'version']
args = ['--verbose'] if '--verbose' in sys.argv else None
@@ -139,8 +147,14 @@ def execute():
errb=lambda: cli.start(args))
if 'start' in sys.argv or 'restart' in sys.argv:
command.default_dict_printer({'start': 'ok'})
+
cli.data = []
- yield cli.execute(sys.argv[1:])
+ cli.print_json = print_json
+ args = sys.argv[1:]
+ if print_json:
+ args.remove('--json')
+
+ yield cli.execute(args)
try:
yield reactor.stop()
except:
diff --git a/src/leap/bitmask/cli/command.py b/src/leap/bitmask/cli/command.py
index 95b0fe8..15b864f 100644
--- a/src/leap/bitmask/cli/command.py
+++ b/src/leap/bitmask/cli/command.py
@@ -57,7 +57,7 @@ class Command(object):
"about each command.")
commands = []
- def __init__(self, cfg):
+ def __init__(self, cfg, print_json=False):
self.cfg = cfg
color_init()
@@ -68,6 +68,7 @@ class Command(object):
self.data = []
if self.service:
self.data = [self.service]
+ self.print_json = print_json
def execute(self, raw_args):
self.parser = argparse.ArgumentParser(usage=self.usage,
@@ -112,7 +113,9 @@ class Command(object):
def _check_err(self, stuff, printer):
obj = json.loads(stuff[0])
- if not obj['error']:
+ if self.print_json:
+ print(json.dumps(obj, indent=2))
+ elif not obj['error']:
return printer(obj['result'])
else:
print Fore.RED + 'ERROR:' + '%s' % obj['error'] + Fore.RESET
diff --git a/src/leap/bitmask/cli/user.py b/src/leap/bitmask/cli/user.py
index a5cad1b..1ab3630 100644
--- a/src/leap/bitmask/cli/user.py
+++ b/src/leap/bitmask/cli/user.py
@@ -45,8 +45,8 @@ SUBCOMMANDS:
'''.format(name=command.appname)
- def __init__(self, cfg):
- super(User, self).__init__(cfg)
+ def __init__(self, *args, **kwargs):
+ super(User, self).__init__(*args, **kwargs)
self.data.append('user')
def create(self, raw_args):
@@ -116,7 +116,9 @@ SUBCOMMANDS:
return self._send(printer=command.default_dict_printer)
def active(self, raw_args):
- username = self.cfg.get('bonafide', 'active', default='<none>')
+ username = self.cfg.get('bonafide', 'active', default='')
+ if not username:
+ username = '<none>'
print(Fore.RESET + 'active'.ljust(10) + Fore.GREEN + username +
Fore.RESET)
return defer.succeed(None)