Skip to content

Commit

Permalink
Added a command line tool skeleton (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
climategadgets committed Aug 28, 2024
1 parent f8e927a commit b4c9c41
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/hcc-cli/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/logs/
25 changes: 25 additions & 0 deletions app/hcc-cli/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
plugins {
application

alias(libs.plugins.spring.boot)
alias(libs.plugins.spring.dependency.management)
}

application {
applicationName = "hcc-cli"
mainClass.set("com.homeclimatecontrol.hcc.cli.HccCLI")
}

dependencies {

implementation(libs.jcommander)
implementation(libs.springboot.starter)
implementation(libs.springboot.starter.log4j2)
implementation(libs.springboot.starter.rsocket)
}

configurations {
all {
exclude("org.springframework.boot", "spring-boot-starter-logging")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.homeclimatecontrol.hcc.cli;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class HccCLI implements CommandLineRunner {

private static final Logger logger = LogManager.getLogger(HccCLI.class);

private static final String COMMAND_GET_META = "get-meta";

public abstract class CommandBase {

@Parameter(names = { "--url" }, description = "URL to connect to", required = true)
String url;
}

public class CommandGetMeta extends CommandBase {

}

public static void main(String[] args) {

var builder = new SpringApplicationBuilder(HccCLI.class);

builder.headless(false);

var context = builder.run(args);

context.close();
}

@Override
public void run(String... args) throws Exception {

var commandGetMeta = new CommandGetMeta();
var jc = JCommander
.newBuilder()
.addCommand(COMMAND_GET_META, commandGetMeta)
.build();

try {

jc.parse(args);

if (jc.getParsedCommand() == null) {
terminate(jc, "Please specify exactly one command", null);
}

switch (jc.getParsedCommand()) {

case COMMAND_GET_META -> getMeta(commandGetMeta.url);

}

} catch (ParameterException ex) {

terminate(jc, "Invalid command line arguments", ex);

} catch (Throwable t) {

terminate(jc, "Unhandled exception, terminating", t);
}
}

private void getMeta(String url) {
ThreadContext.push("getMeta");
try {
logger.info("url={}", url);
throw new UnsupportedOperationException("Stay tuned");
} finally {
ThreadContext.pop();
}
}

private static void terminate(JCommander jc, String message, Throwable t) {

logger.fatal(message, t);
jc.usage();
System.exit(-1);
}
}
118 changes: 118 additions & 0 deletions app/hcc-cli/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration status="WARN" monitorInterval="30">
<Properties>
<Property name="logDir">logs</Property>
</Properties>
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT">
<PatternLayout pattern="%highlight{%d{HH:mm:ss,SSS} %level %class{1} %t %NDC %message%n}{TRACE=white}"/>
<Filters>
<!-- Set your comfortable debug level for the console. -->
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
</Console>
<RollingFile
name="TraceAppender"
fileName="${logDir}/dz-trace.log"
filePattern="${logDir}/dz-trace.log.%d{yyyy-MM-dd}.gz">
<PatternLayout pattern="%highlight{%d{yyyy-MM-dd HH:mm:ss,SSS} %level %class{1} %t %NDC %message%n}{TRACE=white}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${logDir}" maxDepth="2">
<IfFileName glob="dz-trace.log.*.gz" />
<!-- Trace level logs are HUGE -->
<IfLastModified age="P1D" />
</Delete>
</DefaultRolloverStrategy>
<Filters>
<ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
</RollingFile>
<RollingFile
name="DebugAppender"
fileName="${logDir}/dz-debug.log"
filePattern="${logDir}/dz-debug.log.%d{yyyy-MM-dd}.gz">
<PatternLayout pattern="%highlight{%d{yyyy-MM-dd HH:mm:ss,SSS} %level %class{1} %t %NDC %message%n}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${logDir}" maxDepth="2">
<IfFileName glob="dz-debug.log.*.gz" />
<!-- Debug level logs are BIG -->
<IfLastModified age="P7D" />
</Delete>
</DefaultRolloverStrategy>
<Filters>
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
</RollingFile>
<RollingFile
name="InfoAppender"
fileName="${logDir}/dz-info.log"
filePattern="${logDir}/dz-info.log.%d{yyyy-MM-dd}.gz">
<PatternLayout pattern="%highlight{%d{yyyy-MM-dd HH:mm:ss,SSS} %level %class{1} %t %NDC %message%n}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${logDir}" maxDepth="2">
<IfFileName glob="dz-info.log.*.gz" />
<!-- Debug level logs are BIG -->
<IfLastModified age="P7D" />
</Delete>
</DefaultRolloverStrategy>
<Filters>
<!-- Set your comfortable debug level for the debug log. Best be left as is.-->
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
</RollingFile>
<RollingFile
name="WarnAppender"
fileName="${logDir}/dz-warn.log"
filePattern="${logDir}/dz-warn.log.%d{yyyy-MM-dd}.gz">
<PatternLayout pattern="%highlight{%d{yyyy-MM-dd HH:mm:ss,SSS} %level %class{1} %t %NDC %message%n}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${logDir}" maxDepth="2">
<IfFileName glob="dz-warn.log.*.gz" />
<IfLastModified age="P30D" />
</Delete>
</DefaultRolloverStrategy>
<Filters>
<!-- Set your comfortable debug level for the long term log. -->
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
</RollingFile>

</Appenders>
<Loggers>
<Logger name="net.sf.dz3r" level="debug" />
<Logger name="com.dalsemi" level="info" />
<Logger name="org.springframework" level="info" />

<!-- Reactive classes -->

<Logger name="net.sf.dz3r" level="trace" />

<Root level="INFO">

<AppenderRef ref="ConsoleAppender"/>

<!--
Uncomment the section below if you need extended diagnostics.
WARNING: consumes a lot of disk space.
-->
<AppenderRef ref="TraceAppender"/>
<AppenderRef ref="DebugAppender"/>

<AppenderRef ref="InfoAppender"/>
<AppenderRef ref="WarnAppender"/>

</Root>
</Loggers>
</Configuration>
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ httpclient = "4.5.14"
influxdb = "2.24"
jackson = "2.17.0"
jacoco = "0.8.11"
jcommander = "2.0"
jib = "3.4.3"
jmdns = "3.5.9"
junit5 = "5.10.2"
Expand Down Expand Up @@ -49,6 +50,7 @@ jackson-databind = { module = "com.fasterxml.jackson.core:jackson-databind", ver
jackson-dataformat-yaml = { module = "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml", version.ref = "jackson" }
jackson-datatype-jdk8 = { module = "com.fasterxml.jackson.datatype:jackson-datatype-jdk8", version.ref = "jackson" }
jackson-datatype-jsr310 = { module = "com.fasterxml.jackson.datatype:jackson-datatype-jsr310", version.ref = "jackson" }
jcommander = { module = "org.jcommander:jcommander", version.ref = "jcommander" }
jib-layer-filter = { module = "com.google.cloud.tools:jib-layer-filter-extension-gradle", version = "0.3.0" }
jmdns = { module = "org.jmdns:jmdns", version.ref = "jmdns" }
junit5-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "junit5" }
Expand Down Expand Up @@ -86,6 +88,7 @@ spring-standalone-webflux = { module = "org.springframework:spring-webflux", ver
# Versions are missing here because they are implied by spring-dependency-management plugin

springboot-configuration-processor = { module = "org.springframework.boot:spring-boot-configuration-processor" }
springboot-starter = { module = "org.springframework.boot:spring-boot-starter" }
springboot-starter-actuator = { module = "org.springframework.boot:spring-boot-starter-actuator" }
springboot-starter-log4j2 = { module = "org.springframework.boot:spring-boot-starter-log4j2" }
springboot-starter-rsocket = { module = "org.springframework.boot:spring-boot-starter-rsocket" }
Expand Down
4 changes: 4 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@ include("app:hcc-springboot")

include("app:hcc-quarkus")

// This is the command line tool, useful mostly for troubleshooting until further notice.

include("app:hcc-cli")

rootProject.name = "dz3-master"

0 comments on commit b4c9c41

Please sign in to comment.