Implement CLI framework with output formatters #8

Closed
opened 2026-10-04 19:28:02 +00:00 by daniel · 1 comment
Owner

Imported from GitHub samson-media/barn#8 — opened by @daniel-samson on 2026-01-21 12:37:20 UTC

Overview

Set up the CLI framework with picocli and implement output formatters (JSON, XML, human-readable).

Context

  • Read first: README.md (CLI usage examples)
  • Depends on: #1 (project structure)

Requirements

1. Output Format Enum

com.samsonmedia.barn.cli.OutputFormat.java:

public enum OutputFormat {
    HUMAN,  // Default: pretty, human-readable
    JSON,   // Machine-readable JSON
    XML     // Machine-readable XML
}

2. Global Options Mixin

com.samsonmedia.barn.cli.GlobalOptions.java:

public class GlobalOptions {
    @Option(names = {"-o", "--output"}, 
            description = "Output format: human, json, xml", 
            defaultValue = "human")
    private OutputFormat outputFormat = OutputFormat.HUMAN;
    
    @Option(names = {"--config"}, 
            description = "Path to config file")
    private Path configPath;
    
    @Option(names = {"--offline"}, 
            description = "Run without service (for testing)")
    private boolean offline;
    
    public OutputFormat getOutputFormat() { return outputFormat; }
    public Optional<Path> getConfigPath() { return Optional.ofNullable(configPath); }
    public boolean isOffline() { return offline; }
}

3. Output Formatter Interface

com.samsonmedia.barn.cli.OutputFormatter.java:

public interface OutputFormatter {
    // Format a single object
    String format(Object value);
    
    // Format a list
    String formatList(List<?> values);
    
    // Format an error
    String formatError(String message, Throwable cause);
    
    // Factory method
    static OutputFormatter forFormat(OutputFormat format) {
        return switch (format) {
            case HUMAN -> new HumanFormatter();
            case JSON -> new JsonFormatter();
            case XML -> new XmlFormatter();
        };
    }
}

4. Human-Readable Formatter

com.samsonmedia.barn.cli.HumanFormatter.java:

public class HumanFormatter implements OutputFormatter {
    // Format job as table row
    // Format job list as table with headers
    // Use ANSI colors for terminal (detect TTY)
}

Example output:

ID         STATE      TAG         CREATED              EXIT
job-9f83c  running    transcode   2026-01-21 18:40:01  -
job-a7b2d  succeeded  download    2026-01-21 18:35:00  0
job-c4e1f  failed     upload      2026-01-21 18:30:00  1

5. JSON Formatter

com.samsonmedia.barn.cli.JsonFormatter.java:

public class JsonFormatter implements OutputFormatter {
    private final ObjectMapper mapper;
    
    // Pretty-print JSON with proper date formatting
}

Example output:

{
  "id": "job-9f83c",
  "state": "running",
  "tag": "transcode",
  "created_at": "2026-01-21T18:40:01Z"
}

6. XML Formatter

com.samsonmedia.barn.cli.XmlFormatter.java:

public class XmlFormatter implements OutputFormatter {
    // Simple XML output
}

Example output:

<job>
  <id>job-9f83c</id>
  <state>running</state>
  <tag>transcode</tag>
  <created_at>2026-01-21T18:40:01Z</created_at>
</job>

7. Base Command Class

com.samsonmedia.barn.cli.BaseCommand.java:

public abstract class BaseCommand implements Callable<Integer> {
    @Mixin
    protected GlobalOptions globalOptions;
    
    protected OutputFormatter getFormatter() {
        return OutputFormatter.forFormat(globalOptions.getOutputFormat());
    }
    
    protected void output(Object value) {
        System.out.println(getFormatter().format(value));
    }
    
    protected void outputError(String message, Throwable cause) {
        System.err.println(getFormatter().formatError(message, cause));
    }
}

8. Tests Required

  • HumanFormatterTest.java: Test table formatting, colors
  • JsonFormatterTest.java: Test JSON output matches spec
  • XmlFormatterTest.java: Test XML output
  • GlobalOptionsTest.java: Test option parsing

Acceptance Criteria

  • --output=json produces valid JSON
  • --output=xml produces valid XML
  • Default output is human-readable
  • --config option overrides default config
  • --offline flag works for testing
  • mvn verify passes
  • Tests have 80%+ coverage

Implementation Notes

For ANSI colors, check if stdout is a TTY:

boolean isTty = System.console() != null;

