From 06654302d57c075247e4b68290ef8cb138d4c953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Stormacq?= Date: Sun, 24 Nov 2024 20:40:18 +0100 Subject: [PATCH] add --with-url option --- .../lambda-init/Initializer.swift | 54 +++++++++++++------ .../lambda-init/Template.swift | 29 ++++++++++ 2 files changed, 68 insertions(+), 15 deletions(-) diff --git a/Sources/AWSLambdaPluginHelper/lambda-init/Initializer.swift b/Sources/AWSLambdaPluginHelper/lambda-init/Initializer.swift index 8515d586..02954317 100644 --- a/Sources/AWSLambdaPluginHelper/lambda-init/Initializer.swift +++ b/Sources/AWSLambdaPluginHelper/lambda-init/Initializer.swift @@ -21,42 +21,48 @@ import Foundation struct Initializer { private let destFileName = "Sources/main.swift" - + func initialize(arguments: [String]) async throws { let configuration = try InitializerConfiguration(arguments: arguments) - + if configuration.help { self.displayHelpMessage() return } - + let destFileURL = configuration.destinationDir.appendingPathComponent(destFileName) do { - try functionWithUrlTemplate.write(to: destFileURL, atomically: true, encoding: .utf8) - + + let template = TemplateType.template(for: configuration.templateType) + try template.write(to: destFileURL, atomically: true, encoding: .utf8) + if configuration.verboseLogging { print("File created at: \(destFileURL)") } - + print("✅ Lambda function written to \(destFileName)") - print("📦 You can now package with: 'swift package archive'") + print("📦 You can now package with: 'swift package lambda-build'") } catch { print("🛑Failed to create the Lambda function file: \(error)") } } - - + + private func displayHelpMessage() { print( """ OVERVIEW: A SwiftPM plugin to scaffold a HelloWorld Lambda function. - + By default, it creates a Lambda function that receives a JSON + document and responds with another JSON document. + USAGE: swift package lambda-init [--help] [--verbose] + [--with-url] [--allow-writing-to-package-directory] - + OPTIONS: + --with-url Create a Lambda function exposed with an URL --allow-writing-to-package-directory Don't ask for permissions to write files. --verbose Produce verbose output for debugging. --help Show help information. @@ -65,32 +71,50 @@ struct Initializer { } } +private enum TemplateType { + case `default` + case url + + static func template(for type: TemplateType) -> String { + switch type { + case .default: return functionWithJSONTemplate + case .url: return functionWithUrlTemplate + } + } +} + private struct InitializerConfiguration: CustomStringConvertible { public let help: Bool public let verboseLogging: Bool public let destinationDir: URL - + public let templateType: TemplateType + public init(arguments: [String]) throws { var argumentExtractor = ArgumentExtractor(arguments) let verboseArgument = argumentExtractor.extractFlag(named: "verbose") > 0 let helpArgument = argumentExtractor.extractFlag(named: "help") > 0 let destDirArgument = argumentExtractor.extractOption(named: "dest-dir") - + let templateURLArgument = argumentExtractor.extractFlag(named: "with-url") > 0 + // help required ? self.help = helpArgument - + // verbose logging required ? self.verboseLogging = verboseArgument // dest dir self.destinationDir = URL(fileURLWithPath: destDirArgument[0]) + + // template type. Default is the JSON one + self.templateType = templateURLArgument ? .url : .default } - + var description: String { """ { verboseLogging: \(self.verboseLogging) destinationDir: \(self.destinationDir) + templateType: \(self.templateType) } """ } diff --git a/Sources/AWSLambdaPluginHelper/lambda-init/Template.swift b/Sources/AWSLambdaPluginHelper/lambda-init/Template.swift index 0ca37ace..865a122a 100644 --- a/Sources/AWSLambdaPluginHelper/lambda-init/Template.swift +++ b/Sources/AWSLambdaPluginHelper/lambda-init/Template.swift @@ -31,3 +31,32 @@ let functionWithUrlTemplate = #""" try await runtime.run() """# + +let functionWithJSONTemplate = #""" + import AWSLambdaRuntime + + // the data structure to represent the input parameter + struct HelloRequest: Decodable { + let name: String + let age: Int + } + + // the data structure to represent the output response + struct HelloResponse: Encodable { + let greetings: String + } + + // in this example we receive a HelloRequest JSON and we return a HelloResponse JSON + + // the Lambda runtime + let runtime = LambdaRuntime { + (event: HelloRequest, context: LambdaContext) in + + HelloResponse( + greetings: "Hello \(event.name). You look \(event.age > 30 ? "younger" : "older") than your age." + ) + } + + // start the loop + try await runtime.run() + """#