|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | +// snippet-start:[swift.sfn.hello] |
| 5 | +// An example that shows how to use the AWS SDK for Swift to perform a simple |
| 6 | +// operation using Amazon Elastic Compute Cloud (EC2). |
| 7 | +// |
| 8 | + |
| 9 | +import ArgumentParser |
| 10 | +import Foundation |
| 11 | + |
| 12 | +// snippet-start:[swift.sfn.import] |
| 13 | +import AWSSFN |
| 14 | +// snippet-end:[swift.sfn.import] |
| 15 | + |
| 16 | +struct ExampleCommand: ParsableCommand { |
| 17 | + @Option(help: "The AWS Region to run AWS API calls in.") |
| 18 | + var awsRegion = "us-east-1" |
| 19 | + |
| 20 | + static var configuration = CommandConfiguration( |
| 21 | + commandName: "hello-sfn", |
| 22 | + abstract: """ |
| 23 | + Demonstrates a simple operation using AWS Step Functions. |
| 24 | + """, |
| 25 | + discussion: """ |
| 26 | + An example showing how to make a call to AWS Step Functions using the |
| 27 | + AWS SDK for Swift. |
| 28 | + """ |
| 29 | + ) |
| 30 | + |
| 31 | + /// Called by ``main()`` to run the bulk of the example. |
| 32 | + func runAsync() async throws { |
| 33 | + let sfnConfig = try await SFNClient.SFNClientConfiguration(region: awsRegion) |
| 34 | + let sfnClient = SFNClient(config: sfnConfig) |
| 35 | + |
| 36 | + do { |
| 37 | + let output = try await sfnClient.listStateMachines( |
| 38 | + input: ListStateMachinesInput( |
| 39 | + maxResults: 10 |
| 40 | + ) |
| 41 | + ) |
| 42 | + |
| 43 | + guard let stateMachines = output.stateMachines else { |
| 44 | + print("*** No state machines found.") |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + print("Found \(stateMachines.count) state machines (capped to 10)...") |
| 49 | + for machine in stateMachines { |
| 50 | + print(" \(machine.name ?? "<unnamed>"): \(machine.stateMachineArn ?? "<unknown>")") |
| 51 | + } |
| 52 | + } catch { |
| 53 | + print("*** Error fetching state machine list: \(error.localizedDescription)") |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/// The program's asynchronous entry point. |
| 59 | +@main |
| 60 | +struct Main { |
| 61 | + static func main() async { |
| 62 | + let args = Array(CommandLine.arguments.dropFirst()) |
| 63 | + |
| 64 | + do { |
| 65 | + let command = try ExampleCommand.parse(args) |
| 66 | + try await command.runAsync() |
| 67 | + } catch { |
| 68 | + ExampleCommand.exit(withError: error) |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | +// snippet-end:[swift.sfn.hello] |
0 commit comments