Skip to content

Commit

Permalink
add repl
Browse files Browse the repository at this point in the history
  • Loading branch information
meeuw committed Jul 4, 2021
1 parent 0e6fbaa commit 0cf490d
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion ykman/cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
import time
import sys
import logging
import json
import io
import contextlib


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -352,7 +355,35 @@ def list_keys(ctx, serials, readers):
click.echo(f"{name} [{mode}] <access denied>")


COMMANDS = (list_keys, info, otp, openpgp, oath, piv, fido, config, apdu)
@cli.command("repl")
@click.pass_context
def repl(ctx):
"""
Enter commands as REPL (formatted as JSON)
"""
while 1:
line = input()
stdout = io.StringIO()
stderr = io.StringIO()
if not line:
break
try:
with contextlib.redirect_stderr(stderr), contextlib.redirect_stdout(stdout):
cli.main(args=json.loads(line))
except SystemExit:
pass
print(
json.dumps(
{
"stdout": stdout.getvalue().splitlines(),
"stderr": stderr.getvalue().splitlines(),
}
),
flush=True,
)


COMMANDS = (repl, list_keys, info, otp, openpgp, oath, piv, fido, config, apdu)


for cmd in COMMANDS:
Expand Down

0 comments on commit 0cf490d

Please sign in to comment.