From b87de6969d4152d649eb23ca465a1898b31b6497 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serkan=20=C3=96ZAL?= Date: Mon, 26 Jul 2021 13:10:15 +0300 Subject: [PATCH 1/2] Added ability to get handler class name through `_HANDLER` environment variable like on real "AWS Lambda" or "Lambci" platform --- .../java/cloud/localstack/LambdaExecutor.java | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/main/java/cloud/localstack/LambdaExecutor.java b/src/main/java/cloud/localstack/LambdaExecutor.java index ba3ca32..9603c24 100644 --- a/src/main/java/cloud/localstack/LambdaExecutor.java +++ b/src/main/java/cloud/localstack/LambdaExecutor.java @@ -41,13 +41,13 @@ public class LambdaExecutor { @SuppressWarnings("unchecked") public static void main(String[] args) throws Exception { - if(args.length < 2) { + if (args.length != 1 && args.length != 2) { System.err.println("Usage: java " + LambdaExecutor.class.getSimpleName() + - " "); + " [] "); System.exit(1); } - String fileContent = readFile(args[1]); + String fileContent = args.length == 1 ? readFile(args[0]) : readFile(args[1]); ObjectMapper reader = new ObjectMapper(); reader.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true); reader.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); @@ -56,7 +56,17 @@ public static void main(String[] args) throws Exception { List> records = (List>) get(map, "Records"); Object inputObject = map; - Object handler = getHandler(args[0]); + Object handler; + if (args.length == 2) { + handler = getHandler(args[0]); + } else { + String handlerEnvVar = System.getenv("_HANDLER"); + if (handlerEnvVar == null) { + System.err.println("Handler must be provided by '_HANDLER' environment variable"); + System.exit(1); + } + handler = getHandler(handlerEnvVar); + } if (records == null) { Optional deserialisedInput = getInputObject(reader, fileContent, handler); if (deserialisedInput.isPresent()) { @@ -134,19 +144,19 @@ private static Object getHandler(String handlerName) throws NoSuchMethodExceptio public static T get(Map map, String key) { T result = map.get(key); - if(result != null) { + if (result != null) { return result; } key = StringUtils.uncapitalize(key); result = map.get(key); - if(result != null) { + if (result != null) { return result; } return map.get(key.toLowerCase()); } public static String readFile(String file) throws Exception { - if(!file.startsWith("/")) { + if (!file.startsWith("/")) { file = System.getProperty("user.dir") + "/" + file; } return Files.lines(Paths.get(file), StandardCharsets.UTF_8).collect(Collectors.joining()); From ab47bc1695d7e42c808dbf810ced7101b5ff9748 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serkan=20=C3=96ZAL?= Date: Mon, 26 Jul 2021 13:42:49 +0300 Subject: [PATCH 2/2] Bumped version and added new version description to the changelog --- README.md | 3 ++- pom.xml | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c9733bb..8cece37 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ Simply add the following dependency to your `pom.xml` file: cloud.localstack localstack-utils - 0.2.13 + 0.2.14 ``` @@ -108,6 +108,7 @@ make build ## Change Log +* v0.2.14: Add ability to get handler class name through `_HANDLER` environment variable like on real **AWS Lambda** platform and **Lambci** environment * v0.2.11: Enable specification of "platform" when configuring container * v0.2.10: Add Lambda async utils for AWS SDK v2; add support for specifying bind mounts and init scripts via `@LocalstackDockerProperties`; add PowerMock integration for easy patching of AWS SDK to use local endpoints; add support for configuring the Docker image name via `@LocalstackDockerProperties`; add tests for templated emails * v0.2.8: Allow overwriting the port binding via environment variables diff --git a/pom.xml b/pom.xml index 1a74bd1..00b9667 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ cloud.localstack localstack-utils jar - 0.2.13 + 0.2.14 localstack-utils Java utilities for the LocalStack platform.