-
Notifications
You must be signed in to change notification settings - Fork 30
/
command_dockyard.py
80 lines (62 loc) · 3.37 KB
/
command_dockyard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from armada_command.armada_utils import print_err, print_table
from armada_command.dockyard import alias
from armada_command.dockyard.alias import print_http_dockyard_unavailability_warning
from armada_command.dockyard.dockyard import dockyard_factory, DockyardFactoryException, DockyardDetectionException
def add_arguments(parser):
subparsers = parser.add_subparsers(help='Manage dockyard aliases', dest='dockyard_command')
parser_set_help = 'Set dockyard alias'
parser_set = subparsers.add_parser('set', help=parser_set_help, description=parser_set_help)
parser_set.add_argument('name', help='Name of the dockyard alias')
parser_set.add_argument('address', help='ip[:port] of the dockyard')
parser_set.add_argument('--user', help='user')
parser_set.add_argument('--password', help='password')
parser_set.set_defaults(dockyard_func=command_dockyard_set)
subparsers.add_parser(parser_set)
parser_list_help = 'List dockyard aliases'
parser_list = subparsers.add_parser('list', help=parser_list_help, description=parser_list_help)
parser_list.set_defaults(dockyard_func=command_dockyard_list)
subparsers.add_parser(parser_list)
parser_remove_help = 'Delete dockyard alias'
parser_remove = subparsers.add_parser('delete', help=parser_remove_help, description=parser_remove_help)
parser_remove.add_argument('name', help='Name of the dockyard alias')
parser_remove.set_defaults(dockyard_func=command_dockyard_remove)
subparsers.add_parser(parser_remove)
parser_default_help = 'Get or set default alias'
parser_default = subparsers.add_parser('default', help=parser_default_help, description=parser_default_help)
parser_default.add_argument('name', help='Name of the dockyard alias', nargs='?')
parser_default.set_defaults(dockyard_func=command_dockyard_default)
subparsers.add_parser(parser_default)
def command_dockyard(args):
args.dockyard_func(args)
def command_dockyard_set(args):
warning_header = " Warning!\n Your dockyard alias has been set BUT:"
alias.set_alias(args.name, args.address, args.user, args.password)
try:
dockyard = dockyard_factory(args.address, args.user, args.password)
if dockyard.is_http():
print_http_dockyard_unavailability_warning(args.address, args.name, warning_header)
except (DockyardFactoryException, DockyardDetectionException) as e:
print_err('{}\n{}'.format(warning_header, e))
def command_dockyard_list(args):
output_header = ['Default', 'Alias', 'Address', 'User', 'Password']
output_rows = [output_header]
alias_list = alias.get_list()
for alias_dict in alias_list:
default_string = '->'.rjust(len(output_header[0])) if alias_dict['is_default'] else ''
row = [default_string, alias_dict['name'], alias_dict['address'], alias_dict.get('user', ''),
_hide_password(alias_dict.get('password', ''))]
output_rows.append(row)
print_table(output_rows)
def _hide_password(password):
return '****' if password else ''
def command_dockyard_remove(args):
alias.remove_alias(args.name)
def command_dockyard_default(args):
if args.name:
alias.set_default(args.name)
else:
default_alias = alias.get_default()
if default_alias:
print('Default alias for dockyard is: {default_alias}'.format(**locals()))
else:
print('No default alias set')