def do_source(self, arg): """Add source to given artifact or most recently added artifact if not specified Usage: source # adds to last created artifact source <artifact name|session id> # adds to specific artifact """ if arg == '': last = self.session.receive('artifacts') _type = detect_type(last) else: _type = detect_type(arg) is_key, value = lookup_key(self.session, arg) if is_key and value is None: error('Unable to find artifact key in session (%s)' % arg) return elif is_key and value is not None: arg = value else: pass if self.db.exists(_type, {'name': last}): self.db.update_one(_type, {'name': last}, {'source': arg}) success('Added source to artifact entry (%s: %s)' % (last, arg)) else: warning('Failed to find last artifact in MongoDB. Run "new <artifact name>" before using the source command')
def do_report(self, arg): """Save artifact report as JSON file Usage: report <artifact name> report <session id>""" is_key, value = lookup_key(self.session, arg) if is_key and value is None: error('Unable to find artifact key in session (%s)' % arg) return elif is_key and value is not None: arg = value else: pass _type = detect_type(arg) result = self.db.find(_type, {'name': arg}, one=True) if len(result) == 0: warning('No entry found for artifact (%s)' % arg) else: report = storage.JSON(data=result, file_path=output_dir) report.save() if os.path.exists(report.file_path): success('Saved artifact report (%s)' % report.file_path) else: error('Failed to properly save report')
def do_cat(self, arg): """View artifact details or list API keys Usage: cat apikeys cat <artifact name>""" if arg == 'apikeys': data = json.load(open(common.API_CONF, 'rb')) print json.dumps(data, indent=2) else: is_key, value = lookup_key(self.session, arg) if is_key and value is None: error('Unable to find artifact key in session (%s)' % arg) return elif is_key and value is not None: arg = value else: pass artifact_type = detect_type(arg) result = self.db.find(artifact_type, {'name': arg}, one=True) if len(result) == 0: info('No entry found for artifact (%s)' % arg) else: print json.dumps(result, indent=2, separators=(',', ':'))
def do_delete(self, arg): """Remove artifact from database by name or ID Usage: delete <name> delete <session id>""" is_key, value = lookup_key(self.session, arg) if is_key and value is None: error('Unable to find artifact key in session (%s)' % arg) return elif is_key and value is not None: arg = value else: pass artifact_type = detect_type(arg) self.db.delete_one(artifact_type, {'name': arg})