Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add REPL command #425

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add repl
  • Loading branch information
meeuw committed Jul 4, 2021
commit 4d545445c75331eacaa29a1dde347f38825deb2a
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()
if not line:
break
stdout = io.StringIO()
stderr = io.StringIO()
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