From 73c2319645b83c7258aa2c4d2bf8dd0da6923143 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Tue, 17 Dec 2019 09:40:55 -0800 Subject: [PATCH] Add check for source existing in inq add --- inquisitor/cli.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/inquisitor/cli.py b/inquisitor/cli.py index 7ff4fa9..fea0334 100644 --- a/inquisitor/cli.py +++ b/inquisitor/cli.py @@ -85,6 +85,7 @@ def command_add(args): parser.add_argument("--body", help="HTML") parser.add_argument("--tags", help="Comma-separated list") parser.add_argument("--ttl", type=int, help="Cleanup protection in seconds") + parser.add_argument("--create", action="store_true", help="Create source if it doesn't exist") args = parser.parse_args(args) if not args.title: @@ -94,6 +95,15 @@ def command_add(args): logger.error("Couldn't find dungeon. Set INQUISITOR_DUNGEON or cd to parent folder of ./dungeon") return -1 + source = args.source or 'inquisitor' + cell_path = os.path.join(DUNGEON_PATH, source) + if not os.path.isdir(cell_path): + if args.create: + os.mkdir(cell_path) + else: + logger.error("Source '{}' does not exist".format(source)) + return -1 + from importer import populate_new item = { 'id': '{:x}'.format(random.getrandbits(16 * 4)), @@ -109,6 +119,7 @@ def command_add(args): if args.tags: item['tags'] = [str(tag) for tag in args.tags] if args.ttl: item['ttl'] = int(args.ttl) populate_new(item) + s = json.dumps(item, indent=2) path = os.path.join(DUNGEON_PATH, item['source'], item['id'] + '.item') with open(path, 'w', encoding='utf8') as f: