Skip to content

Commit

Permalink
chore: Json printing inconsistencies (#1)
Browse files Browse the repository at this point in the history
* fix: Fixed some inconsistencies when printing to the console with the --json option.

* fix: will now give more info if the script file failed to be deleted

* chore: set version 2.5.8
  • Loading branch information
oscarostrand committed Mar 28, 2023
1 parent 92b4b01 commit ba0db97
Show file tree
Hide file tree
Showing 30 changed files with 64 additions and 86 deletions.
2 changes: 1 addition & 1 deletion src/main/java/se/attini/AwsAccountFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public AwsAccountFacade(AwsClientFactory awsClientFactory,
public String getAccount() {
return accountCache.computeIfAbsent(globalConfig.getProfile().orElse(Profile.create("default")),
profile ->{
try(StsClient stsClient = awsClientFactory.stsClient();) {
try(StsClient stsClient = awsClientFactory.stsClient()) {
return stsClient.getCallerIdentity()
.account();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/se/attini/DistributionDataFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Optional<DistData> getDistributionData(Environment environment,
DistributionName distributionName) {

String s = environment.getName().getName() + "-" + distributionName.getName();
try( DynamoDbClient dynamoDbClient = awsClientFactory.dynamoClient();) {
try( DynamoDbClient dynamoDbClient = awsClientFactory.dynamoClient()) {
Map<String, AttributeValue> item = dynamoDbClient
.getItem(GetItemRequest.builder().tableName(
"AttiniResourceStatesV1")
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/se/attini/cli/AttiniCliCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static void main(String[] args) {
public static class VersionProvider implements CommandLine.IVersionProvider {
public final static int MAJOR = 2;
public final static int MINOR = 5;
public final static int PATCH = 7;
public final static int PATCH = 8;

public static final String VERSION_STRING = MAJOR + "." + MINOR + "." + PATCH;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/se/attini/cli/ConsolePrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public ConsolePrinter(ObjectMapperFactory objectMapperFactory, GlobalConfig glob
public void print(PrintItem message) {
switch (message.getPrintType()) {
case ERROR -> System.err.println(PrintUtil.toRed(message.getMessage()));
case SUCCESS -> System.out.println(PrintUtil.toRed(message.getMessage()));
case SUCCESS -> System.out.println(PrintUtil.toGreen(message.getMessage()));
case NORMAL_SAME_LINE -> System.out.print('\r' + message.getMessage() + " ".repeat(20));
case NEW_LINE -> System.out.println();
default -> System.out.println(message.getMessage());
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/se/attini/cli/PrintItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ public static PrintItem message(PrintType printType, String message){
public enum PrintType{
SUCCESS,
ERROR,
ERROR_DEBUG,
NORMAL,
NORMAL_NO_TIMESTAMP,
NORMAL_SAME_LINE,
NEW_LINE
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/se/attini/cli/RemoveStackService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest;
import software.amazon.awssdk.services.sts.StsClient;

public class RemoveStackService {
private final AwsClientFactory awsClientFactory;
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/se/attini/cli/deployment/DataEmitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public class DataEmitter {


public DataEmitter(GlobalConfig globalConfig,
ObjectMapper objectMapper, ConsolePrinter consolePrinter) {
ObjectMapper objectMapper,
ConsolePrinter consolePrinter) {
this.globalConfig = requireNonNull(globalConfig, "globalConfig");
this.objectMapper = requireNonNull(objectMapper, "objectMapper");
this.consolePrinter = requireNonNull(consolePrinter, "consolePrinter");
Expand All @@ -34,10 +35,6 @@ public void emitNewLine() {
}
}

public ObjectMapper getObjectMapper() {
return objectMapper;
}

public void emitKeyValueSameLine(String key, String value, PrintUtil.Color color) {
if (globalConfig.printAsJson()) {
ObjectNode objectNode = objectMapper.createObjectNode();
Expand All @@ -61,9 +58,21 @@ public void emitString(String value) {
} else {
consolePrinter.print(message(value));
}
}

public void emitPrintItem(PrintItem printItem) {
if (globalConfig.printAsJson()) {
ObjectNode objectNode = objectMapper.createObjectNode()
.put("timestamp", Instant.now().toEpochMilli())
.put("type", "string")
.put("data", printItem.getMessage().trim());
consolePrinter.print(message(objectNode.toString()));
} else {
consolePrinter.print(printItem);
}
}


public void emitKeyValue(String key, String value) {
emitKeyValue(key, value, PrintUtil.Color.DEFAULT);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/se/attini/cli/deployment/DeploymentData.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

@Introspected
@ReflectiveAccess
@SuppressWarnings("unused")
public class DeploymentData {

private final String distributionName;
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/se/attini/cli/deployment/RollBackCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import static java.util.Objects.requireNonNull;
import static se.attini.cli.ConsolePrinter.ErrorPrintType.TEXT;

import jakarta.inject.Inject;
import picocli.CommandLine;
Expand All @@ -12,6 +13,7 @@
import se.attini.cli.ErrorResolver;
import se.attini.cli.PrintItem;
import se.attini.cli.global.DebugOption;
import se.attini.cli.global.JsonOption;
import se.attini.cli.global.RegionAndProfileOption;
import se.attini.deployment.DeployDistributionRequest;
import se.attini.deployment.DeployDistributionResponse;
Expand All @@ -36,6 +38,9 @@ public class RollBackCommand implements Runnable {
@CommandLine.Mixin
private DebugOption debugOption;

@CommandLine.Mixin
private JsonOption jsonOption;

@Option(names = {"--distribution-name", "-n"}, description = "Specify a name of a distribution. Required.", required = true)
private DistributionName distributionName;

Expand Down Expand Up @@ -91,13 +96,16 @@ public void run() {
.build());


consolePrinter.print(PrintItem.successMessage("Distribution successfully redeployed"));
} catch (DeploymentAbortedException e) {
consolePrinter.print(PrintItem.message(e.getMessage()));
} catch (Exception e) {
CliError resolve = ErrorResolver.resolve(e);
consolePrinter.printError(resolve);
System.exit(resolve.getErrorCode().getExitCode());
CliError cliError = ErrorResolver.resolve(e);
if (jsonOption.printAsJson()) {
consolePrinter.printError(cliError);
} else {
consolePrinter.printError(cliError, TEXT);
}
System.exit(cliError.getErrorCode().getExitCode());
}

}
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/se/attini/config/BeanFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,12 @@ public DataEmitter dataEmitter(GlobalConfig globalConfig, ObjectMapper objectMap
public SetupAttiniService setupAttiniService(AwsClientFactory awsClientFactory,
ProfileFacade profileFacade,
UserInputReader userInputReader,
DataEmitter dataEmitter,
ConsolePrinter consolePrinter) {
DataEmitter dataEmitter) {
return new SetupAttiniService(awsClientFactory,
profileFacade,
userInputReader,
new GuidedSetup(userInputReader),
dataEmitter,
consolePrinter);
dataEmitter);
}

@Singleton
Expand Down
1 change: 1 addition & 0 deletions src/main/java/se/attini/context/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

@Introspected
@ReflectiveAccess
@SuppressWarnings("unused")
public class Context {
public final String attiniVersion;
public final String account;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/se/attini/context/DeploymentPlanContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

@Introspected
@ReflectiveAccess
@SuppressWarnings("unused")
public class DeploymentPlanContext {
private final String name;
private final String lastStatus;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/se/attini/context/DistributionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

@Introspected
@ReflectiveAccess
@SuppressWarnings("unused")
public class DistributionContext {
private final String name;
private final String distributionId;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/se/attini/context/EnvironmentContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

@Introspected
@ReflectiveAccess
@SuppressWarnings("unused")
public class EnvironmentContext {
private final String environmentName;
private final List<String> warnings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,6 @@ public Builder setDistributionName(DistributionName distributionName) {
return this;
}

public Builder of(FollowDeploymentRequest followDeploymentRequest) {
this.environment = followDeploymentRequest.environment;
this.objectIdentifier = followDeploymentRequest.objectIdentifier;
this.distributionName = followDeploymentRequest.distributionName;
return this;
}

public FollowDeploymentRequest build() {
return new FollowDeploymentRequest(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private void pollDeploymentPlanCloudFormation(StackName stackName,

waitFor(3000);
//TODO above wait is a temp workaround that is not very good.
//Things like throtelling could cause the template to take considerably longer.
//Things like throttling could cause the template to take considerably longer.
//In order to handle this the deploy origin lambda will need to report the status
//of the template deployment so that the CLI can react to it.
StackStatus stackStatus = getStackStatusWithRetry(stackName, cloudFormationClient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public Optional<DistributionId> getDistributionId(Path path) {

/**
* Will return an instance of AttiniConfigFile e if present in the provided zip archive.
* Will throw an exception if no file is present or more then one file is present
* Will throw an exception if no file is present, or more than one file is present
*
* @param file a zipped distribution
* @return AttiniConfigFile
Expand Down
25 changes: 4 additions & 21 deletions src/main/java/se/attini/domain/Deployment.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
public class Deployment {

private final Distribution distribution;
private final Region region;
private final EnvironmentName environment;
private final Instant deployTime;
private final DeploymentError deploymentError;
Expand All @@ -32,7 +31,6 @@ public class Deployment {

private Deployment(Builder builder) {
this.distribution = builder.distribution;
this.region = builder.region;
this.environment = builder.environment;
this.deployTime = builder.deployTime;
this.deploymentError = builder.deploymentError;
Expand All @@ -57,10 +55,6 @@ public Distribution getDistribution() {
return distribution;
}

public Optional<Region> getRegion() {
return Optional.ofNullable(region);
}

public Optional<DeploymentError> getDeploymentError() {
return Optional.ofNullable(deploymentError);
}
Expand Down Expand Up @@ -120,12 +114,10 @@ public boolean equals(Object o) {
Deployment that = (Deployment) o;
return initStackUnchanged == that.initStackUnchanged && Objects.equals(distribution,
that.distribution) && Objects.equals(
region,
that.region) && Objects.equals(environment, that.environment) && Objects.equals(
deployTime,
that.deployTime) && Objects.equals(deploymentError,
that.deploymentError) && Objects.equals(stackName,
that.stackName) && Objects.equals(
environment,
that.environment) && Objects.equals(deployTime, that.deployTime) && Objects.equals(
deploymentError,
that.deploymentError) && Objects.equals(stackName, that.stackName) && Objects.equals(
executionArn,
that.executionArn) && Objects.equals(deploymentPlanCount,
that.deploymentPlanCount) && Objects.equals(
Expand All @@ -142,7 +134,6 @@ public boolean equals(Object o) {
@Override
public int hashCode() {
return Objects.hash(distribution,
region,
environment,
deployTime,
deploymentError,
Expand All @@ -162,7 +153,6 @@ public int hashCode() {
public String toString() {
return "Deployment{" +
"distribution=" + distribution +
", region=" + region +
", environment=" + environment +
", deployTime=" + deployTime +
", deploymentError=" + deploymentError +
Expand All @@ -181,7 +171,6 @@ public String toString() {

public static class Builder {
private Distribution distribution;
private Region region;
private EnvironmentName environment;
private Instant deployTime;
private DeploymentError deploymentError;
Expand Down Expand Up @@ -224,12 +213,6 @@ public Builder setDistribution(Distribution distribution) {
this.distribution = distribution;
return this;
}

public Builder setRegion(Region region) {
this.region = region;
return this;
}

public Builder setEnvironment(EnvironmentName environment) {
this.environment = environment;
return this;
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/se/attini/domain/DeploymentPlanCount.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ public int getValue() {
return value;
}

public static DeploymentPlanCount create(int value){
return new DeploymentPlanCount(value);
}

public static DeploymentPlanCount create(String value){
try {
return new DeploymentPlanCount(Integer.parseInt(value));
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/se/attini/domain/Distribution.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@ public Builder setDistributionId(DistributionId distributionId) {
return this;
}

public Builder of(Distribution distribution) {
this.distributionName = distribution.distributionName;
this.distributionId = distribution.distributionId;
return this;
}

public Distribution build() {
return new Distribution(this);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/se/attini/domain/LogLevel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package se.attini.domain;

@SuppressWarnings("unused")
public enum LogLevel {
ALL,
TRACE,
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/se/attini/domain/Region.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ public String getName() {
return name;
}

public software.amazon.awssdk.regions.Region toAwsRegion(){
return software.amazon.awssdk.regions.Region.of(name);
}

public static Region create(String name){
return new Region(name);
}
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/se/attini/pack/LifeCycleHooksService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -117,15 +118,22 @@ private void runCommands(ArrayNode arrayNode,
giveExecuteAccess(file.toPath());

runScriptFile(file, emitter);
if (!file.delete()) {
emitter.emitString("Could not delete script file");
}

deleteFile(file);

} catch (IOException e) {
throw new AttiniFileSystemException("Could not run commands", e);
}
}

private void deleteFile(File file){
try {
Files.delete(file.toPath());
} catch (IOException e) {
emitter.emitString("Could not delete script file, reason: " + e.getMessage());
}
}

private void giveExecuteAccess(Path file) {

String command = chmodFileCommand(file.toString());
Expand Down
1 change: 1 addition & 0 deletions src/main/java/se/attini/pack/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

@Introspected
@ReflectiveAccess
@SuppressWarnings("unused")
public class Metadata {

private final Set<String> usedRunners;
Expand Down

0 comments on commit ba0db97

Please sign in to comment.