Skip to content

Commit c3a749a

Browse files
author
Imhoertha Ojior
committed
Fix argument parsing in LoadMaxValues.java
1 parent 64e076d commit c3a749a

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

scripts/PrintDistinctPKs/LoadMaxValues/java/src/main/java/org/example/LoadMaxValues.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import software.amazon.awssdk.services.sts.StsClient;
88
import software.amazon.awssdk.services.sts.model.GetCallerIdentityRequest;
99
import software.amazon.awssdk.services.sts.model.GetCallerIdentityResponse;
10+
import software.amazon.awssdk.regions.Region;
1011

1112

1213
import java.util.Arrays;
@@ -22,13 +23,14 @@ public class LoadMaxValues {
2223
private static AttributeValue MAX_SORT_KEY_VALUE_B;
2324

2425
private static DynamoDbClient dynamoDb;
26+
private static Region awsRegion;
2527

2628
public static void main(String[] args) {
2729

2830
String tableName;
2931
String region;
3032

31-
if (args.length != 2 || !args[2].equals("--region")) {
33+
if (args.length != 2 || !args[0].equals("--region")) {
3234
System.out.println("Error: --region param not passed, checking AWS_DEFAULT_REGION environment variable.");
3335

3436
// If they didn't pass the table name and region on the command line, see if they
@@ -38,32 +40,31 @@ public static void main(String[] args) {
3840
if (region == null || region.isEmpty()) {
3941
System.out.println("Error: AWS_DEFAULT_REGION environment variable is not set.");
4042
System.exit(1);
41-
} else {
42-
software.amazon.awssdk.regions.Region awsRegion = null;
43-
44-
try {
45-
awsRegion = software.amazon.awssdk.regions.Region.of(region);
46-
} catch (IllegalArgumentException e) {
47-
System.out.println("Error: Invalid AWS region specified in AWS_DEFAULT_REGION.");
48-
System.exit(1);
49-
}
50-
51-
dynamoDb = DynamoDbClient.builder().region(awsRegion).build();
52-
53-
// Validate region by making an API call to AWS STS service
54-
try {
55-
StsClient stsClient = StsClient.builder().region(awsRegion).build();
56-
GetCallerIdentityRequest request = GetCallerIdentityRequest.builder().build();
57-
GetCallerIdentityResponse response = stsClient.getCallerIdentity(request);
58-
System.out.println("Region is valid. Account ID: " + response.account());
59-
} catch (SdkException e) {
60-
System.out.println("Error: Unable to validate region. Check your AWS credentials and region.");
61-
System.exit(1);
62-
}
6343
}
6444
} else {
65-
region = args[1];
45+
region = args[1];
46+
}
47+
48+
try {
49+
awsRegion = Region.of(region);
50+
} catch (IllegalArgumentException e) {
51+
System.out.println("Error: Invalid AWS region specified in AWS_DEFAULT_REGION.");
52+
System.exit(1);
53+
}
54+
55+
// Validate region by making an API call to AWS STS service
56+
try {
57+
StsClient stsClient = StsClient.builder().region(awsRegion).build();
58+
GetCallerIdentityRequest request = GetCallerIdentityRequest.builder().build();
59+
GetCallerIdentityResponse response = stsClient.getCallerIdentity(request);
60+
System.out.println("Region is valid. Account ID: " + response.account());
61+
} catch (SdkException e) {
62+
System.out.println("Error: Unable to validate region. Check your AWS credentials and region.");
63+
System.exit(1);
6664
}
65+
66+
Initialize DynamoDB client.
67+
dynamoDb = DynamoDbClient.builder().region(awsRegion).build();
6768

6869
// We need to create a string that is encoded in UTF-8 to 1024 bytes of the highest
6970
// code point. This is 256 code points. Each code point is a 4 byte value in UTF-8.

0 commit comments

Comments
 (0)