mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-23 06:00:47 -05:00
fix(cli): honour --no-color on parse errors and show profile defaults in --help (#35)
This commit is contained in:
1 parent
bc59dff8c3
commit
fc67e72a62
8 files changed
+126
-13
No files matched your search
@@ -16,6 +16,8 @@ import io.floci.cli.update.UpdateNotifier;
|
||||
import picocli.CommandLine;
|
||||
import picocli.CommandLine.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@Command(
|
||||
name = "floci",
|
||||
description = "Manage your local Floci emulators%n%n" +
|
||||
@@ -74,7 +76,7 @@ public class FlociCli implements Runnable {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (System.getenv("NO_COLOR") != null || System.console() == null) {
|
||||
if (colorDisabled(args, System.getenv("NO_COLOR") != null, System.console() != null)) {
|
||||
Ansi.disable();
|
||||
}
|
||||
|
||||
@@ -146,6 +148,16 @@ public class FlociCli implements Runnable {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this invocation prints without ANSI colour. {@code --no-color} is read from the raw
|
||||
* arguments here as well as by {@code GlobalOptions.printer()}: a parse error is printed by the
|
||||
* exception handlers before any command, and so any printer, exists.
|
||||
*/
|
||||
public static boolean colorDisabled(String[] args, boolean noColorEnv, boolean interactive) {
|
||||
return noColorEnv || !interactive || Arrays.stream(args)
|
||||
.anyMatch(a -> a.equals("--no-color") || a.equalsIgnoreCase("--no-color=true"));
|
||||
}
|
||||
|
||||
// Returns true when routing should NOT apply: explicit product subgroup,
|
||||
// or a product-independent command like "config" or "completion".
|
||||
private static boolean isExplicitProduct(String[] args) {
|
||||
|
||||
@@ -169,7 +169,14 @@ public class StartCommand implements Callable<Integer> {
|
||||
try {
|
||||
URI uri = URI.create(endpoint);
|
||||
if (uri.getHost() == null) return "http://localhost:" + port;
|
||||
return new URI(uri.getScheme(), null, uri.getHost(), port, uri.getPath(), null, null).toString();
|
||||
// Raw components: the decoding getters would turn ?token=a%26b into ?token=a&b.
|
||||
StringBuilder url = new StringBuilder(uri.getScheme()).append("://");
|
||||
if (uri.getRawUserInfo() != null) url.append(uri.getRawUserInfo()).append('@');
|
||||
url.append(uri.getHost()).append(':').append(port); // getHost keeps IPv6 brackets
|
||||
if (uri.getRawPath() != null) url.append(uri.getRawPath());
|
||||
if (uri.getRawQuery() != null) url.append('?').append(uri.getRawQuery());
|
||||
if (uri.getRawFragment() != null) url.append('#').append(uri.getRawFragment());
|
||||
return url.toString();
|
||||
} catch (Exception e) {
|
||||
return "http://localhost:" + port;
|
||||
}
|
||||
|
||||
@@ -77,11 +77,16 @@ public class ProfileDefaultValueProvider implements IDefaultValueProvider {
|
||||
String name = profileOption.getValue();
|
||||
if (name == null) return null;
|
||||
|
||||
// Defaults are applied even when -h/-V was requested, so a bad profile must not stop
|
||||
// `floci start --profile typo --help` from printing its usage.
|
||||
if (helpRequested(command)) return null;
|
||||
|
||||
Profile profile = resolve(name, command.commandLine());
|
||||
// Help renders ${DEFAULT-VALUE} through this provider, so a good profile's values show up
|
||||
// in `--help`. A bad one must not stop `floci start --profile typo --help` from printing
|
||||
// its usage, so only then is the failure swallowed.
|
||||
Profile profile;
|
||||
try {
|
||||
profile = resolve(name, command.commandLine());
|
||||
} catch (ProfileNotFoundException e) {
|
||||
if (helpRequested(command)) return null;
|
||||
throw e;
|
||||
}
|
||||
for (String optionName : option.names()) {
|
||||
String value = ProfileDefaults.valueFor(profile, optionName);
|
||||
if (value != null) return value;
|
||||
|
||||
@@ -22,6 +22,7 @@ public class FlociHttpClient {
|
||||
public static final String DEFAULT_CONTROL_PREFIX = "/_floci";
|
||||
|
||||
private final String endpoint;
|
||||
private final String query;
|
||||
private final String controlPrefix;
|
||||
private final HttpClient http;
|
||||
|
||||
@@ -34,7 +35,14 @@ public class FlociHttpClient {
|
||||
* (e.g. {@code /_floci} for AWS/Azure, {@code /_floci-gcp} for GCP).
|
||||
*/
|
||||
public FlociHttpClient(String endpoint, String controlPrefix) {
|
||||
this.endpoint = endpoint.endsWith("/") ? endpoint.substring(0, endpoint.length() - 1) : endpoint;
|
||||
// Request paths go before an endpoint's query: http://h:1/?x=1 + /health is http://h:1/health?x=1.
|
||||
// A fragment is never sent, so it is dropped.
|
||||
int fragment = endpoint.indexOf('#');
|
||||
String base = fragment >= 0 ? endpoint.substring(0, fragment) : endpoint;
|
||||
int query = base.indexOf('?');
|
||||
this.query = query >= 0 ? base.substring(query) : "";
|
||||
base = query >= 0 ? base.substring(0, query) : base;
|
||||
this.endpoint = base.endsWith("/") ? base.substring(0, base.length() - 1) : base;
|
||||
this.controlPrefix = controlPrefix.endsWith("/")
|
||||
? controlPrefix.substring(0, controlPrefix.length() - 1)
|
||||
: controlPrefix;
|
||||
@@ -98,7 +106,7 @@ public class FlociHttpClient {
|
||||
public boolean isReachable() {
|
||||
try {
|
||||
HttpRequest req = HttpRequest.newBuilder()
|
||||
.uri(URI.create(endpoint + controlPrefix + "/health"))
|
||||
.uri(url(controlPrefix + "/health"))
|
||||
.timeout(Duration.ofSeconds(3))
|
||||
.GET()
|
||||
.build();
|
||||
@@ -117,7 +125,7 @@ public class FlociHttpClient {
|
||||
String path = controlPrefix + "/tls-cert";
|
||||
try {
|
||||
HttpRequest req = HttpRequest.newBuilder()
|
||||
.uri(URI.create(endpoint + path))
|
||||
.uri(url(path))
|
||||
.timeout(Duration.ofSeconds(10))
|
||||
.GET()
|
||||
.build();
|
||||
@@ -169,7 +177,7 @@ public class FlociHttpClient {
|
||||
private JsonNode getJson(String path, Duration timeout) throws FlociException {
|
||||
try {
|
||||
HttpRequest req = HttpRequest.newBuilder()
|
||||
.uri(URI.create(endpoint + path))
|
||||
.uri(url(path))
|
||||
.timeout(timeout)
|
||||
.header("Accept", "application/json")
|
||||
.GET()
|
||||
@@ -192,7 +200,7 @@ public class FlociHttpClient {
|
||||
private Map<String, Object> postJson(String path, String body) throws FlociException {
|
||||
try {
|
||||
HttpRequest req = HttpRequest.newBuilder()
|
||||
.uri(URI.create(endpoint + path))
|
||||
.uri(url(path))
|
||||
.timeout(Duration.ofSeconds(30))
|
||||
.header("Content-Type", "application/json")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(body))
|
||||
@@ -215,7 +223,7 @@ public class FlociHttpClient {
|
||||
private void deleteRequest(String path) throws FlociException {
|
||||
try {
|
||||
HttpRequest req = HttpRequest.newBuilder()
|
||||
.uri(URI.create(endpoint + path))
|
||||
.uri(url(path))
|
||||
.timeout(Duration.ofSeconds(10))
|
||||
.DELETE()
|
||||
.build();
|
||||
@@ -233,4 +241,8 @@ public class FlociHttpClient {
|
||||
public record HealthInfo(String version, String edition, String[] services) {}
|
||||
public record ServerInfo(String version, String edition) {}
|
||||
public record InitState(boolean boot, boolean start, boolean ready, boolean shutdown) {}
|
||||
|
||||
private URI url(String path) {
|
||||
return URI.create(endpoint + path + query);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user