Skip to content

Commit

Permalink
Add initial support for CLI structured messages
Browse files Browse the repository at this point in the history
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
wadimw committed Nov 20, 2023
1 parent 661de36 commit 5739fce
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
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
}
}
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);
}
}
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;
}
}
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()));
}
}

0 comments on commit 5739fce

Please sign in to comment.