diff --git a/nc_password_client/nc_password_client.py b/nc_password_client/nc_password_client.py index b5a1bdf..b34531a 100755 --- a/nc_password_client/nc_password_client.py +++ b/nc_password_client/nc_password_client.py @@ -96,7 +96,6 @@ class NextcloudHandler: elif os.environ.get('NEXTCLOUD_SSL_MODE') == 'skip': self.ssl = False - self.details = params.get('details') or False self.x_api_session = None self.host = params.get('host') or os.environ.get('NEXTCLOUD_HOST') @@ -243,6 +242,7 @@ class NextcloudHandler: def get(self, path): '''Do a GET request''' + self.debug({ "action": "get", "message": "Requesting {path}" }) try: r = requests.get( f'{self.http}://{self.host}/{path}', @@ -251,6 +251,9 @@ class NextcloudHandler: ) if r.status_code == 200: + self.debug( + {"action": "get", "status_code": r.status_code, "size": len(r.content)} + ) return r.json() if r.status_code == 404: self.error( @@ -277,6 +280,7 @@ class NextcloudHandler: "error": error } ) + sys.exit(5) return None def propfind(self, path): @@ -597,7 +601,7 @@ class NextcloudHandler: return folder.get('id') return None - def get_password(self, name): + def get_password(self, name, details): '''Get a password''' r = self.list_passwords() ret = [] @@ -606,7 +610,7 @@ class NextcloudHandler: item_key = self.keychain['keys'].get(item['cseKey']) if item_key: if decrypt_item(item_key, item, 'label') == name: - if self.details: + if details: item['password'] = decrypt_item(item_key, item, 'password') item['url'] = decrypt_item(item_key, item, 'url') item['username'] = decrypt_item(item_key, item, 'username') @@ -618,7 +622,7 @@ class NextcloudHandler: ret.append(decrypt_item(item_key, item, 'password')) else: if item['label'] == name: - if self.details: + if details: ret.append(item) else: ret.append(item['password']) @@ -640,8 +644,11 @@ class NextcloudHandler: def create_password(self, post_obj): '''Create/add a password''' - if 'folder' in post_obj: - post_obj['folder'] = self.get_folder_id(post_obj['folder']) + if 'folder' in post_obj and post_obj['folder']: + folder_id = self.get_folder_id(post_obj['folder']) + if not folder_id: + folder_id = self.create_passwords_folder(post_obj['folder'])['id'] + post_obj['folder'] = folder_id if not 'username' in post_obj: post_obj['username'] = '' if not 'url' in post_obj: @@ -810,7 +817,6 @@ class NcPasswordClient: "api_token": api_token, "cse_password": cse_password, "timeout": timeout, - "details": True, } self.nc = NextcloudHandler(params) @@ -828,7 +834,7 @@ class NcPasswordClient: def info(self, obj): '''Show information''' - self.info( + self._log.info( json.dumps(obj, indent=2) ) @@ -838,9 +844,9 @@ class NcPasswordClient: json.dumps(obj, indent=2) ) - def get_password(self, name): + def get_password(self, name, details): '''Get a single password''' - self.info(self.nc.get_password(name)) + self.info(self.nc.get_password(name, details)) def list_passwords(self): '''List all passwords''' @@ -1011,11 +1017,17 @@ def cli(ctx, debug_level, log_file, host, user, api_token, cse_password, timeout @cli.command() @click.option('--name', '-n', required=True, help='Name of the password to show') +@click.option( + '--details', '-D', + is_flag=True, + default=False, + help='Show all details of the password or just the password (False)' +) @click_config_file.configuration_option() @click.pass_context -def show(ctx, name): +def show(ctx, name, details): '''Show a single password''' - ctx.obj['NcPasswordClient'].get_password(name) + ctx.obj['NcPasswordClient'].get_password(name, details) @cli.command() @click_config_file.configuration_option()