python cli tool: handle script arguments
This commit is contained in:
parent
a47a5364c4
commit
a4e015af10
|
@ -3,6 +3,7 @@ import json
|
|||
import subprocess
|
||||
import urllib.parse
|
||||
import os
|
||||
import sys
|
||||
|
||||
def listnotes(filter = ""):
|
||||
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:
|
||||
f.write(content)
|
||||
|
||||
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"
|
||||
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]
|
||||
def editnote(note):
|
||||
content = note["content"]
|
||||
|
||||
writetextfile("data/backupnote.md", content)
|
||||
|
@ -69,7 +49,39 @@ while not (command == "quit" or command == "exit" or command == "q"):
|
|||
else:
|
||||
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:
|
||||
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("> ")
|
Loading…
Reference in New Issue