Skip to content

Commit

Permalink
Fix logging, and write staging repository IDs to files
Browse files Browse the repository at this point in the history
Fix: #7
Fix: #6
  • Loading branch information
io7m committed Apr 18, 2021
1 parent 46d72ea commit 5ca4b88
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 38 deletions.
16 changes: 15 additions & 1 deletion README-CHANGES.xml
@@ -1,11 +1,25 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<c:changelog project="com.io7m.brooklime" xmlns:c="urn:com.io7m.changelog:4.0">
<c:releases>
<c:release date="2021-01-01T16:16:54+00:00" is-open="false" ticket-system="com.github.io7m.brooklime" version="1.0.0">
<c:release date="2021-01-01T00:00:00+00:00" is-open="false" ticket-system="com.github.io7m.brooklime" version="1.0.0">
<c:changes>
<c:change date="2021-01-01T00:00:00+00:00" summary="Initial public release"/>
</c:changes>
</c:release>
<c:release date="2021-04-18T10:07:08+00:00" is-open="true" ticket-system="com.github.io7m.brooklime" version="1.1.0">
<c:changes>
<c:change date="2021-04-18T00:00:00+00:00" summary="Fix logging">
<c:tickets>
<c:ticket id="6"/>
</c:tickets>
</c:change>
<c:change date="2021-04-18T10:07:08+00:00" summary="Add the ability write staging repository IDs to a file">
<c:tickets>
<c:ticket id="7"/>
</c:tickets>
</c:change>
</c:changes>
</c:release>
</c:releases>
<c:ticket-systems>
<c:ticket-system default="true" id="com.github.io7m.brooklime" url="https://www.github.com/io7m/brooklime/issues/"/>
Expand Down
Expand Up @@ -22,12 +22,22 @@
import com.io7m.brooklime.api.BLNexusClientProviderType;
import com.io7m.brooklime.api.BLNexusClientType;
import com.io7m.brooklime.api.BLStagingRepositoryCreate;
import org.apache.commons.io.output.CloseShieldOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.PrintStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;

import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
import static java.nio.file.StandardOpenOption.WRITE;

@Parameters(commandDescription = "Create a staging repository")
public final class BLCommandCreateStagingRepository extends BLCommandRoot
{
Expand Down Expand Up @@ -83,6 +93,13 @@ public final class BLCommandCreateStagingRepository extends BLCommandRoot
)
private int retryCount = 25;

@Parameter(
names = "--outputFile",
description = "The output file that will contain the staging repository ID",
required = false
)
private Path outputFile;

public BLCommandCreateStagingRepository()
{

Expand All @@ -109,19 +126,40 @@ public Status execute()
.setRetryDelay(Duration.ofSeconds(this.retrySeconds))
.build();

try (BLNexusClientType client = clients.createClient(clientConfiguration)) {
BLChatter.getInstance().start();
try (PrintStream output = this.outputStream()) {
try (BLNexusClientType client = clients.createClient(clientConfiguration)) {
BLChatter.getInstance().start();

final String repository =
client.stagingRepositoryCreate(
BLStagingRepositoryCreate.builder()
.setDescription(this.stagingRepositoryDescription)
.build()
);
final String repository =
client.stagingRepositoryCreate(
BLStagingRepositoryCreate.builder()
.setDescription(this.stagingRepositoryDescription)
.build()
);

System.out.println(repository);
output.println(repository);
output.flush();
}
}

return Status.SUCCESS;
}

private PrintStream outputStream()
throws IOException
{
if (this.outputFile != null) {
return new PrintStream(
Files.newOutputStream(this.outputFile, CREATE, TRUNCATE_EXISTING, WRITE),
false,
StandardCharsets.UTF_8.name()
);
}

return new PrintStream(
new CloseShieldOutputStream(System.out),
false,
StandardCharsets.UTF_8.name()
);
}
}
32 changes: 4 additions & 28 deletions com.io7m.brooklime.cmdline/src/main/resources/logback.xml
Expand Up @@ -2,44 +2,20 @@

<configuration debug="false">

<appender name="STDERR"
class="ch.qos.logback.core.ConsoleAppender">
<appender
name="STDERR"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>brooklime: %logger{128}: %level: %msg%n</pattern>
</encoder>
<target>System.err</target>
</appender>

<appender name="ROLLING_FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>brooklime.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- Keep 30 days' worth of history capped at 1GB total size -->
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>

<encoder>
<pattern>%d{yyyy-MM-dd'T'HH:mm:ss Z} [%thread] %-5level %logger{128} - %msg%n</pattern>
</encoder>
</appender>

<appender name="ASYNC_FILE"
class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="ROLLING_FILE"/>
</appender>

<logger name="io.netty"
level="INFO"/>

<!--
<logger name="org.apache.hc.client5.http.wire"
level="INFO"/>
-->

<root level="INFO">
<root level="TRACE">
<appender-ref ref="STDERR"/>
<appender-ref ref="ASYNC_FILE"/>
</root>

</configuration>
Expand Up @@ -64,6 +64,12 @@
<Cell>false</Cell>
<Cell>The number of seconds to pause between retrying failed HTTP requests.</Cell>
</Row>
<Row>
<Cell><Term type="parameter">--outputFile</Term></Cell>
<Cell>Path</Cell>
<Cell>false</Cell>
<Cell>The output file that will contain the staging repository ID.</Cell>
</Row>
</Table>
</FormalItem>
</Subsection>
Expand Down
Expand Up @@ -33,6 +33,8 @@
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -303,6 +305,63 @@ public void testCreateStagingRepository()
});
}

/**
* Creating staging repositories works if the server returns the right data.
*/

@Test
public void testCreateStagingRepositoryOutput()
throws Exception
{
MOCK_SERVER.when(
HttpRequest.request()
.withPath(
"/service/local/staging/profiles/88536b02-fb30-4ee3-9831-0c5b290bd913/start")
).respond(
HttpResponse.response()
.withStatusCode(Integer.valueOf(201))
.withBody(
"<promoteResponse><data><stagedRepositoryId>r0</stagedRepositoryId></data></promoteResponse>")
);

MOCK_SERVER.when(
HttpRequest.request()
.withPath("/service/local/staging/repository/r0")
).respond(
HttpResponse.response()
.withStatusCode(Integer.valueOf(200))
.withBody(resourceBytesOf(
this.directory,
"stagingRepositoryCreated0.xml"))
);

final Path outputFile =
this.directory.resolve("repository.txt");

MainExitless.main(new String[]{
"create",
"--user",
"user",
"--password",
"pass",
"--stagingProfileId",
"88536b02-fb30-4ee3-9831-0c5b290bd913",
"--verbose",
"trace",
"--description",
"An example repository.",
"--baseURI",
this.serverAddress.toString(),
"--outputFile",
outputFile.toString()
});

Assertions.assertEquals(
"r0",
new String(Files.readAllBytes(outputFile), StandardCharsets.UTF_8).trim()
);
}

/**
* Showing staging repositories works if the server returns the right data.
*/
Expand Down

0 comments on commit 5ca4b88

Please sign in to comment.