| 81 | return add_parser |
| 82 | |
| 83 | def execute(self, params, **kwargs): |
| 84 | title = kwargs['title'] if 'title' in kwargs else None |
| 85 | login = kwargs['login'] if 'login' in kwargs else None |
| 86 | password = kwargs['password'] if 'password' in kwargs else None |
| 87 | url = kwargs['url'] if 'url' in kwargs else None |
| 88 | custom_list = kwargs['custom'] if 'custom' in kwargs else None |
| 89 | notes = kwargs['notes'] if 'notes' in kwargs else None |
| 90 | |
| 91 | generate = kwargs['generate'] if 'generate' in kwargs else None |
| 92 | if generate: |
| 93 | password = generator.generate(16) |
| 94 | |
| 95 | force = kwargs['force'] if 'force' in kwargs else None |
| 96 | if not force: |
| 97 | if not title: |
| 98 | title = input('...' + 'Title: '.rjust(16)) |
| 99 | if not login: |
| 100 | login = input('...' + 'Login: '.rjust(16)) |
| 101 | if not password: |
| 102 | password = input('...' + 'Password: '.rjust(16)) |
| 103 | if not url: |
| 104 | url = input('...' + 'Login URL: '.rjust(16)) |
| 105 | if not title: |
| 106 | raise CommandError('add', 'Invalid title. Expected non-empty string.') |
| 107 | |
| 108 | custom = [] |
| 109 | if custom_list: |
| 110 | if type(custom_list) == str: |
| 111 | if custom_list[0] == '{' and custom_list[-1] == '}': |
| 112 | try: |
| 113 | custom_json = json.loads(custom_list) |
| 114 | for k, v in custom_json.items(): |
| 115 | custom.append({ |
| 116 | 'name': k, |
| 117 | 'value': self.custom_field_value(v) |
| 118 | }) |
| 119 | except ValueError as e: |
| 120 | raise CommandError('add', 'Invalid custom fields JSON input: {0}'.format(e)) |
| 121 | else: |
| 122 | pairs = custom_list.split(',') |
| 123 | for pair in pairs: |
| 124 | idx = pair.find(':') |
| 125 | if idx > 0: |
| 126 | custom.append({ |
| 127 | 'name': pair[:idx].strip(), |
| 128 | 'value': self.custom_field_value(pair[idx+1:].strip()) |
| 129 | }) |
| 130 | else: |
| 131 | raise CommandError('add', 'Invalid custom fields input. Expected: "Key:Value". Got: "{0}"'.format(pair)) |
| 132 | |
| 133 | elif type(custom_list) == list: |
| 134 | for c in custom_list: |
| 135 | if type(c) == dict: |
| 136 | name = c.get('name') |
| 137 | value = c.get('value') |
| 138 | if name and value: |
| 139 | custom.append({ |
| 140 | 'type': 'text', |