take spill from file

This commit is contained in:
2023-06-20 17:39:10 +03:00
parent a59e4af742
commit 77cb302b5d

View File

@@ -12,11 +12,11 @@ import string
def get_config():
default_config = {
'SPILLER_STORAGE': os.path.expanduser("~/.config/spiller/storage.json")
"SPILLER_STORAGE": os.path.expanduser("~/.config/spiller/storage.json")
}
try:
with open(os.path.expanduser("~/.config/spiller/config.json"), 'rt') as fp:
with open(os.path.expanduser("~/.config/spiller/config.json"), "rt") as fp:
default_config.update(json.load(fp))
except Exception:
pass
@@ -24,7 +24,6 @@ def get_config():
def get_opts():
parser = argparse.ArgumentParser(
prog="spill",
description="""Key/value storage that uses JSON and GPG as backend.
@@ -48,7 +47,14 @@ def get_opts():
set_parser.add_argument(
"data",
action="store",
help="Data to store",
nargs="?",
help="Data to store. Must use this or --data-file.",
)
set_parser.add_argument(
"--data-file",
action="store",
type=argparse.FileType("r"),
help="Read the data to store from a file. Must use this or [data]. Will strip newlines at the end.",
)
set_parser.add_argument(
"--plain", action="store_true", default=False, help="Do not encrypt"
@@ -63,7 +69,7 @@ def get_opts():
"--key-file",
action="store",
default=None,
type=argparse.FileType('r'),
type=argparse.FileType("r"),
help="Read encryption key stored in a file",
)
get_parser.add_argument(
@@ -81,8 +87,8 @@ def get_opts():
"--key-file",
action="store",
default=None,
type=argparse.FileType('r'),
help="Read encryption key stored in a file",
type=argparse.FileType("r"),
help="Read encryption key stored in a file. Will strip newlines at the end.",
)
del_parser.add_argument(
"name",
@@ -90,14 +96,24 @@ def get_opts():
help="Name of secret to delete",
)
args = parser.parse_args()
try:
if args.key_file:
with args.key_file as fp:
args.key = fp.read().rstrip("\n")
except AttributeError:
pass
return args
if args.command == "set":
if args.data and args.data_file:
raise parser.error("Can not use both [data] and --data-file")
if args.data is None and args.data_file is None:
raise parser.error("Must use either [data] or --data-file")
if args.data_file:
with args.data_file as fp:
args.data = fp.read().rstrip("\n")
return args
def list_storage():
@@ -119,7 +135,6 @@ def list_storage():
def load_storage():
try:
with open(JSON, "rt") as fp:
return json.load(fp)
@@ -128,7 +143,6 @@ def load_storage():
def save_storage(storage):
if not os.path.exists(JSON):
os.makedirs(os.path.dirname(JSON), exist_ok=True)
with open(JSON, "wt") as fp:
@@ -140,7 +154,6 @@ def save_storage(storage):
def del_storage(name):
storage = load_storage()
del storage[name]
save_storage(storage)
@@ -195,7 +208,6 @@ def retrieve(name, key=None):
def encrypt(data, key):
p = subprocess.run(
["gpg", "-a", "--symmetric", "--batch", "--passphrase-fd", "0"],
input=f"{key}\n{data}".encode(),
@@ -209,7 +221,6 @@ def encrypt(data, key):
def decrypt(encrypted, key):
if key == None:
print("Requires --key!", file=sys.stderr)
sys.exit(1)
@@ -227,7 +238,6 @@ def decrypt(encrypted, key):
def get_random_key():
return "-".join(
[
"".join(
@@ -239,9 +249,8 @@ def get_random_key():
CONFIG = get_config()
JSON = os.getenv(
"SPILLER_STORAGE", CONFIG['SPILLER_STORAGE']
)
JSON = os.getenv("SPILLER_STORAGE", CONFIG["SPILLER_STORAGE"])
def main():
opts = get_opts()