JSON dates should be ISO-8601:

mapper.registerModule(new JavaTimeModule());
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

Definition of Done

  • All three formatters work correctly
  • Global options parsed properly
  • Base command provides common functionality
  • All tests pass
<!-- github-import:issue:8 --> _Imported from GitHub samson-media/barn#8 — opened by **@daniel-samson** on 2026-01-21 12:37:20 UTC_ ## Overview Set up the CLI framework with picocli and implement output formatters (JSON, XML, human-readable). ## Context - **Read first**: `README.md` (CLI usage examples) - **Depends on**: #1 (project structure) ## Requirements ### 1. Output Format Enum `com.samsonmedia.barn.cli.OutputFormat.java`: ```java public enum OutputFormat { HUMAN, // Default: pretty, human-readable JSON, // Machine-readable JSON XML // Machine-readable XML } ``` ### 2. Global Options Mixin `com.samsonmedia.barn.cli.GlobalOptions.java`: ```java public class GlobalOptions { @Option(names = {"-o", "--output"}, description = "Output format: human, json, xml", defaultValue = "human") private OutputFormat outputFormat = OutputFormat.HUMAN; @Option(names = {"--config"}, description = "Path to config file") private Path configPath; @Option(names = {"--offline"}, description = "Run without service (for testing)") private boolean offline; public OutputFormat getOutputFormat() { return outputFormat; } public Optional<Path> getConfigPath() { return Optional.ofNullable(configPath); } public boolean isOffline() { return offline; } } ``` ### 3. Output Formatter Interface `com.samsonmedia.barn.cli.OutputFormatter.java`: ```java public interface OutputFormatter { // Format a single object String format(Object value); // Format a list String formatList(List<?> values); // Format an error String formatError(String message, Throwable cause); // Factory method static OutputFormatter forFormat(OutputFormat format) { return switch (format) { case HUMAN -> new HumanFormatter(); case JSON -> new JsonFormatter(); case XML -> new XmlFormatter(); }; } } ``` ### 4. Human-Readable Formatter `com.samsonmedia.barn.cli.HumanFormatter.java`: ```java public class HumanFormatter implements OutputFormatter { // Format job as table row // Format job list as table with headers // Use ANSI colors for terminal (detect TTY) } ``` Example output: ``` ID STATE TAG CREATED EXIT job-9f83c running transcode 2026-01-21 18:40:01 - job-a7b2d succeeded download 2026-01-21 18:35:00 0 job-c4e1f failed upload 2026-01-21 18:30:00 1 ``` ### 5. JSON Formatter `com.samsonmedia.barn.cli.JsonFormatter.java`: ```java public class JsonFormatter implements OutputFormatter { private final ObjectMapper mapper; // Pretty-print JSON with proper date formatting } ``` Example output: ```json { "id": "job-9f83c", "state": "running", "tag": "transcode", "created_at": "2026-01-21T18:40:01Z" } ``` ### 6. XML Formatter `com.samsonmedia.barn.cli.XmlFormatter.java`: ```java public class XmlFormatter implements OutputFormatter { // Simple XML output } ``` Example output: ```xml <job> <id>job-9f83c</id> <state>running</state> <tag>transcode</tag> <created_at>2026-01-21T18:40:01Z</created_at> </job> ``` ### 7. Base Command Class `com.samsonmedia.barn.cli.BaseCommand.java`: ```java public abstract class BaseCommand implements Callable<Integer> { @Mixin protected GlobalOptions globalOptions; protected OutputFormatter getFormatter() { return OutputFormatter.forFormat(globalOptions.getOutputFormat()); } protected void output(Object value) { System.out.println(getFormatter().format(value)); } protected void outputError(String message, Throwable cause) { System.err.println(getFormatter().formatError(message, cause)); } } ``` ### 8. Tests Required - `HumanFormatterTest.java`: Test table formatting, colors - `JsonFormatterTest.java`: Test JSON output matches spec - `XmlFormatterTest.java`: Test XML output - `GlobalOptionsTest.java`: Test option parsing ## Acceptance Criteria - [ ] `--output=json` produces valid JSON - [ ] `--output=xml` produces valid XML - [ ] Default output is human-readable - [ ] `--config` option overrides default config - [ ] `--offline` flag works for testing - [ ] `mvn verify` passes - [ ] Tests have 80%+ coverage ## Implementation Notes For ANSI colors, check if stdout is a TTY: ```java boolean isTty = System.console() != null; ``` JSON dates should be ISO-8601: ```java mapper.registerModule(new JavaTimeModule()); mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); ``` ## Definition of Done - All three formatters work correctly - Global options parsed properly - Base command provides common functionality - All tests pass
daniel added this to the M3: CLI Commands milestone 2026-10-04 19:28:02 +00:00
daniel added the cli label 2026-10-04 19:28:02 +00:00
Author
Owner

