Skip to content

Commit

Permalink
Merge pull request #88 from MichaelAquilina/copy_settings
Browse files Browse the repository at this point in the history
Add --copy-target-credentials
  • Loading branch information
MichaelAquilina committed Oct 30, 2017
2 parents e554cde + 80e82c8 commit 3b7e429
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 10 deletions.
26 changes: 21 additions & 5 deletions s4/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,12 @@ def main(arguments):
daemon_parser.add_argument('--read-delay', default=1000, type=int)
daemon_parser.add_argument('--conflicts', default='ignore', choices=['1', '2', 'ignore'])

subparsers.add_parser('add', help="Add a new Target to synchronise")
add_parser = subparsers.add_parser('add', help="Add a new Target to synchronise")
add_parser.add_argument(
'--copy-target-credentials',
'-C',
help="Copy credentials from an existing target instead of typing them in again"
)

sync_parser = subparsers.add_parser('sync', help="Synchronise Targets with S3")
sync_parser.add_argument('targets', nargs='*')
Expand Down Expand Up @@ -235,7 +240,7 @@ def main(arguments):

def get_config():
if not os.path.exists(CONFIG_FILE_PATH):
return {'targets': []}
return {'targets': {}}

with open(CONFIG_FILE_PATH, 'r') as fp:
config = json.load(fp)
Expand Down Expand Up @@ -454,14 +459,25 @@ def targets_command(args, config, logger):


def add_command(args, config, logger):
entry = {}
target = args.copy_target_credentials
all_targets = list(config['targets'].keys())
if target is not None and target not in all_targets:
logger.info('"%s" is an unknown target', target)
logger.info('Choices are: %s', all_targets)
return

entry = {}
entry['local_folder'] = os.path.expanduser(utils.get_input('local folder: '))
entry['s3_uri'] = utils.get_input('s3 uri: ')
entry['aws_access_key_id'] = utils.get_input('AWS Access Key ID: ')
entry['aws_secret_access_key'] = utils.get_input('AWS Secret Access Key: ', secret=True)
entry['region_name'] = utils.get_input('region name: ')

if target is not None:
entry['aws_access_key_id'] = config['targets'][target]['aws_access_key_id']
entry['aws_secret_access_key'] = config['targets'][target]['aws_secret_access_key']
else:
entry['aws_access_key_id'] = utils.get_input('AWS Access Key ID: ')
entry['aws_secret_access_key'] = utils.get_input('AWS Secret Access Key: ', secret=True)

default_name = os.path.basename(entry['s3_uri'])
name = utils.get_input('Provide a name for this entry [{}]: '.format(default_name))

Expand Down
74 changes: 69 additions & 5 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def test_add_command(self, add_command):
class TestGetConfigFile(object):
@mock.patch('s4.cli.CONFIG_FILE_PATH', '/i/dont/exist')
def test_no_file(self):
assert cli.get_config() == {'targets': []}
assert cli.get_config() == {'targets': {}}

def test_correct_output(self, config_file):
with open(config_file, 'w') as fp:
Expand Down Expand Up @@ -624,14 +624,15 @@ def test_correct_behaviour(self, get_input, config_file):
fake_stream = FakeInputStream([
'/home/user/Documents',
's3://mybucket/Documents',
'eu-west-2',
'aaaaaaaaaaaaaaaaaaaaaaaa',
'bbbbbbbbbbbbbbbbbbbbbbbb',
'eu-west-2',
'',
])
get_input.side_effect = fake_stream
args = argparse.Namespace(copy_target_credentials=None)

cli.add_command(None, {}, create_logger())
cli.add_command(args, {'targets': {}}, create_logger())

with open(config_file, 'r') as fp:
new_config = json.load(fp)
Expand All @@ -649,18 +650,81 @@ def test_correct_behaviour(self, get_input, config_file):
}
assert new_config == expected_config

def test_copy_target_credentials(self, get_input, config_file):
fake_stream = FakeInputStream([
'/home/user/Animals',
's3://mybucket/Zoo',
'us-west-2',
'Beasts',
])
get_input.side_effect = fake_stream
args = argparse.Namespace(copy_target_credentials='bar')

cli.add_command(
args,
{
'targets': {
'bar': {
'aws_secret_access_key': 'bar-secretz',
'aws_access_key_id': 'so-much-bar',
}
}
},
create_logger()
)

with open(config_file, 'r') as fp:
new_config = json.load(fp)

expected_config = {
'targets': {
'bar': {
'aws_access_key_id': 'so-much-bar',
'aws_secret_access_key': 'bar-secretz',
},
'Beasts': {
'local_folder': '/home/user/Animals',
's3_uri': 's3://mybucket/Zoo',
'aws_access_key_id': 'so-much-bar',
'aws_secret_access_key': 'bar-secretz',
'region_name': 'us-west-2'
}
}
}
assert new_config == expected_config

def test_copy_target_credentials_bad_target(self, get_input, capsys):
fake_stream = FakeInputStream([
'/home/user/Animals',
's3://mybucket/Zoo',
'us-west-2',
'Beasts',
])
get_input.side_effect = fake_stream
args = argparse.Namespace(copy_target_credentials='Foo')

cli.add_command(args, {'targets': {'bar': {}}}, create_logger())

out, err = capsys.readouterr()
assert out == ''
assert err == (
'"Foo" is an unknown target\n'
'Choices are: [\'bar\']\n'
)

def test_custom_target_name(self, get_input, config_file):
fake_stream = FakeInputStream([
'/home/user/Music',
's3://mybucket/Musiccccc',
'us-west-1',
'1234567890',
'abcdefghij',
'us-west-1',
'Tunes',
])
get_input.side_effect = fake_stream
args = argparse.Namespace(copy_target_credentials=None)

cli.add_command(None, {}, create_logger())
cli.add_command(args, {'targets': {}}, create_logger())

with open(config_file, 'r') as fp:
new_config = json.load(fp)
Expand Down

0 comments on commit 3b7e429

Please sign in to comment.