Skip to content

Commit 56a0d5f

Browse files
committed
do not overwrite SAM deployment descriptor, unless --force is passed as an option
1 parent 4cc294a commit 56a0d5f

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

Examples/SAM/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ nodeploy: build
2323
swift package --disable-sandbox deploy --verbose --nodeploy --configuration debug
2424

2525
localtest:
26-
sam local invoke -t sam.json -e Tests/LambdaTests/data/apiv2.json HttpApiLambda
26+
sam local invoke -t sam.yaml -e Tests/LambdaTests/data/apiv2.json HttpApiLambda
2727

2828
list:
29-
sam list endpoints -t sam.json --stack-name swift-aws-lambda-runtime-example --output json
29+
sam list endpoints -t sam.yaml --stack-name swift-aws-lambda-runtime-example --output json
3030

3131
clean:
3232
# find . -name .build -exec rm -rf {} \;

Plugins/AWSLambdaDeployer/Plugin.swift

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct AWSLambdaPackager: CommandPlugin {
2727
}
2828

2929
// gather file paths
30-
let samDeploymentDescriptorFilePath = "\(context.package.directory)/sam.json"
30+
let samDeploymentDescriptorFilePath = "\(context.package.directory)/sam.yaml"
3131

3232
let swiftExecutablePath = try self.findExecutable(context: context,
3333
executableName: "swift",
@@ -45,6 +45,7 @@ struct AWSLambdaPackager: CommandPlugin {
4545
swiftExecutable: swiftExecutablePath,
4646
samDeploymentDescriptorFilePath: samDeploymentDescriptorFilePath,
4747
archivePath: configuration.archiveDirectory,
48+
force: configuration.force,
4849
verboseLogging: configuration.verboseLogging)
4950

5051

@@ -76,6 +77,7 @@ struct AWSLambdaPackager: CommandPlugin {
7677
swiftExecutable: Path,
7778
samDeploymentDescriptorFilePath: String,
7879
archivePath: String,
80+
force: Bool,
7981
verboseLogging: Bool) throws {
8082
print("-------------------------------------------------------------------------")
8183
print("Generating SAM deployment descriptor")
@@ -131,10 +133,19 @@ struct AWSLambdaPackager: CommandPlugin {
131133
// logLevel: configuration.verboseLogging ? .debug : .silent)
132134
try FileManager.default.removeItem(atPath: helperFilePath)
133135

134-
// write the generated SAM deployment decsriptor to disk
135-
FileManager.default.createFile(atPath: samDeploymentDescriptorFilePath,
136-
contents: samDeploymentDescriptor.data(using: .utf8))
137-
verboseLogging ? print("\(samDeploymentDescriptorFilePath)") : nil
136+
// write the generated SAM deployment descriptor to disk
137+
if FileManager.default.fileExists(atPath: samDeploymentDescriptorFilePath) && !force {
138+
139+
print("SAM deployment descriptor already exists at")
140+
print("\(samDeploymentDescriptorFilePath)")
141+
print("use --force option to overwrite it.")
142+
143+
} else {
144+
145+
FileManager.default.createFile(atPath: samDeploymentDescriptorFilePath,
146+
contents: samDeploymentDescriptor.data(using: .utf8))
147+
verboseLogging ? print("Overwriting file at \(samDeploymentDescriptorFilePath)") : nil
148+
}
138149

139150
} catch let error as DeployerPluginError {
140151
print("Error while compiling Deploy.swift")
@@ -271,23 +282,28 @@ REQUIREMENTS: To use this plugin, you must have an AWS account and have `sam` in
271282
You can install sam with the following command:
272283
(brew tap aws/tap && brew install aws-sam-cli)
273284
274-
USAGE: swift package --disable-sandbox deploy [--help] [--verbose] [--nodeploy] [--configuration <configuration>] [--archive-path <archive_path>] [--stack-name <stack-name>]
285+
USAGE: swift package --disable-sandbox deploy [--help] [--verbose]
286+
[--archive-path <archive_path>]
287+
[--configuration <configuration>]
288+
[--force] [--nodeploy] [--nolist]
289+
[--stack-name <stack-name>]
275290
276291
OPTIONS:
277292
--verbose Produce verbose output for debugging.
278-
--nodeploy Generates the JSON deployment descriptor, but do not deploy.
279-
--configuration <configuration>
280-
Build for a specific configuration.
281-
Must be aligned with what was used to build and package.
282-
Valid values: [ debug, release ] (default: debug)
283293
--archive-path <archive-path>
284294
The path where the archive plugin created the ZIP archive.
285295
Must be aligned with the value passed to archive --output-path.
286296
(default: .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager)
297+
--configuration <configuration>
298+
Build for a specific configuration.
299+
Must be aligned with what was used to build and package.
300+
Valid values: [ debug, release ] (default: debug)
301+
--force Overwrites existing SAM deployment descriptor
302+
--nodeploy Generates the JSON deployment descriptor, but do not deploy.
303+
--nolist Do not list endpoints
287304
--stack-name <stack-name>
288305
The name of the CloudFormation stack when deploying.
289306
(default: the project name)
290-
--nolist Do not list endpoints
291307
--help Show help information.
292308
""")
293309
}
@@ -298,6 +314,7 @@ private struct Configuration: CustomStringConvertible {
298314
public let help: Bool
299315
public let noDeploy: Bool
300316
public let noList: Bool
317+
public let force: Bool
301318
public let verboseLogging: Bool
302319
public let archiveDirectory: String
303320
public let stackName: String
@@ -316,6 +333,7 @@ private struct Configuration: CustomStringConvertible {
316333
let nodeployArgument = argumentExtractor.extractFlag(named: "nodeploy") > 0
317334
let verboseArgument = argumentExtractor.extractFlag(named: "verbose") > 0
318335
let noListArgument = argumentExtractor.extractFlag(named: "nolist") > 0
336+
let forceArgument = argumentExtractor.extractFlag(named: "force") > 0
319337
let configurationArgument = argumentExtractor.extractOption(named: "configuration")
320338
let archiveDirectoryArgument = argumentExtractor.extractOption(named: "archive-path")
321339
let stackNameArgument = argumentExtractor.extractOption(named: "stackname")
@@ -324,6 +342,9 @@ private struct Configuration: CustomStringConvertible {
324342
// help required ?
325343
self.help = helpArgument
326344

345+
// force overwrite the SAM deployment descriptor when it already exists
346+
self.force = forceArgument
347+
327348
// define deployment option
328349
self.noDeploy = nodeployArgument
329350

@@ -358,7 +379,7 @@ private struct Configuration: CustomStringConvertible {
358379
"invalid archive directory: \(self.archiveDirectory)\nthe directory does not exists")
359380
}
360381

361-
// infer or consume stackname
382+
// infer or consume stack name
362383
if let stackName = stackNameArgument.first {
363384
self.stackName = stackName
364385
} else {

0 commit comments

Comments
 (0)