@daniel-samson commented on 2026-01-21 13:33:36 UTC:

✅ Ticket #8 completed: Implemented CLI framework with output formatters.

OutputFormat (src/main/java/com/samsonmedia/barn/cli/OutputFormat.java):

  • Enum with HUMAN, JSON, XML options

GlobalOptions (src/main/java/com/samsonmedia/barn/cli/GlobalOptions.java):

  • Picocli mixin with -o/--output, --config, and --offline options
  • Default output format is HUMAN

OutputFormatter (src/main/java/com/samsonmedia/barn/cli/OutputFormatter.java):

  • Interface with format(), formatList(), formatError() methods
  • Factory method forFormat() to create appropriate formatter

HumanFormatter (src/main/java/com/samsonmedia/barn/cli/HumanFormatter.java):

  • Human-readable table format with aligned columns
  • Optional ANSI color support (auto-detected via System.console())
  • State-based coloring: QUEUED=cyan, RUNNING=yellow, SUCCEEDED=green, FAILED=red

JsonFormatter (src/main/java/com/samsonmedia/barn/cli/JsonFormatter.java):

  • Machine-readable JSON output
  • ISO-8601 date formatting
  • Pretty-printed with indentation

XmlFormatter (src/main/java/com/samsonmedia/barn/cli/XmlFormatter.java):

  • Machine-readable XML output
  • Proper XML escaping for special characters
  • Snake_case element naming

BaseCommand (src/main/java/com/samsonmedia/barn/cli/BaseCommand.java):

  • Base class for all CLI commands
  • Provides output(), outputList(), outputError() methods
  • Exit code constants (EXIT_SUCCESS, EXIT_ERROR, EXIT_USAGE)

Tests:

  • HumanFormatterTest: 14 tests
  • JsonFormatterTest: 13 tests
  • XmlFormatterTest: 14 tests
  • GlobalOptionsTest: 12 tests
  • OutputFormatterTest: 3 tests

All 470 tests passing. mvn verify successful.

<!-- github-import:comment:3778165159 --> _**@daniel-samson** commented on 2026-01-21 13:33:36 UTC:_ ✅ Ticket #8 completed: Implemented CLI framework with output formatters. **OutputFormat** (src/main/java/com/samsonmedia/barn/cli/OutputFormat.java): - Enum with HUMAN, JSON, XML options **GlobalOptions** (src/main/java/com/samsonmedia/barn/cli/GlobalOptions.java): - Picocli mixin with -o/--output, --config, and --offline options - Default output format is HUMAN **OutputFormatter** (src/main/java/com/samsonmedia/barn/cli/OutputFormatter.java): - Interface with format(), formatList(), formatError() methods - Factory method forFormat() to create appropriate formatter **HumanFormatter** (src/main/java/com/samsonmedia/barn/cli/HumanFormatter.java): - Human-readable table format with aligned columns - Optional ANSI color support (auto-detected via System.console()) - State-based coloring: QUEUED=cyan, RUNNING=yellow, SUCCEEDED=green, FAILED=red **JsonFormatter** (src/main/java/com/samsonmedia/barn/cli/JsonFormatter.java): - Machine-readable JSON output - ISO-8601 date formatting - Pretty-printed with indentation **XmlFormatter** (src/main/java/com/samsonmedia/barn/cli/XmlFormatter.java): - Machine-readable XML output - Proper XML escaping for special characters - Snake_case element naming **BaseCommand** (src/main/java/com/samsonmedia/barn/cli/BaseCommand.java): - Base class for all CLI commands - Provides output(), outputList(), outputError() methods - Exit code constants (EXIT_SUCCESS, EXIT_ERROR, EXIT_USAGE) **Tests**: - HumanFormatterTest: 14 tests - JsonFormatterTest: 13 tests - XmlFormatterTest: 14 tests - GlobalOptionsTest: 12 tests - OutputFormatterTest: 3 tests All 470 tests passing. mvn verify successful.
This repo is archived. You cannot comment on issues.