Skip to content

Commit 0665430

Browse files
committed
add --with-url option
1 parent 47dd245 commit 0665430

File tree

2 files changed

+68
-15
lines changed

2 files changed

+68
-15
lines changed

Sources/AWSLambdaPluginHelper/lambda-init/Initializer.swift

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,48 @@ import Foundation
2121
struct Initializer {
2222

2323
private let destFileName = "Sources/main.swift"
24-
24+
2525
func initialize(arguments: [String]) async throws {
2626

2727
let configuration = try InitializerConfiguration(arguments: arguments)
28-
28+
2929
if configuration.help {
3030
self.displayHelpMessage()
3131
return
3232
}
33-
33+
3434
let destFileURL = configuration.destinationDir.appendingPathComponent(destFileName)
3535
do {
36-
try functionWithUrlTemplate.write(to: destFileURL, atomically: true, encoding: .utf8)
37-
36+
37+
let template = TemplateType.template(for: configuration.templateType)
38+
try template.write(to: destFileURL, atomically: true, encoding: .utf8)
39+
3840
if configuration.verboseLogging {
3941
print("File created at: \(destFileURL)")
4042
}
41-
43+
4244
print("✅ Lambda function written to \(destFileName)")
43-
print("📦 You can now package with: 'swift package archive'")
45+
print("📦 You can now package with: 'swift package lambda-build'")
4446
} catch {
4547
print("🛑Failed to create the Lambda function file: \(error)")
4648
}
4749
}
48-
49-
50+
51+
5052
private func displayHelpMessage() {
5153
print(
5254
"""
5355
OVERVIEW: A SwiftPM plugin to scaffold a HelloWorld Lambda function.
54-
56+
By default, it creates a Lambda function that receives a JSON
57+
document and responds with another JSON document.
58+
5559
USAGE: swift package lambda-init
5660
[--help] [--verbose]
61+
[--with-url]
5762
[--allow-writing-to-package-directory]
58-
63+
5964
OPTIONS:
65+
--with-url Create a Lambda function exposed with an URL
6066
--allow-writing-to-package-directory Don't ask for permissions to write files.
6167
--verbose Produce verbose output for debugging.
6268
--help Show help information.
@@ -65,32 +71,50 @@ struct Initializer {
6571
}
6672
}
6773

74+
private enum TemplateType {
75+
case `default`
76+
case url
77+
78+
static func template(for type: TemplateType) -> String {
79+
switch type {
80+
case .default: return functionWithJSONTemplate
81+
case .url: return functionWithUrlTemplate
82+
}
83+
}
84+
}
85+
6886
private struct InitializerConfiguration: CustomStringConvertible {
6987
public let help: Bool
7088
public let verboseLogging: Bool
7189
public let destinationDir: URL
72-
90+
public let templateType: TemplateType
91+
7392
public init(arguments: [String]) throws {
7493
var argumentExtractor = ArgumentExtractor(arguments)
7594
let verboseArgument = argumentExtractor.extractFlag(named: "verbose") > 0
7695
let helpArgument = argumentExtractor.extractFlag(named: "help") > 0
7796
let destDirArgument = argumentExtractor.extractOption(named: "dest-dir")
78-
97+
let templateURLArgument = argumentExtractor.extractFlag(named: "with-url") > 0
98+
7999
// help required ?
80100
self.help = helpArgument
81-
101+
82102
// verbose logging required ?
83103
self.verboseLogging = verboseArgument
84104

85105
// dest dir
86106
self.destinationDir = URL(fileURLWithPath: destDirArgument[0])
107+
108+
// template type. Default is the JSON one
109+
self.templateType = templateURLArgument ? .url : .default
87110
}
88-
111+
89112
var description: String {
90113
"""
91114
{
92115
verboseLogging: \(self.verboseLogging)
93116
destinationDir: \(self.destinationDir)
117+
templateType: \(self.templateType)
94118
}
95119
"""
96120
}

Sources/AWSLambdaPluginHelper/lambda-init/Template.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,32 @@ let functionWithUrlTemplate = #"""
3131

3232
try await runtime.run()
3333
"""#
34+
35+
let functionWithJSONTemplate = #"""
36+
import AWSLambdaRuntime
37+
38+
// the data structure to represent the input parameter
39+
struct HelloRequest: Decodable {
40+
let name: String
41+
let age: Int
42+
}
43+
44+
// the data structure to represent the output response
45+
struct HelloResponse: Encodable {
46+
let greetings: String
47+
}
48+
49+
// in this example we receive a HelloRequest JSON and we return a HelloResponse JSON
50+
51+
// the Lambda runtime
52+
let runtime = LambdaRuntime {
53+
(event: HelloRequest, context: LambdaContext) in
54+
55+
HelloResponse(
56+
greetings: "Hello \(event.name). You look \(event.age > 30 ? "younger" : "older") than your age."
57+
)
58+
}
59+
60+
// start the loop
61+
try await runtime.run()
62+
"""#

0 commit comments

Comments
 (0)