Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ dependencies {
implementation("info.picocli:picocli")
implementation("info.picocli:picocli-jansi-graalvm:1.2.0")
implementation("org.fusesource.jansi:jansi:2.4.2")
implementation("info.picocli:picocli-shell-jline3:4.7.6")
implementation("org.jline:jline:3.30.5")
implementation("io.micronaut:micronaut-http-client")
implementation("io.micronaut.picocli:micronaut-picocli")
implementation("io.micronaut.serde:micronaut-serde-jackson")
Expand All @@ -36,7 +38,7 @@ dependencies {


application {
mainClass = "org.justserve.BaseCommand"
mainClass = "org.justserve.cli.JustServeCommand"
}

java {
Expand Down
26 changes: 26 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
services:
linux-shell:
image: ghcr.io/graalvm/native-image-community:21-muslib
container_name: justserve-linux-shell
working_dir: /app
volumes:
- .:/app
env_file: .env
entrypoint: /bin/sh
command:
- -c
- "mkdir -p /temp_app && cp -r . /temp_app/ && cd /temp_app && microdnf install -y findutils && sed -i 's/\r$//' ./gradlew && if [ -t 0 ]; then /bin/bash; else tail -f /dev/null; fi"
stdin_open: true
tty: true

linux-test:
image: ghcr.io/graalvm/native-image-community:21-muslib
container_name: justserve-linux-test
working_dir: /app
volumes:
- .:/app
env_file: .env
entrypoint: /bin/sh
command:
- -c
- "mkdir -p /temp_app && cp -r . /temp_app/ && cd /temp_app && microdnf install -y findutils && sed -i 's/\r$//' ./gradlew && ./gradlew test"
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
micronautVersion=4.8.3
justserveCliVersion=0.0.5-SNAPSHOT
justserveCliVersion=0.0.5-SNAPSHOT
org.gradle.console=rich
28 changes: 28 additions & 0 deletions src/main/java/org/justserve/cli/JustServeCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.justserve.cli;

import io.micronaut.configuration.picocli.PicocliRunner;
import org.justserve.cli.command.BaseCommand;
import org.justserve.cli.command.GetTempPassword;
import org.justserve.cli.util.JustServeVersionProvider;
import picocli.CommandLine.Command;
import picocli.CommandLine.ParameterException;
import picocli.jansi.graalvm.AnsiConsole;

@Command(subcommands = GetTempPassword.class,
mixinStandardHelpOptions = true,
name = "justserve", versionProvider = JustServeVersionProvider.class,
description = "justserve-cli is a terminal tool to help specialists and admin using JustServe")
public class JustServeCommand extends BaseCommand implements Runnable {

public static void main(String[] args) {
try (AnsiConsole ignored = AnsiConsole.windowsInstall()) {
PicocliRunner.run(JustServeCommand.class, args);
}

}

@Override
public void run() {
throw new ParameterException(spec.commandLine(), "No command specified");
}
}
70 changes: 70 additions & 0 deletions src/main/java/org/justserve/cli/command/BaseCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.justserve.cli.command;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.ReflectiveAccess;
import picocli.CommandLine.Command;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Spec;

import java.io.PrintWriter;
import java.util.Optional;

import static picocli.CommandLine.Help.Ansi.AUTO;

@Command
public class BaseCommand implements ConsoleOutput{

@Spec
@ReflectiveAccess
protected CommandSpec spec;


@Override
public void out(String message) {
outWriter().ifPresent(writer -> writer.println(AUTO.string(message)));
}

public void err(String message) {
errWriter().ifPresent(writer -> writer.println(AUTO.string("@|bold,red | Error|@ " + message)));
}

public void warning(String message) {
outWriter().ifPresent(writer -> writer.println(AUTO.string("@|bold,red | Warning|@ " + message)));
}

@Override
public void green(String message) {
outWriter().ifPresent(writer -> writer.println(AUTO.string("@|bold,green " + message + "|@")));
}

@Override
public void red(String message) {
outWriter().ifPresent(writer -> writer.println(AUTO.string("@|bold,red " + message + "|@")));
}

@Override
public boolean showStacktrace() {
return false;
}

@Override
public boolean verbose() {
return false;
}


@NonNull
public Optional<PrintWriter> outWriter() {
return getSpec().map(spec -> spec.commandLine().getOut());
}

@NonNull
public Optional<PrintWriter> errWriter() {
return getSpec().map(spec -> spec.commandLine().getErr());
}

@NonNull
public Optional<CommandSpec> getSpec() {
return Optional.ofNullable(spec);
}
}
34 changes: 34 additions & 0 deletions src/main/java/org/justserve/cli/command/CommonOptionsMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.justserve.cli.command;

import io.micronaut.core.annotation.ReflectiveAccess;
import org.justserve.cli.util.JustServeVersionProvider;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

/**
* Mixin that adds help, version and other common options to a command. Example usage:
* <pre>
* &#064;Command(name = "command")
* class App {
* &#064;Mixin
* CommonOptionsMixin commonOptions // adds common options to the command
*
* // ...
* }
* </pre>
*
* @author Remko Popma
* @version 1.0
*/
@Command(mixinStandardHelpOptions = true, versionProvider = JustServeVersionProvider.class)
@SuppressWarnings("checkstyle:VisibilityModifier")
public class CommonOptionsMixin {

@Option(names = {"-x", "--stacktrace"}, defaultValue = "false", description = "Show full stack trace when exceptions occur.")
@ReflectiveAccess
public boolean showStacktrace;

@Option(names = {"-v", "--verbose"}, defaultValue = "false", description = "Create verbose output.")
@ReflectiveAccess
public boolean verbose;
}
43 changes: 43 additions & 0 deletions src/main/java/org/justserve/cli/command/ConsoleOutput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.justserve.cli.command;

public interface ConsoleOutput {
ConsoleOutput NOOP = new ConsoleOutput() {
@Override
public void out(String message) { }

@Override
public void err(String message) { }

@Override
public void warning(String message) { }

@Override
public boolean showStacktrace() {
return false;
}

@Override
public boolean verbose() {
return false;
}

};

void out(String message);

void err(String message);

void warning(String message);

boolean showStacktrace();

boolean verbose();

default void green(String message) {
out(message);
}

default void red(String message) {
out(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,34 @@
package org.justserve;
package org.justserve.cli.command;

import io.micronaut.configuration.picocli.PicocliRunner;
import io.micronaut.context.annotation.Value;
import io.micronaut.core.annotation.ReflectiveAccess;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.client.exceptions.HttpClientResponseException;
import jakarta.inject.Inject;
import jakarta.inject.Provider;
import org.justserve.client.UserClient;
import org.justserve.model.UserHashRequestByEmail;
import org.justserve.util.VersionProvider;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.jansi.graalvm.AnsiConsole;

import static org.justserve.util.JustServePrinter.printError;
import static org.justserve.util.JustServePrinter.printNormal;
import static org.justserve.cli.util.JustServePrinter.printError;
import static org.justserve.cli.util.JustServePrinter.printNormal;

@Command(name = "justserve", versionProvider = VersionProvider.class,
description = "justserve-cli is a terminal tool to help specialists and admin using JustServe")
public class BaseCommand implements Runnable {
@Command(name = "getTempPassword", description = "get a temporary password for a user")
public class GetTempPassword extends BaseCommand implements Runnable {

@ReflectiveAccess
@Option(names = {"-e", "--email"}, description = "email for the user whose temporary password will be generated")
String email;

@Option(names = {"version", "--version", "-v"}, versionHelp = true, description = "print version info and exit")
boolean version = false;

@Inject
@ReflectiveAccess
Provider<UserClient> userClientProvider;

@Value("${justserve.token}")
String token;

public static void main(String[] args) {
try (AnsiConsole ignored = AnsiConsole.windowsInstall()) {
PicocliRunner.run(BaseCommand.class, args);
}

}

@Override
public void run() {
HttpResponse<String> response;
if ("i-need-to-be-defined".equals(token) || null == token) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.justserve.util;
package org.justserve.cli.util;

import org.fusesource.jansi.Ansi;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.justserve.util;
package org.justserve.cli.util;

import io.micronaut.context.annotation.Value;
import picocli.CommandLine;

import static org.justserve.util.JustServePrinter.styleEmphasis;
import static org.justserve.util.JustServePrinter.styleTitle;
import static org.justserve.cli.util.JustServePrinter.styleEmphasis;
import static org.justserve.cli.util.JustServePrinter.styleTitle;

public class VersionProvider implements CommandLine.IVersionProvider {
public class JustServeVersionProvider implements CommandLine.IVersionProvider {

@Value("${micronaut.application.version}")
String justserveCliVersion;
Expand Down
2 changes: 1 addition & 1 deletion src/test/groovy/org/justserve/client/UserClientSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class UserClientSpec extends JustServeSpec {
userClient = ctx.getBean(UserClient)
}

def "get tempPassword for #email and "() {
def "get tempPassword for #email"() {
when:
HttpResponse<String> response = null
def caughtException = null
Expand Down
Loading
Loading