summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKali Kaneko <kali@leap.se>2016-07-06 14:24:30 +0200
committerKali Kaneko <kali@leap.se>2016-07-06 19:05:47 +0200
commit2a39fcaf8cf6d20c2cfac46ddba661ddc9699f39 (patch)
tree6a50af2254f751a9061652458a57302925cfd176
parent44bfc93d60c0060f6153609c86f5deebfd084d2e (diff)
[refactor] beautify cli output
-rwxr-xr-xsrc/leap/bitmask/cli/bitmask_cli.py48
-rw-r--r--src/leap/bitmask/cli/command.py23
-rw-r--r--src/leap/bitmask/cli/eip.py10
-rw-r--r--src/leap/bitmask/cli/keys.py38
-rw-r--r--src/leap/bitmask/cli/mail.py10
-rw-r--r--src/leap/bitmask/cli/user.py21
6 files changed, 90 insertions, 60 deletions
diff --git a/src/leap/bitmask/cli/bitmask_cli.py b/src/leap/bitmask/cli/bitmask_cli.py
index 76b27c94..0aafeb78 100755
--- a/src/leap/bitmask/cli/bitmask_cli.py
+++ b/src/leap/bitmask/cli/bitmask_cli.py
@@ -18,6 +18,7 @@
"""
Bitmask Command Line interface: zmq client.
"""
+import json
import sys
from colorama import Fore
@@ -31,30 +32,30 @@ from leap.bitmask.cli.user import User
class BitmaskCLI(Command):
- usage = '''bitmask_cli <command> [<args>]
+ usage = '''bitmaskctl <command> [<args>]
Controls the Bitmask application.
SERVICE COMMANDS:
- user Handles Bitmask accounts
- mail Bitmask Encrypted Mail
- eip Encrypted Internet Proxy
- keys Bitmask Keymanager
+ user Handles Bitmask accounts
+ mail Bitmask Encrypted Mail
+ eip Encrypted Internet Proxy
+ keys Bitmask Keymanager
GENERAL COMMANDS:
- version prints version number and exit
- launch launch the Bitmask backend daemon
- shutdown shutdown Bitmask backend daemon
- status displays general status about the running Bitmask services
- stats show some debug info about bitmask-core
- help show this help message
+ version prints version number and exit
+ launch launch the Bitmask backend daemon
+ shutdown shutdown Bitmask backend daemon
+ status displays general status about the running Bitmask services
+ stats show some debug info about bitmask-core
+ help show this help message
'''
- epilog = ("Use 'bitmask_cli <command> help' to learn more "
+ epilog = ("Use 'bitmaskctl <command> help' to learn more "
"about each command.")
- commands = ['shutdown', 'status', 'stats']
+ commands = ['shutdown', 'stats']
def user(self, raw_args):
user = User()
@@ -82,9 +83,26 @@ GENERAL COMMANDS:
return defer.succeed(None)
def version(self, raw_args):
- print Fore.GREEN + 'bitmask_cli: 0.0.1' + Fore.RESET
+ print(Fore.GREEN + 'bitmask_cli: ' + Fore.RESET + '0.0.1')
self.data = ['version']
- return self._send()
+ return self._send(printer=self._print_version)
+
+ def _print_version(self, version):
+ corever = version['version_core']
+ print(Fore.GREEN + 'bitmask_core: ' + Fore.RESET + corever)
+
+ def status(self, raw_args):
+ self.data = ['status']
+ return self._send(printer=self._print_status)
+
+ def _print_status(self, status):
+ statusdict = json.loads(status)
+ for key, value in statusdict.items():
+ color = Fore.GREEN
+ if value == 'stopped':
+ color = Fore.RED
+ print(key.ljust(10) + ': ' + color +
+ value + Fore.RESET)
def execute():
diff --git a/src/leap/bitmask/cli/command.py b/src/leap/bitmask/cli/command.py
index ac427c76..36552c03 100644
--- a/src/leap/bitmask/cli/command.py
+++ b/src/leap/bitmask/cli/command.py
@@ -31,15 +31,28 @@ from txzmq import ZmqRequestTimeoutError
from leap.bitmask.core import ENDPOINT
+appname = 'bitmaskctl'
+
+
def _print_result(result):
print Fore.GREEN + '%s' % result + Fore.RESET
+def default_dict_printer(result):
+ for key, value in result.items():
+ if value is None:
+ value = str(value)
+ print(Fore.RESET + key.ljust(10) + Fore.GREEN + value + Fore.RESET)
+
+
class Command(object):
+ """A generic command dispatcher.
+ Any command in the class attribute `commands` will be dispached and
+ represented with a generic printer."""
service = ''
- usage = '''%s %s <subcommand>''' % tuple(sys.argv[:2])
- epilog = ("Use '%s %s <subcommand> --help' to learn more "
- "about each command." % tuple(sys.argv[:2]))
+ usage = '''{name} <subcommand>'''.format(name=appname)
+ epilog = ("Use bitmaskctl <subcommand> --help' to learn more "
+ "about each command.")
commands = []
def __init__(self):
@@ -61,9 +74,11 @@ class Command(object):
except SystemExit:
return defer.succeed(None)
+ # if command is in the default list, send the bare command
+ # and use the default printer
if args.command in self.commands:
self.data += [args.command]
- return self._send()
+ return self._send(printer=default_dict_printer)
elif (args.command == 'execute' or
args.command.startswith('_') or
diff --git a/src/leap/bitmask/cli/eip.py b/src/leap/bitmask/cli/eip.py
index 389b662e..2ce43d81 100644
--- a/src/leap/bitmask/cli/eip.py
+++ b/src/leap/bitmask/cli/eip.py
@@ -17,16 +17,14 @@
"""
Bitmask Command Line interface: eip
"""
-import sys
-
-from leap.bitmask.cli.command import Command
+from leap.bitmask.cli.command import appname, Command
class Eip(Command):
service = 'eip'
- usage = '''%s eip <subcommand>
+ usage = '''{name} eip <subcommand>
-Bitmask encrypted internet service
+Bitmask Encrypted Internet Service
SUBCOMMANDS:
@@ -34,6 +32,6 @@ SUBCOMMANDS:
stop Stop service
status Display status about service
-''' % sys.argv[0]
+'''.format(name=appname)
commands = ['start', 'stop', 'status']
diff --git a/src/leap/bitmask/cli/keys.py b/src/leap/bitmask/cli/keys.py
index b07b4ab8..829dd46f 100644
--- a/src/leap/bitmask/cli/keys.py
+++ b/src/leap/bitmask/cli/keys.py
@@ -22,13 +22,13 @@ import sys
from colorama import Fore
-from leap.bitmask.cli.command import Command
+from leap.bitmask.cli.command import appname, Command
from leap.keymanager.validation import ValidationLevels
class Keys(Command):
service = 'keys'
- usage = '''%s keys <subcommand>
+ usage = '''{name} keys <subcommand>
Bitmask Keymanager management service
@@ -38,8 +38,7 @@ SUBCOMMANDS:
export Export a given key
insert Insert a key to the key storage
delete Delete a key from the key storage
-
-''' % sys.argv[0]
+'''.format(name=appname)
def list(self, raw_args):
parser = argparse.ArgumentParser(
@@ -104,21 +103,22 @@ SUBCOMMANDS:
return self._send()
def _print_key_list(self, keys):
- print Fore.GREEN
for key in keys:
- print key["fingerprint"] + " " + key['address']
- print Fore.RESET
+ print(Fore.GREEN +
+ key["fingerprint"] + " " + key['address'] +
+ Fore.RESET)
def _print_key(self, key):
- print Fore.GREEN
- print "Uids: " + ', '.join(key['uids'])
- print "Fingerprint: " + key['fingerprint']
- print "Length: " + str(key['length'])
- print "Expiration: " + key['expiry_date']
- print "Validation: " + key['validation']
- print("Used: " + "sig:" + str(key['sign_used']) +
- ", encr:" + str(key['encr_used']))
- print "Refresed: " + key['refreshed_at']
- print Fore.RESET
- print ""
- print key['key_data']
+ print(Fore.GREEN)
+ print("Uids: " + ', '.join(key['uids']))
+ print("Fingerprint:" + key['fingerprint'])
+ print("Length: " + str(key['length']))
+ print("Expiration: " + key['expiry_date'])
+ print("Validation: " + key['validation'])
+ print("Used: " + "sig:" +
+ str(key['sign_used']) + ", encr:" +
+ str(key['encr_used']))
+ print("Refreshed: " + key['refreshed_at'])
+ print(Fore.RESET)
+ print("")
+ print(key['key_data'])
diff --git a/src/leap/bitmask/cli/mail.py b/src/leap/bitmask/cli/mail.py
index a02797cc..76fdc3b9 100644
--- a/src/leap/bitmask/cli/mail.py
+++ b/src/leap/bitmask/cli/mail.py
@@ -17,16 +17,14 @@
"""
Bitmask Command Line interface: mail
"""
-import sys
-
-from leap.bitmask.cli.command import Command
+from leap.bitmask.cli.command import appname, Command
class Mail(Command):
service = 'mail'
- usage = '''%s mail <subcommand>
+ usage = '''{name} mail <subcommand>
-Bitmask encrypted internet service
+Bitmask Encrypted Email service
SUBCOMMANDS:
@@ -36,7 +34,7 @@ SUBCOMMANDS:
get-token Returns token for the mail service
get-smtp-certificate Downloads a new smtp certificate
-''' % sys.argv[0]
+'''.format(name=appname)
commands = ['enable', 'disable', 'status', 'get-token',
'get-smtp-certificate']
diff --git a/src/leap/bitmask/cli/user.py b/src/leap/bitmask/cli/user.py
index f2bfc0c0..dccfc7d5 100644
--- a/src/leap/bitmask/cli/user.py
+++ b/src/leap/bitmask/cli/user.py
@@ -21,23 +21,23 @@ import argparse
import getpass
import sys
-from leap.bitmask.cli.command import Command
+from leap.bitmask.cli import command
-class User(Command):
+class User(command.Command):
service = 'user'
- usage = '''%s user <subcommand>
+ usage = '''{name} user <subcommand>
Bitmask account service
SUBCOMMANDS:
- create Register a new user, if possible
- auth Logs in gainst the provider
+ create Registers new user, if possible
+ auth Logs in against the provider
logout Ends any active session with the provider
active Shows the active user, if any
-''' % sys.argv[0]
+'''.format(name=command.appname)
commands = ['active']
@@ -45,23 +45,24 @@ SUBCOMMANDS:
username = self.username(raw_args)
passwd = getpass.getpass()
self.data += ['signup', username, passwd]
- return self._send()
+ return self._send(printer=command.default_dict_printer)
def auth(self, raw_args):
username = self.username(raw_args)
passwd = getpass.getpass()
self.data += ['authenticate', username, passwd]
- return self._send()
+ return self._send(printer=command.default_dict_printer)
def logout(self, raw_args):
username = self.username(raw_args)
self.data += ['logout', username]
- return self._send()
+ return self._send(printer=command.default_dict_printer)
def username(self, raw_args):
+ args = tuple([command.appname] + sys.argv[1:3])
parser = argparse.ArgumentParser(
description='Bitmask user',
- prog='%s %s %s' % tuple(sys.argv[:3]))
+ prog='%s %s %s' % args)
parser.add_argument('username', nargs=1,
help='username ID, in the form <user@example.org>')
subargs = parser.parse_args(raw_args)