Make edit command more robust

This commit is contained in:
Tim Van Baak 2023-05-31 19:53:46 -07:00
parent 096a594813
commit 48efb2d3cd
1 changed files with 44 additions and 15 deletions

View File

@ -26,12 +26,14 @@ def cmd_edit(cmd_args):
description=cmd_edit.__doc__, description=cmd_edit.__doc__,
) )
parser.add_argument( parser.add_argument(
"--base", "--data",
"-d",
default=intake_data_dir(), default=intake_data_dir(),
help="Path to the intake data directory containing source directories", help="Path to the intake data directory",
) )
parser.add_argument( parser.add_argument(
"--source", "--source",
"-s",
required=True, required=True,
help="Source name to edit", help="Source name to edit",
) )
@ -42,21 +44,44 @@ def cmd_edit(cmd_args):
print("Cannot edit, no EDITOR set") print("Cannot edit, no EDITOR set")
return 1 return 1
data = Path(args.data)
source_path: Path = data / args.source
if not source_path.exists():
yn = input("Source does not exist, create? [yN] ")
if yn.strip().lower() != "y":
return 0
source_path.mkdir()
with (source_path / "intake.json").open("w") as f:
json.dump({
"fetch": {
"exe": "",
"args": [],
},
"actions": {},
"env": {},
}, f, indent=2)
# Make a copy of the config # Make a copy of the config
source = LocalSource(Path(args.base), args.source) source = LocalSource(data, args.source)
tmp_path = source.source_path / "intake.json.tmp" tmp_path = source.source_path / "intake.json.tmp"
tmp_path.write_text(json.dumps(source.get_config(), indent=2)) tmp_path.write_text(json.dumps(source.get_config(), indent=2))
# Edit the config while True:
subprocess.run([editor_cmd, tmp_path]) # Edit the config
subprocess.run([editor_cmd, tmp_path])
# Commit the change if the new config is valid # Check if the new config is valid
try: try:
json.load(tmp_path.open()) json.load(tmp_path.open())
except json.JSONDecodeError: except json.JSONDecodeError:
print("Invalid JSON") yn = input("Invalid JSON. Return to editor? [Yn] ")
return 1 if yn.strip().lower() != "n":
tmp_path.replace(source.source_path / "intake.json") continue
tmp_path.unlink()
return 0
tmp_path.replace(source.source_path / "intake.json")
break
return 0 return 0
@ -68,12 +93,14 @@ def cmd_update(cmd_args):
description=cmd_update.__doc__, description=cmd_update.__doc__,
) )
parser.add_argument( parser.add_argument(
"--base", "--data",
"-d",
default=intake_data_dir(), default=intake_data_dir(),
help="Path to the intake data directory containing source directories", help="Path to the intake data directory containing source directories",
) )
parser.add_argument( parser.add_argument(
"--source", "--source",
"-s",
required=True, required=True,
help="Source name to fetch", help="Source name to fetch",
) )
@ -84,7 +111,7 @@ def cmd_update(cmd_args):
) )
args = parser.parse_args(cmd_args) args = parser.parse_args(cmd_args)
source = LocalSource(Path(args.base), args.source) source = LocalSource(Path(args.data), args.source)
try: try:
items = fetch_items(source) items = fetch_items(source)
if not args.dry_run: if not args.dry_run:
@ -118,6 +145,7 @@ def cmd_feed(cmd_args):
) )
parser.add_argument( parser.add_argument(
"--sources", "--sources",
"-s",
nargs="+", nargs="+",
help="Limit feed to these sources", help="Limit feed to these sources",
) )
@ -176,7 +204,8 @@ def cmd_run(cmd_args):
description=cmd_run.__doc__, description=cmd_run.__doc__,
) )
parser.add_argument( parser.add_argument(
"--base", "--data",
"-d",
default=intake_data_dir(), default=intake_data_dir(),
help="Path to the intake data directory containing source directories", help="Path to the intake data directory containing source directories",
) )