diff --git a/src/main/java/org/justserve/BaseCommand.java b/src/main/java/org/justserve/BaseCommand.java index 76b78ea..746f510 100644 --- a/src/main/java/org/justserve/BaseCommand.java +++ b/src/main/java/org/justserve/BaseCommand.java @@ -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 userClientProvider; @@ -42,12 +40,8 @@ public static void main(String[] args) { } public void run() { - if (version) { - printNormal(justserveCliVersion); - return; - } HttpResponse 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.")); diff --git a/src/main/java/org/justserve/JustServeClientFilter.java b/src/main/java/org/justserve/auth/JustServeClientFilter.java similarity index 98% rename from src/main/java/org/justserve/JustServeClientFilter.java rename to src/main/java/org/justserve/auth/JustServeClientFilter.java index 8825843..932da88 100644 --- a/src/main/java/org/justserve/JustServeClientFilter.java +++ b/src/main/java/org/justserve/auth/JustServeClientFilter.java @@ -1,4 +1,4 @@ -package org.justserve; +package org.justserve.auth; import io.micronaut.context.annotation.Property; import io.micronaut.context.annotation.Requires; diff --git a/src/main/java/org/justserve/JustServePrinter.java b/src/main/java/org/justserve/util/JustServePrinter.java similarity index 72% rename from src/main/java/org/justserve/JustServePrinter.java rename to src/main/java/org/justserve/util/JustServePrinter.java index 87ea710..6a01c62 100644 --- a/src/main/java/org/justserve/JustServePrinter.java +++ b/src/main/java/org/justserve/util/JustServePrinter.java @@ -1,4 +1,4 @@ -package org.justserve; +package org.justserve.util; import org.fusesource.jansi.Ansi; @@ -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(); @@ -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)); } /** @@ -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. * diff --git a/src/main/java/org/justserve/util/VersionProvider.java b/src/main/java/org/justserve/util/VersionProvider.java new file mode 100644 index 0000000..082db41 --- /dev/null +++ b/src/main/java/org/justserve/util/VersionProvider.java @@ -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)}; + } +} diff --git a/src/test/groovy/org/justserve/BaseCommandSpec.groovy b/src/test/groovy/org/justserve/BaseCommandSpec.groovy index 0786f85..7d54604 100644 --- a/src/test/groovy/org/justserve/BaseCommandSpec.groovy +++ b/src/test/groovy/org/justserve/BaseCommandSpec.groovy @@ -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*\$")