Skip to content

Add ability to get handler class name through _HANDLER environment variable … #67

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

Merged
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Simply add the following dependency to your `pom.xml` file:
<dependency>
<groupId>cloud.localstack</groupId>
<artifactId>localstack-utils</artifactId>
<version>0.2.13</version>
<version>0.2.14</version>
</dependency>
```

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>cloud.localstack</groupId>
<artifactId>localstack-utils</artifactId>
<packaging>jar</packaging>
<version>0.2.13</version>
<version>0.2.14</version>
<name>localstack-utils</name>

<description>Java utilities for the LocalStack platform.</description>
Expand Down
24 changes: 17 additions & 7 deletions src/main/java/cloud/localstack/LambdaExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() +
" <lambdaClass> <recordsFilePath>");
" [<lambdaClass>] <recordsFilePath>");
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);
Expand All @@ -56,7 +56,17 @@ public static void main(String[] args) throws Exception {
List<Map<String,Object>> records = (List<Map<String, Object>>) 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<Object> deserialisedInput = getInputObject(reader, fileContent, handler);
if (deserialisedInput.isPresent()) {
Expand Down Expand Up @@ -134,19 +144,19 @@ private static Object getHandler(String handlerName) throws NoSuchMethodExceptio

public static <T> T get(Map<String,T> 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());
Expand Down