mirror of
https://github.com/swift-server/swift-aws-lambda-runtime.git
synced 2026-06-02 07:27:33 +00:00
add --with-url option
This commit is contained in:
@@ -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)
|
||||
}
|
||||
"""
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
"""#
|
||||
|
||||
Reference in New Issue
Block a user