Files
q-tools/py-packages/spiller/spiller/__init__.py
2025-09-19 12:15:33 +03:00

118 lines
3.6 KiB
Python

import argparse
import os
from spiller.spiller import DEFAULT_CONFIG, DEFAULT_STORAGE, Spiller, decrypt, encrypt
__version__ = "0.5"
def get_opts():
parser = argparse.ArgumentParser(
prog="spill",
description=f"""Key/value storage that uses JSON and GPG as backend.
Values are encrypted symmetrically with the key provided, or a random string is generated.
Encryption key can be passed from a variable SPILLER_KEY instead of a switch.
Storage file can be changed with SPILLER_STORAGE env variable, in a
"SPILLER_STORAGE": key in a JSON file read at {DEFAULT_STORAGE}
""",
)
parser.add_argument("--version", action="version", version=__version__)
parser.add_argument("-q", "--quiet", action="store_true", default=False)
subparsers = parser.add_subparsers(dest="command", help="Command")
set_parser = subparsers.add_parser("set")
get_parser = subparsers.add_parser("get")
del_parser = subparsers.add_parser("list")
del_parser = subparsers.add_parser("del")
set_parser.add_argument(
"name",
action="store",
help="Name of secret",
)
set_parser.add_argument(
"data",
action="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. Use filename - for stdin",
)
set_parser.add_argument("--plain", action="store_true", default=False, help="Do not encrypt")
set_parser.add_argument(
"--key",
action="store",
default=os.getenv("SPILLER_KEY", None),
help="Encryption key (or use SPILLER_KEY)",
)
set_parser.add_argument(
"--key-file",
action="store",
default=None,
type=argparse.FileType("r"),
help="Read encryption key stored in a file",
)
get_parser.add_argument(
"name",
action="store",
help="Name of secret",
)
get_parser.add_argument(
"--key",
action="store",
default=os.getenv("SPILLER_KEY", None),
help="Decryption key (or use SPILLER_KEY)",
)
get_parser.add_argument(
"--key-file",
action="store",
default=None,
type=argparse.FileType("r"),
help="Read encryption key stored in a file. Will strip newlines at the end.",
)
del_parser.add_argument(
"name",
action="store",
help="Name of secret to delete",
)
args = parser.parse_args()
if args.command is None:
raise parser.error("Command missing")
try:
if args.key_file:
with args.key_file as fp:
args.key = fp.read().rstrip("\n")
except AttributeError:
pass
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 main():
opts = get_opts()
spill = Spiller()
spill.verbose = not opts.quiet
if opts.command == "set":
spill.store(opts.name, opts.data, opts.key, opts.plain)
if opts.command == "get":
print(spill.retrieve(opts.name, opts.key), end="")
if opts.command == "list":
print(spill.format_storage())
if opts.command == "del":
spill.del_storage(opts.name)