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