Skip to content

Commit b87de69

Browse files
author
Serkan ÖZAL
committed
Added ability to get handler class name through _HANDLER environment variable like on real "AWS Lambda" or "Lambci" platform
1 parent 9d22eb7 commit b87de69

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

Diff for: src/main/java/cloud/localstack/LambdaExecutor.java

+17-7
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ public class LambdaExecutor {
4141

4242
@SuppressWarnings("unchecked")
4343
public static void main(String[] args) throws Exception {
44-
if(args.length < 2) {
44+
if (args.length != 1 && args.length != 2) {
4545
System.err.println("Usage: java " + LambdaExecutor.class.getSimpleName() +
46-
" <lambdaClass> <recordsFilePath>");
46+
" [<lambdaClass>] <recordsFilePath>");
4747
System.exit(1);
4848
}
4949

50-
String fileContent = readFile(args[1]);
50+
String fileContent = args.length == 1 ? readFile(args[0]) : readFile(args[1]);
5151
ObjectMapper reader = new ObjectMapper();
5252
reader.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
5353
reader.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
@@ -56,7 +56,17 @@ public static void main(String[] args) throws Exception {
5656
List<Map<String,Object>> records = (List<Map<String, Object>>) get(map, "Records");
5757
Object inputObject = map;
5858

59-
Object handler = getHandler(args[0]);
59+
Object handler;
60+
if (args.length == 2) {
61+
handler = getHandler(args[0]);
62+
} else {
63+
String handlerEnvVar = System.getenv("_HANDLER");
64+
if (handlerEnvVar == null) {
65+
System.err.println("Handler must be provided by '_HANDLER' environment variable");
66+
System.exit(1);
67+
}
68+
handler = getHandler(handlerEnvVar);
69+
}
6070
if (records == null) {
6171
Optional<Object> deserialisedInput = getInputObject(reader, fileContent, handler);
6272
if (deserialisedInput.isPresent()) {
@@ -134,19 +144,19 @@ private static Object getHandler(String handlerName) throws NoSuchMethodExceptio
134144

135145
public static <T> T get(Map<String,T> map, String key) {
136146
T result = map.get(key);
137-
if(result != null) {
147+
if (result != null) {
138148
return result;
139149
}
140150
key = StringUtils.uncapitalize(key);
141151
result = map.get(key);
142-
if(result != null) {
152+
if (result != null) {
143153
return result;
144154
}
145155
return map.get(key.toLowerCase());
146156
}
147157

148158
public static String readFile(String file) throws Exception {
149-
if(!file.startsWith("/")) {
159+
if (!file.startsWith("/")) {
150160
file = System.getProperty("user.dir") + "/" + file;
151161
}
152162
return Files.lines(Paths.get(file), StandardCharsets.UTF_8).collect(Collectors.joining());

0 commit comments

Comments
 (0)