2020-02-23 15:07:02 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
import subprocess
|
|
|
|
|
import yaml
|
|
|
|
|
|
2021-10-03 12:24:11 +02:00
|
|
|
PASS = '/usr/bin/pass'
|
2020-02-23 15:07:02 +01:00
|
|
|
|
|
|
|
|
def run(args):
|
|
|
|
|
return(subprocess.run(
|
|
|
|
|
args,
|
|
|
|
|
stdin=sys.stdin,
|
|
|
|
|
stdout=sys.stdout,
|
|
|
|
|
stderr=sys.stderr).returncode)
|
|
|
|
|
|
|
|
|
|
def get_stdout(args):
|
|
|
|
|
return(subprocess.run(
|
|
|
|
|
args,
|
|
|
|
|
stdout=subprocess.PIPE).stdout)
|
|
|
|
|
|
|
|
|
|
def extract_from_dict(d, k, last=None):
|
|
|
|
|
if '.' in k:
|
|
|
|
|
k1, k2 = k.split('.', 1)
|
|
|
|
|
try:
|
|
|
|
|
d1 = d[k1]
|
|
|
|
|
return extract_from_dict(d1, k2, last=k1)
|
|
|
|
|
except KeyError:
|
|
|
|
|
print("\"{}\" was not found in the data".format(k))
|
|
|
|
|
sys.exit(3)
|
|
|
|
|
else:
|
|
|
|
|
if not isinstance(d, dict):
|
|
|
|
|
print("element \"{}\" is not a dictionary".format(last))
|
|
|
|
|
sys.exit(3)
|
|
|
|
|
try:
|
|
|
|
|
return d[k]
|
|
|
|
|
except KeyError:
|
|
|
|
|
print("\"{}\" was not found in the data".format(k))
|
|
|
|
|
sys.exit(3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cmd_get(*args):
|
|
|
|
|
if len(args) == 1:
|
|
|
|
|
print(get_stdout(['pass', 'show', args[0]]).split(b'\n')[0].decode())
|
|
|
|
|
return 0
|
|
|
|
|
|
2021-01-17 22:10:26 +01:00
|
|
|
if len(args) < 2:
|
2020-02-23 15:07:02 +01:00
|
|
|
print("what do you want to get?")
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
# get the pass output
|
|
|
|
|
out = get_stdout(['pass', 'show', args[0]])
|
|
|
|
|
try:
|
|
|
|
|
data = list(yaml.safe_load_all(out))[-1]
|
|
|
|
|
except yaml.scanner.ScannerError as e:
|
|
|
|
|
print("invalid YAML data: {}".format(str(e)))
|
|
|
|
|
return 2
|
|
|
|
|
except IndexError:
|
|
|
|
|
print("invalid YAML data: no YAML document found")
|
|
|
|
|
return 2
|
|
|
|
|
|
|
|
|
|
if not isinstance(data, dict):
|
|
|
|
|
print("invalid YAML data, not a dict")
|
|
|
|
|
return 2
|
|
|
|
|
|
2021-01-17 22:10:26 +01:00
|
|
|
for element in args[1:]:
|
|
|
|
|
keydata = extract_from_dict(data, element)
|
|
|
|
|
if isinstance(keydata, list) or isinstance(keydata, dict):
|
|
|
|
|
print(yaml.safe_dump(keydata, default_flow_style=False).strip())
|
|
|
|
|
else:
|
|
|
|
|
print(keydata)
|
2020-02-23 15:07:02 +01:00
|
|
|
|
|
|
|
|
cmds = ['get']
|
2020-03-28 16:09:23 +01:00
|
|
|
git_push_commands = ['insert', 'edit', 'generate', 'rm', 'mv', 'cp']
|
2020-02-23 15:07:02 +01:00
|
|
|
|
2020-03-28 16:09:23 +01:00
|
|
|
def run_pass(argv):
|
|
|
|
|
return run([PASS] + argv[1:])
|
|
|
|
|
|
|
|
|
|
def just_call_pass(argv):
|
|
|
|
|
sys.exit(run_pass(argv))
|
|
|
|
|
|
|
|
|
|
if len(sys.argv) == 1:
|
|
|
|
|
just_call_pass(sys.argv)
|
|
|
|
|
elif sys.argv[1] not in cmds:
|
|
|
|
|
if sys.argv[1] not in git_push_commands:
|
|
|
|
|
just_call_pass(sys.argv)
|
|
|
|
|
else:
|
|
|
|
|
e = run_pass(sys.argv)
|
|
|
|
|
if e == 0:
|
|
|
|
|
print("Pushing to remote repository...")
|
|
|
|
|
run([PASS, 'git', 'push'])
|
|
|
|
|
print("Done")
|
|
|
|
|
sys.exit(e)
|
2020-02-23 15:07:02 +01:00
|
|
|
else:
|
|
|
|
|
sys.exit(globals()['cmd_' + sys.argv[1]](*sys.argv[2:]))
|