diff options
-rwxr-xr-x | src/leap/bitmask/cli/bitmask_cli.py | 30 | ||||
-rw-r--r-- | src/leap/bitmask/cli/command.py | 7 | ||||
-rw-r--r-- | src/leap/bitmask/cli/user.py | 8 |
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 782a52e5..67c6d2c0 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 95b0fe8d..15b864ff 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 a5cad1b0..1ab36302 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) |