-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathentry.swift
More file actions
116 lines (96 loc) · 3.54 KB
/
Copy pathentry.swift
File metadata and controls
116 lines (96 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//
// An example demonstrating how to set up and use an Amazon Simple Queue
// Service client to delete messages from an Amazon SQS queue.
import ArgumentParser
import AWSClientRuntime
import AWSSQS
import Foundation
struct ExampleCommand: ParsableCommand {
@Option(help: "The URL of the Amazon SQS queue from which to delete messages")
var queue: String
@Argument(help: "Receipt handle(s) of the message(s) to delete")
var handles: [String]
@Option(help: "Name of the Amazon Region to use (default: us-east-1)")
var region = "us-east-1"
static var configuration = CommandConfiguration(
commandName: "deletemessages",
abstract: """
This example shows how to delete a batch of messages from an Amazon SQS queue.
""",
discussion: """
"""
)
/// Called by ``main()`` to run the bulk of the example.
func runAsync() async throws {
// snippet-start:[swift.sqs.DeleteMessageBatch]
let config = try await SQSClient.SQSClientConfiguration(region: region)
let sqsClient = SQSClient(config: config)
// Create the list of message entries.
var entries: [SQSClientTypes.DeleteMessageBatchRequestEntry] = []
var messageNumber = 1
for handle in handles {
let entry = SQSClientTypes.DeleteMessageBatchRequestEntry(
id: "\(messageNumber)",
receiptHandle: handle
)
entries.append(entry)
messageNumber += 1
}
// Delete the messages.
let output = try await sqsClient.deleteMessageBatch(
input: DeleteMessageBatchInput(
entries: entries,
queueUrl: queue
)
)
// Get the lists of failed and successful deletions from the output.
guard let failedEntries = output.failed else {
print("Failed deletion list is missing!")
return
}
guard let successfulEntries = output.successful else {
print("Successful deletion list is missing!")
return
}
// Display a list of the failed deletions along with their
// corresponding explanation messages.
if failedEntries.count != 0 {
print("Failed deletions:")
for entry in failedEntries {
print("Message #\(entry.id ?? "<unknown>") failed: \(entry.message ?? "<unknown>")")
}
} else {
print("No failed deletions.")
}
// Output a list of the message numbers that were successfully deleted.
if successfulEntries.count != 0 {
var successes = ""
for entry in successfulEntries {
if successes.count == 0 {
successes = entry.id ?? "<unknown>"
} else {
successes = "\(successes), \(entry.id ?? "<unknown>")"
}
}
print("Succeeded: ", successes)
} else {
print("No successful deletions.")
}
// snippet-end:[swift.sqs.DeleteMessageBatch]
}
}
/// The program's asynchronous entry point.
@main
struct Main {
static func main() async {
let args = Array(CommandLine.arguments.dropFirst())
do {
let command = try ExampleCommand.parse(args)
try await command.runAsync()
} catch {
ExampleCommand.exit(withError: error)
}
}
}