-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial support for CLI structured messages
Added separate writer for outputting structured messages from CLI. Currently, is only able to write to stdout. This can be used in combination with l10n.consoleWriter.consoleOutputType=NONE to achieve parseable output from CLI commands (useful for scripting with a wrapper).
- Loading branch information
Showing
4 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
cli/src/main/java/com/box/l10n/mojito/cli/structuredoutput/StructuredWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.box.l10n.mojito.cli.structuredoutput; | ||
|
||
import org.json.simple.JSONObject; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Writes structured messages to given output. | ||
*/ | ||
public class StructuredWriter { | ||
/** | ||
* logger | ||
*/ | ||
static Logger logger = LoggerFactory.getLogger(StructuredWriter.class); | ||
|
||
OutputType outputType; | ||
|
||
public StructuredWriter(OutputType outputType) { | ||
this.outputType = outputType; | ||
} | ||
|
||
|
||
/** | ||
* Write a message to output | ||
*/ | ||
public <T extends JSONObject> void write(T message) { | ||
String serializedMessage = message.toJSONString(); | ||
logger.trace(serializedMessage); | ||
|
||
if (outputType == OutputType.STDOUT) { | ||
System.out.println(serializedMessage); | ||
} | ||
} | ||
|
||
public enum OutputType { | ||
NONE, STDOUT | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
cli/src/main/java/com/box/l10n/mojito/cli/structuredoutput/StructuredWriterConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.box.l10n.mojito.cli.structuredoutput; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Primary; | ||
|
||
/** | ||
* Configuration for {@link StructuredWriter}. | ||
* | ||
* @author jaurambault | ||
*/ | ||
@Configuration | ||
public class StructuredWriterConfig { | ||
@Primary | ||
@Bean | ||
public StructuredWriter structuredWriter(StructuredWriterConfigurationProperties structuredWriterConfigurationProperties) { | ||
return new StructuredWriter(structuredWriterConfigurationProperties.outputType); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...ava/com/box/l10n/mojito/cli/structuredoutput/StructuredWriterConfigurationProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.box.l10n.mojito.cli.structuredoutput; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@ConfigurationProperties("l10n.structuredWriter") | ||
public class StructuredWriterConfigurationProperties { | ||
StructuredWriter.OutputType outputType = StructuredWriter.OutputType.NONE; | ||
|
||
public StructuredWriter.OutputType getOutputType() { | ||
return outputType; | ||
} | ||
|
||
public void setOutputType(StructuredWriter.OutputType outputType) { | ||
this.outputType = outputType; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
cli/src/test/java/com/box/l10n/mojito/cli/structuredoutput/StructuredWriterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.box.l10n.mojito.cli.structuredoutput; | ||
|
||
import org.json.simple.JSONObject; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.test.OutputCapture; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class StructuredWriterTest { | ||
|
||
/** | ||
* logger | ||
*/ | ||
static Logger logger = LoggerFactory.getLogger(StructuredWriterTest.class); | ||
|
||
@Rule | ||
public OutputCapture outputCapture = new OutputCapture(); | ||
|
||
@Test | ||
public void outputStdout() { | ||
StructuredWriter structuredWriter = new StructuredWriter(StructuredWriter.OutputType.STDOUT); | ||
|
||
JSONObject message = new JSONObject(); | ||
message.put("foo", "bar"); | ||
message.put("baz", 1); | ||
|
||
structuredWriter.write(message); | ||
|
||
assertTrue(outputCapture.toString().contains(message.toJSONString())); | ||
} | ||
|
||
@Test | ||
public void disableOutput() { | ||
StructuredWriter structuredWriter = new StructuredWriter(StructuredWriter.OutputType.NONE); | ||
|
||
JSONObject message = new JSONObject(); | ||
message.put("foo", "bar"); | ||
message.put("baz", 1); | ||
|
||
structuredWriter.write(message); | ||
|
||
assertFalse(outputCapture.toString().contains(message.toJSONString())); | ||
} | ||
} |