Skip to content

Commit

Permalink
Detect TTY, Go Into Non-Interactive Mode If Not TTY (#186)
Browse files Browse the repository at this point in the history
* Detect TTY, Go Into Non-Interactive Mode If Not TTY

The selection prompt breaks when not in a terminal emulator / pseudo tty.

This checks for a tty and reverts to the old non-interactive behavior if not running in a tty.

* Fix tests

* Bump to v0.6.6
  • Loading branch information
kolanos authored Nov 23, 2021
1 parent 5726b1f commit e891f91
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 24 deletions.
31 changes: 21 additions & 10 deletions newrelic_lambda_cli/layers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
#
import sys #

import botocore
import click
Expand Down Expand Up @@ -44,16 +46,25 @@ def index(region, runtime, architecture):
def layer_selection(available_layers, runtime, architecture):
selected_layer = []
if len(available_layers) > 1:
layerOptions = []
for layer in available_layers:
layerOptions.append(layer["LatestMatchingVersion"]["LayerVersionArn"])

response = enquiries.choose(
"Discovered layers for runtime %s (%s)" % (runtime, architecture),
layerOptions,
)
success("Layer %s selected" % response)
selected_layer.append(response)
layer_options = [
layer["LatestMatchingVersion"]["LayerVersionArn"]
for layer in available_layers
]

if sys.stdout.isatty():
response = enquiries.choose(
"Discovered multiple layers for runtime %s (%s)"
% (runtime, architecture),
layer_options,
)
success("Layer %s selected" % response)
selected_layer.append(response)
else:
raise click.UsageError(
"Discovered multiple layers for runtime %s (%s):\n%s\n"
"Pass --layer-arn to specify a layer ARN"
% (runtime, architecture, "\n".join(layer_options))
)
else:
selected_layer.append(
available_layers[0]["LatestMatchingVersion"]["LayerVersionArn"]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name="newrelic-lambda-cli",
version="0.6.5",
version="0.6.6",
python_requires=">=3.3",
description="A CLI to install the New Relic AWS Lambda integration and layers.",
long_description=README,
Expand Down
35 changes: 22 additions & 13 deletions tests/test_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,26 +171,35 @@ def test_add_new_relic(aws_credentials, mock_function_config):
in update_kwargs["Environment"]["Variables"]["NEW_RELIC_LAMBDA_HANDLER"]
)

with patch("newrelic_lambda_cli.layers.enquiries.choose") as mock_enquiries:
mock_layers = [
{
"LatestMatchingVersion": {
"LayerVersionArn": "arn:aws:lambda:us-east-1:123456789:layer/javajava"
}
},
{
"LatestMatchingVersion": {
"LayerVersionArn": "arn:aws:lambda:us-east-1:123456789:layer/NewRelicLambdaExtension"
}
},
]
mock_layers = [
{
"LatestMatchingVersion": {
"LayerVersionArn": "arn:aws:lambda:us-east-1:123456789:layer/javajava"
}
},
{
"LatestMatchingVersion": {
"LayerVersionArn": "arn:aws:lambda:us-east-1:123456789:layer/NewRelicLambdaExtension"
}
},
]

with patch("newrelic_lambda_cli.layers.enquiries.choose") as mock_enquiries, patch(
"sys.stdout.isatty"
) as mock_isatty:
mock_isatty.return_value = True
mock_enquiries.return_value = (
"arn:aws:lambda:us-east-1:123456789:layer/javajava"
)

result = layer_selection(mock_layers, "python3.7", "x86_64")
assert result == [mock_enquiries.return_value]

with patch("sys.stdout.isatty") as mock_isatty:
mock_isatty.return_value = False
with pytest.raises(UsageError):
layer_selection(mock_layers, "python3.7", "x86_64")

config = mock_function_config("python3.7")
_add_new_relic(
layer_install(
Expand Down

0 comments on commit e891f91

Please sign in to comment.