Skip to content

Commit

Permalink
[env-manager] better log message formatting for env command
Browse files Browse the repository at this point in the history
  • Loading branch information
rmannibucau committed Feb 11, 2024
1 parent 5e1ce46 commit 824937d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
7 changes: 4 additions & 3 deletions env-manager/src/main/java/io/yupiik/dev/command/Env.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import static java.io.File.pathSeparator;
import static java.util.Optional.ofNullable;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.INFO;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toMap;

Expand Down Expand Up @@ -115,7 +116,7 @@ public void run() {
public void publish(final LogRecord record) {
// capture to forward messages in the shell when init is done (thanks eval call)
if (logger.isLoggable(record.getLevel())) {
messages.add(record.getMessage());
messages.add(messageHelper.formatLog(record.getLevel(), record.getMessage()));
}

// enable to log at fine level for debug purposes
Expand Down Expand Up @@ -196,9 +197,9 @@ private void createScript(final List<RcService.MatchedPath> rawResolved,
resolved.stream()
// don't log too much, if it does not change, don't re-log it
.filter(Predicate.not(it -> Objects.equals(it.path().toString(), System.getenv(it.properties().envPathVarName()))))
.map(e -> "echo \"[yem] Resolved " + messageHelper.formatToolNameAndVersion(
.map(e -> "echo \"[yem] " + messageHelper.formatLog(INFO, "Resolved " + messageHelper.formatToolNameAndVersion(
e.candidate(), e.properties().toolName(), e.properties().version()) + " to '" +
e.path().toString().replace(home, "~") + "'\";")
e.path().toString().replace(home, "~") + "'") + "\";")
.collect(joining("\n", "", "\n")) :
"";

Expand Down
37 changes: 34 additions & 3 deletions env-manager/src/main/java/io/yupiik/dev/shared/MessageHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.Level;
import java.util.stream.Stream;

@ApplicationScoped
public class MessageHelper {
Expand All @@ -38,8 +40,11 @@ public MessageHelper(final MessagesConfiguration configuration) {
@Init
protected void init() {
supportsEmoji = switch (configuration.disableEmoji()) {
case "auto" -> !Boolean.parseBoolean(System.getenv("CI")) &&
Files.exists(Path.of("/usr/share/fonts/truetype/noto/NotoColorEmoji.ttf"));
case "auto" -> !Boolean.parseBoolean(System.getenv("CI")) && Stream.of(
"/usr/share/fonts/AppleColorEmoji/",
"/usr/share/fonts/truetype/noto/NotoColorEmoji.ttf",
"/usr/share/fonts/google-noto-emoji/NotoColorEmoji.ttf")
.anyMatch(p -> Files.exists(Path.of(p)));
default -> !Boolean.parseBoolean(configuration.disableEmoji());
};
enableColors = switch (configuration.disableColors()) {
Expand Down Expand Up @@ -67,13 +72,39 @@ private String format(final String color, final String value) {
return (enableColors ? colorPrefix + color + 'm' : "") + value + (enableColors ? colorPrefix + "0m" : "");
}

public String error(final String value) {
return format(configuration.errorColor(), value);
}

public String warning(final String value) {
return format(configuration.warningColor(), value);
}

public String formatLog(final Level level, final String message) {
return (supportsEmoji ?
switch (level.intValue()) {
case /*FINE, FINER, FINEST*/ 300, 400, 500 -> "🐞 ";
case /*INFO*/ 800 -> "ℹ ";
case /*WARNING*/ 900 -> "⚠️ ";
case /*SEVERE*/ 1000 -> "🚩 ";
default -> "";
} : "") +
(enableColors ? switch (level.intValue()) {
case 900 -> format(configuration.warningColor(), message);
case 1000 -> format(configuration.errorColor(), message);
default -> message;
} : message);
}

@RootConfiguration("messages")
public record MessagesConfiguration(
@Property(documentation = "Are colors disabled for the terminal.", defaultValue = "\"auto\"") String disableColors,
@Property(documentation = "When color are enabled the tool name color.", defaultValue = "\"0;49;34\"") String toolColor,
@Property(documentation = "Error message color.", defaultValue = "\"31\"") String errorColor,
@Property(documentation = "Warning message color.", defaultValue = "\"33\"") String warningColor,
@Property(documentation = "When color are enabled the version color.", defaultValue = "\"0;49;96\"") String versionColor,
@Property(documentation = "If `false` emoji are totally disabled. " +
"`auto` will test `/usr/share/fonts/truetype/noto/NotoColorEmoji.ttf` presence to enable emojis. " +
"`auto` will test `/usr/share/fonts/truetype/noto/NotoColorEmoji.ttf` and `/usr/share/fonts/google-noto-emoji/NotoColorEmoji.ttf` presence to enable emojis. " +
"`true`/`false` disable/enable emoji whatever the available fonts.", defaultValue = "\"auto\"") String disableEmoji) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private CompletableFuture<List<MatchedPath>> doToolProperties(final List<ToolPro
if (tool.failOnMissing()) {
throw new IllegalStateException("Missing home for " + tool.toolName() + "@" + tool.version());
}
logger.finest(() -> tool.toolName() + "@" + tool.version() + " not available");
logger.warning(() -> tool.toolName() + "@" + tool.version() + " not available");
return completedFuture(Optional.empty());
}))
.toCompletableFuture();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void env(@TempDir final Path work, final URI uri) throws IOException {
export PATH="$work/zulu/21.32.17-ca-jdk21.0.2/distribution_exploded:$PATH";
export JAVA_HOME="$work/zulu/21.32.17-ca-jdk21.0.2/distribution_exploded";
export JAVA_VERSION="21.0.2";
echo "[yem] Resolved java @ 21.0.2 to '$work/zulu/21.32.17-ca-jdk21.0.2/distribution_exploded'\";""")
echo "[yem] Resolved java @ 21.0.2 to '$work/zulu/21.32.17-ca-jdk21.0.2/distribution_exploded'";""")
.replace("$work", work.toString()),
out
.replaceAll("#.*", "")
Expand All @@ -155,7 +155,7 @@ void envInline(@TempDir final Path work, final URI uri) {
export JAVA_HOME="$work/zulu/21.32.17-ca-jdk21.0.2/distribution_exploded";
export JAVA_VERSION="21.0.2";
export YEM_JAVA_HOME_OVERRIDEN="21.0.2";
echo "[yem] Resolved java @ 21.0.2 to '$work/zulu/21.32.17-ca-jdk21.0.2/distribution_exploded'\";""")
echo "[yem] Resolved java @ 21.0.2 to '$work/zulu/21.32.17-ca-jdk21.0.2/distribution_exploded'";""")
.replace("$work", work.toString()),
out
.replaceAll("#.*", "")
Expand All @@ -175,7 +175,7 @@ void envSdkManRc(@TempDir final Path work, final URI uri) throws IOException {
export PATH="$work/zulu/21.32.17-ca-jdk21.0.2/distribution_exploded:$PATH";
export JAVA_HOME="$work/zulu/21.32.17-ca-jdk21.0.2/distribution_exploded";
export JAVA_VERSION="21.0.2";
echo "[yem] Resolved java @ 21.0.2 to '$work/zulu/21.32.17-ca-jdk21.0.2/distribution_exploded'\";""")
echo "[yem] Resolved java @ 21.0.2 to '$work/zulu/21.32.17-ca-jdk21.0.2/distribution_exploded'";""")
.replace("$work", work.toString()),
out
.replaceAll("#.*", "")
Expand Down

0 comments on commit 824937d

Please sign in to comment.