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
18 changes: 6 additions & 12 deletions src/main/java/org/justserve/BaseCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,24 @@
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.JustServePrinter.printError;
import static org.justserve.JustServePrinter.printNormal;
import static org.justserve.util.JustServePrinter.printError;
import static org.justserve.util.JustServePrinter.printNormal;

@Command(name = "justserve",
@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 {

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

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

@Value("${micronaut.application.version}")
String justserveCliVersion;

@Inject
Provider<UserClient> userClientProvider;

Expand All @@ -42,12 +40,8 @@ public static void main(String[] args) {
}

public void run() {
if (version) {
printNormal(justserveCliVersion);
return;
}
HttpResponse<String> response;
if ("i-need-to-be-defined".equals(token) || null == token ) {
if ("i-need-to-be-defined".equals(token) || null == token) {
printError(("NO AUTHENTICATION PROVIDED" + System.lineSeparator() +
"The Authentication token is not assigned as an environment variable." + System.lineSeparator() +
"Please define the environment variable \"JUSTSERVE_TOKEN\" and try again."));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.justserve;
package org.justserve.auth;

import io.micronaut.context.annotation.Property;
import io.micronaut.context.annotation.Requires;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.justserve;
package org.justserve.util;

import org.fusesource.jansi.Ansi;

Expand All @@ -13,13 +13,13 @@
public final class JustServePrinter {

private final static Ansi blue = ansi().fgRgb(0, 158, 185);
private final static Ansi orange = ansi().fgRgb(239, 94, 57);
private final static Ansi orange = ansi().fgRgb(255, 140, 0); // OG color is 239, 94, 57
private final static Ansi red = ansi().fgRgb(233, 59, 84); // I definitely eyeballed this one
private final static Ansi yellow = ansi().fgRgb(225, 188, 33);

private final static Ansi normalStyle = ansi().reset();
private final static Ansi titleStyle = blue;
private final static Ansi emphasisStyle = orange;
private final static Ansi titleStyle = blue.bold();
private final static Ansi emphasisStyle = orange.bold();
private final static Ansi warningStyle = yellow;
private final static Ansi errorTitleStyle = red.bold();
private final static Ansi errorInfoStyle = ansi().reset();
Expand All @@ -32,14 +32,18 @@ private JustServePrinter() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}

private static void jsPrint(String message, Ansi... style) {
private static String applyStyle(String message, Ansi... style) {
Ansi styledMessage = stream(style).reduce(ansi(), Ansi::a, Ansi::a);
System.out.println(styledMessage.a(message).reset());
return styledMessage.a(message).reset().toString();
}

private static void jsPrint(String message, Ansi... style) {
System.out.println(applyStyle(message, style));
}


private static void jsPrintErr(String message, Ansi... style) {
Ansi styledMessage = stream(style).reduce(ansi(), Ansi::a, Ansi::a);
System.err.println(styledMessage.a(message).reset());
System.err.println(applyStyle(message, style));
}

/**
Expand All @@ -51,6 +55,24 @@ public static void printNormal(String message) {
jsPrint(message, normalStyle);
}

/**
* Returns a String stylized in Orange.
*
* @param message The message to print.
*/
public static String styleTitle(String message) {
return applyStyle(message, titleStyle);
}

/**
* Returns a String stylized in the designated emphasis style.
*
* @param message The message to print.
*/
public static String styleEmphasis(String message) {
return applyStyle(message, emphasisStyle);
}

/**
* Prints an error message to the standard error stream in red.
*
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/org/justserve/util/VersionProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.justserve.util;

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

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

public class VersionProvider implements CommandLine.IVersionProvider {

@Value("${micronaut.application.version}")
String justserveCliVersion;

String fancyPrintout = """
_ _ ____
| | | | / ___|
| |_ _ ___| |_| (___ ___ _ ____ _____
| | | | / __| __|\\___ \\ / _ \\ '__\\ \\ / / _ \\
___| | |_| \\__ \\ |_ ____) | __/ | \\ V / __/
|____/ \\__,_|___/\\__|_____/ \\___|_| \\_/ \\___|""";

@Override
public String[] getVersion() {
return new String[]{styleEmphasis(fancyPrintout), styleTitle(justserveCliVersion)};
}
}
9 changes: 8 additions & 1 deletion src/test/groovy/org/justserve/BaseCommandSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ class BaseCommandSpec extends JustServeSpec {
props.load(stream)
}
def ansi = "\\u001B\\[[;\\d]*m"
cliVersion = Pattern.compile("^${ansi}" + props.getProperty('justserveCliVersion') + "${ansi}\\s*\$")
def staticText = """ _ _ ____
| | | | / ___|
| |_ _ ___| |_| (___ ___ _ ____ _____
| | | | / __| __|\\___ \\ / _ \\ '__\\ \\ / / _ \\
___| | |_| \\__ \\ |_ ____) | __/ | \\ V / __/
|____/ \\__,_|___/\\__|_____/ \\___|_| \\_/ \\___|""";
cliVersion = Pattern.compile("^${ansi}${Pattern.quote(staticText)}${ansi}\\s*${ansi}" +
Pattern.quote(props.getProperty('justserveCliVersion')) + "${ansi}\\s*\$")
blankRegex = Pattern.compile "^\\s*\$"
successRegex = Pattern.compile("^${ansi}\\w+${ansi}\\s*\$")
errorRegex = Pattern.compile("(?is)^${ansi}received an unexpected response from JustServe:${ansi}.*\\d+ \\(.*?\\)${ansi}\\s*\$")
Expand Down
Loading