python cli tool: handle script arguments

This commit is contained in:
quenousimporte 2023-09-13 17:10:46 +02:00
parent a47a5364c4
commit a4e015af10
1 changed files with 42 additions and 30 deletions

View File

@ -3,6 +3,7 @@ import json
import subprocess import subprocess
import urllib.parse import urllib.parse
import os import os
import sys
def listnotes(filter = ""): def listnotes(filter = ""):
for i in reversed(range(len(data))): for i in reversed(range(len(data))):
@ -17,28 +18,7 @@ def writetextfile(path, content):
with io.open(path, mode = "w", encoding = "utf-8") as f: with io.open(path, mode = "w", encoding = "utf-8") as f:
f.write(content) f.write(content)
settings = json.loads(readtextfile("settings.json")) def editnote(note):
if os.path.isfile("data/backupdata.acs"):
os.remove("data/backupdata.acs")
subprocess.call(["curl", "-s", "-X", "POST", "-F", "action=fetch", "-F", "password=" + settings["password"], "-o", "data/backupdata.acs", settings["url"] + "/handler.php"])
subprocess.call([settings["commands"]["gpg"], "-q", "--yes", "--output", "data/backupdata.json", "--decrypt", "data/backupdata.acs"])
data = json.loads(readtextfile("data/backupdata.json"))
command = "list"
while not (command == "quit" or command == "exit" or command == "q"):
if command == "list":
listnotes()
elif command.split()[0] == "find":
searchstring = command.replace("find ", "")
listnotes(searchstring)
else:
try:
index = int(command)
note = data[index]
content = note["content"] content = note["content"]
writetextfile("data/backupnote.md", content) writetextfile("data/backupnote.md", content)
@ -69,7 +49,39 @@ while not (command == "quit" or command == "exit" or command == "q"):
else: else:
print("no change") print("no change")
settings = json.loads(readtextfile("settings.json"))
if os.path.isfile("data/backupdata.acs"):
os.remove("data/backupdata.acs")
subprocess.call(["curl", "-s", "-X", "POST", "-F", "action=fetch", "-F", "password=" + settings["password"], "-o", "data/backupdata.acs", settings["url"] + "/handler.php"])
subprocess.call([settings["commands"]["gpg"], "-q", "--yes", "--output", "data/backupdata.json", "--decrypt", "data/backupdata.acs"])
data = json.loads(readtextfile("data/backupdata.json"))
command = "list"
if len(sys.argv) > 1:
command = sys.argv[1]
while not (command == "quit" or command == "exit" or command == "q"):
if command == "list":
listnotes()
elif command.split()[0] == "find":
searchstring = command.replace("find ", "")
listnotes(searchstring)
else:
try:
index = int(command)
note = data[index]
except: except:
print("error with command " + command) note = next((x for x in data if x["title"] == command), None)
if note:
editnote(note)
else:
print("unknown command")
command = input("> ") command = input("> ")