Skip to content

Commit

Permalink
Redo cli vaults parameter (fixes #7, fixes #11)
Browse files Browse the repository at this point in the history
  • Loading branch information
lordi committed Oct 8, 2016
1 parent 4960ba4 commit 90e8425
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
35 changes: 25 additions & 10 deletions scripts/syncrypt
Expand Up @@ -38,14 +38,17 @@ class SyncryptCmd():
self.config = None
self.app_config = AppConfig()
self.app_config.read_config_file()
self.vault_dirs = []

def parse(self, args):
self.config = self.parser.parse_args(args)

def setup(self):
setup_logging(self.config.loglevel)
self.loop = asyncio.get_event_loop()
self.app = SyncryptApp(self.app_config, auth_provider=CLIAuthenticationProvider())
self.app = SyncryptApp(self.app_config,
auth_provider=CLIAuthenticationProvider(),
vault_dirs=self.vault_dirs)

def shutdown(self):
self.loop.run_until_complete(self.app.close())
Expand All @@ -57,18 +60,28 @@ class SyncryptCmd():
class SingleVaultCmd(SyncryptCmd):
'''command that supports only a single vault directory'''

def __init__(self):
super(SingleVaultCmd, self).__init__()
# By default, we operate in the current directory
self.vault_dirs = ['.']

def configure_parser(self, parser):
super(SingleVaultCmd, self).configure_parser(parser)
parser.add_argument('-d', metavar='DIRECTORY', type=str,
default='.', dest='directory', help='directory (default: .)')

def setup(self):
self.app_config.add_vault_dir(self.config.directory)
self.vault_dirs = [self.config.directory]
super(SingleVaultCmd, self).setup()

class MultipleVaultCmd(SyncryptCmd):
'''command that supports multiple vault directories'''

def __init__(self):
super(MultipleVaultCmd, self).__init__()
# By default, we operate in the current directory
self.vault_dirs = ['.']

def configure_parser(self, parser):
super(MultipleVaultCmd, self).configure_parser(parser)
parser.add_argument('-d', metavar='DIRECTORY', type=str,
Expand All @@ -77,13 +90,15 @@ class MultipleVaultCmd(SyncryptCmd):
def setup(self):
# For now, each command will work on a default AppConfig object. When
# you want app-level configuration, use ``syncrypt_daemon``.
DIRECTORIES = (['.'] if self.config.directory is None else self.config.directory)
for directory in DIRECTORIES:
self.app_config.add_vault_dir(directory)

if self.config.directory: # not None and not empty
self.vault_dirs = self.config.directory
super(MultipleVaultCmd, self).setup()

class Clone(SyncryptCmd):
class NoVaultCmd(SyncryptCmd):
'''command that operates without a vault'''
pass

class Clone(NoVaultCmd):
command = 'clone'
description = 'clone a remote vault to a local directory'

Expand Down Expand Up @@ -114,14 +129,14 @@ class AddUser(SingleVaultCmd):
def run(self):
self.loop.run_until_complete(self.app.add_user(self.config.email))

class Vaults(SyncryptCmd):
class Vaults(NoVaultCmd):
command = 'vaults'
description = 'list vaults'

def run(self):
self.loop.run_until_complete(self.app.list_vaults())

class Keys(SingleVaultCmd):
class Keys(NoVaultCmd):
command = 'keys'
description = 'list keys'

Expand Down Expand Up @@ -215,7 +230,7 @@ class Log(MultipleVaultCmd):
def run(self):
self.loop.run_until_complete(self.app.print_log(self.config.verbose))

class Login(SyncryptCmd):
class Login(NoVaultCmd):
command = 'login'
description = 'login to server and store auth token'

Expand Down
6 changes: 4 additions & 2 deletions syncrypt/app/syncrypt.py
Expand Up @@ -28,7 +28,7 @@ class SyncryptApp(object):
multiple vaults and report status via a HTTP interface.
'''

def __init__(self, config, auth_provider=None):
def __init__(self, config, auth_provider=None, vault_dirs=None):
self.auth_provider = auth_provider
self.vaults = []

Expand Down Expand Up @@ -57,7 +57,9 @@ def handler(*args, **kwargs):
self.identity.init()

# register vault objects
for vault_dir in self.config.vault_dirs:
if vault_dirs is None:
vault_dirs = self.config.vault_dirs
for vault_dir in vault_dirs:
self.vaults.append(Vault(vault_dir))

super(SyncryptApp, self).__init__()
Expand Down

0 comments on commit 90e8425

Please sign in to comment.