diff --git a/AGENTS.md b/AGENTS.md index 1e7d796..2a3bf2f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -44,18 +44,26 @@ java -jar target/floci.jar Bare commands (`floci start`) route to the configured default product (`aws` unless changed via `floci config default-product aws|gcp|az|oci`). Explicit product groups are `floci aws` (same classes as the root tree), `floci gcp`, `floci az`, and `floci oci`. -### The four product trees — CRITICAL +### The unified product tree — CRITICAL -The GCP (`commands/gcp/`), Azure (`commands/az/`), and OCI (`commands/oci/`) command trees are near-verbatim copies of the root/AWS tree (`commands/`), each with its own `GlobalOptions` variant: +There is ONE implementation of every shared command, in `commands/`, parameterized by `ProductProfile` (`io.floci.cli.ProductProfile`) — the single source of per-product config: -| Tree | Mixin | Default endpoint | Container | Env prefix | Control prefix | -|------|-------|------------------|-----------|------------|----------------| -| root / `aws` | `GlobalOptions` | `http://localhost:4566` | `floci` | `FLOCI_*` | `/_floci` | -| `gcp` | `GcpGlobalOptions` | `http://localhost:4588` | `floci-gcp` | `FLOCI_GCP_*` | `/_floci-gcp` | -| `az` | `AzGlobalOptions` | `http://localhost:4577` | `floci-az` | `FLOCI_AZ_*` | `/_floci` (same as AWS — intentional) | -| `oci` | `OciGlobalOptions` | `http://localhost:4599` | `floci-oci` | `FLOCI_OCI_*` | `/_floci-oci` | +| Profile | Default endpoint | Container | Env prefix | Control prefix | +|---------|------------------|-----------|------------|----------------| +| `AWS` (root tree) | `http://localhost:4566` | `floci` | `FLOCI_*` | `/_floci` | +| `GCP` | `http://localhost:4588` | `floci-gcp` | `FLOCI_GCP_*` | `/_floci-gcp` | +| `AZ` | `http://localhost:4577` | `floci-az` | `FLOCI_AZ_*` | `/_floci` (same as AWS — intentional) | +| `OCI` | `http://localhost:4599` | `floci-oci` | `FLOCI_OCI_*` | `/_floci-oci` | -**Until the trees are unified: any change to a shared command MUST be applied to all four trees** (e.g. `StartCommand`, `GcpStartCommand`, `AzStartCommand`, `OciStartCommand`). Drift between the copies is a known bug source — check the sibling trees before and after editing. Only `EnvCommand`/`GcpEnvCommand`/`AzEnvCommand`/`OciEnvCommand` are genuinely product-specific, plus the OCI-only `OciSetupCommand` (the OCI CLI/SDKs require a config file + signing key, so `floci oci setup` scaffolds them; no other tree needs an equivalent). +The `commands/gcp/`, `commands/az/`, and `commands/oci/` leaf classes are **~8-line shims**: `GcpStartCommand extends StartCommand { super(ProductProfile.GCP); }` with their own `@Command` annotation carrying the per-product description. `ProductProfileTest` pins every profile field; `ProductTreesParsingTest` pins each tree's defaults. + +Rules for the unified tree: + +- **Every parameterized command pre-initializes its mixin in the constructor**: `this.global = new GlobalOptions(profile)`. Picocli uses a non-null `@Mixin` field instance as-is; a command that forgets gets AWS defaults in every tree (the per-tree parse tests catch this). +- **Per-product option defaults are constructor-set field values**, not annotation `defaultValue`s (annotation strings are compile-time constants). Use `(default: ${DEFAULT-VALUE})` in the option description to render them in help. +- **Never subclass a group command** (`GcpCommand`, `*ConfigCommand`, `*SnapshotCommand` groups): picocli registers the `subcommands` arrays of both super and subclass — instant duplicate-name crash. Groups stay standalone registration-only classes. +- Product-varying strings come from the profile: `displayName()` for banners, `commandPrefix()` for hint strings, `envVar("SERVICES")` for container env vars, `controlPrefix()` for `FlociHttpClient`, `serverRepo()` for issue-tracker links. +- Only `EnvCommand`/`GcpEnvCommand`/`AzEnvCommand`/`OciEnvCommand` are genuinely product-specific (not shims), plus the OCI-only `OciSetupCommand` (the OCI CLI/SDKs require a config file + signing key, so `floci oci setup` scaffolds them; no other tree needs an equivalent). Commands call `global.printer()` at the start of `call()` — never store a `Printer` as a field. @@ -83,12 +91,9 @@ if (printer.format() != OutputFormat.text) { `Check` is a `@FunctionalInterface` taking `(endpoint, container)` and returning `CheckResult`. Order matters: Docker checks run before server checks because later checks depend on Docker being up. -The structure differs per tree (do not assume they match): +One `DoctorCommand` serves every tree: `dockerChecks(profile)` builds the 9 Docker/server checks (image and control prefix from the profile), and the constructor appends the product's companion list — `AWS_COMPANION_CHECKS` (AWS CLI checks), `AZ_COMPANION_CHECKS` (az CLI checks), empty for GCP/OCI. `DoctorCheckListTest` pins each product's composition and order. -- **AWS** (`commands/DoctorCommand.java`): `DOCKER_CHECKS` (static list of the 9 Docker/server checks) + `AWS_COMPANION_CHECKS` (AWS CLI checks), combined via constructor into an instance `allChecks` list. -- **GCP/Azure/OCI** (`GcpDoctorCommand`/`AzDoctorCommand`/`OciDoctorCommand`): a single static `ALL_CHECKS` list built in a static initializer. - -To add a check: create a class in `doctor/checks/`, then append it to the relevant list(s) — in all trees where it applies. +To add a check: create a class in `doctor/checks/`, then add it to `dockerChecks()` (all products) or the relevant companion list. ### Config profiles diff --git a/CHANGELOG.md b/CHANGELOG.md index 83e672c..b1aca37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,15 @@ This project uses [Semantic Versioning](https://semver.org/). ## [Unreleased] +### Changed + +- Internal: the four near-identical product command trees were unified into one `ProductProfile`-parameterized implementation — the `gcp`/`az`/`oci` command classes are now thin subclasses of the root commands, eliminating the copy-drift bug class +- **Snapshot commands are now real API calls in every product tree.** `floci gcp|az|oci snapshot save/load/list/delete` call the product server's `/snapshots` control-plane endpoints and degrade gracefully when the server doesn't support them yet — previously they printed a "not yet available" note and exited 0. `snapshot export`/`import` now exit 1 with an error in all four trees (previously exit 0 with a note on gcp/az/oci); CI scripts that relied on the silent success of stubbed snapshot commands will now see failures. Snapshot error messages point at the product's GitHub issue tracker instead of the maintainer-only TODO.md +- `floci gcp|az|oci logs` gained the `--service` flag and `services` gained `--mode`, matching the AWS tree (they had drifted apart); `version` on the AWS tree now prints the port-auto-detected endpoint in its "not reachable" hint like the other trees +- `services` and `snapshot save/load/list/delete` now auto-detect the endpoint from the running container's port mapping in every tree (like `status`/`wait`/`env` always did), so a container started with `--port ` is found without passing `--endpoint` +- Global `--endpoint`/`--container` help text now shows each product's default value +- The AWS tree now brands itself **Floci AWS** in banners, messages, and help text ("Starting Floci AWS container...", "Floci AWS Doctor", "Show Floci AWS container and server status"), matching the Floci GCP / Floci Azure / Floci OCI naming + ### Added - `floci oci` command group — full lifecycle support for the Floci OCI (Oracle Cloud) emulator (`floci-oci`, default port 4599): `start`, `stop`, `restart`, `status`, `logs`, `wait`, `version`, `services`, `doctor`, `env`, `config`, and stub `snapshot` commands, mirroring the GCP/Azure trees. `floci oci env` exports `OCI_CLI_ENDPOINT`, `FLOCI_OCI_ENDPOINT`, and `TF_VAR_CLIENT_HOST_OVERRIDES` for the OCI CLI, the `ocilocal` wrapper, and the `oracle/oci` Terraform provider. `floci config default-product oci` routes bare commands to the OCI tree diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 714dbd5..b292055 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -50,7 +50,7 @@ java -jar target/floci.jar stop ## Architecture -See [AGENTS.md](AGENTS.md) for the architecture: command wiring, the four product trees (AWS/GCP/Azure/OCI), the Docker and HTTP I/O boundaries, the output pipeline, native-image constraints, and scope rules. +See [AGENTS.md](AGENTS.md) for the architecture: command wiring, the unified `ProductProfile`-parameterized command tree (AWS/GCP/Azure/OCI), the Docker and HTTP I/O boundaries, the output pipeline, native-image constraints, and scope rules. `AGENTS.md` is the canonical agent instructions file for this repository, following the [AGENTS.md standard](https://agents.md/). If your coding agent expects a different filename, create a local symlink instead of copying the file: @@ -61,7 +61,7 @@ ln -s AGENTS.md GEMINI.md ### Rules that trip up most PRs -- **Four product trees.** The `gcp`, `az`, and `oci` command trees mirror the root/AWS tree. A change to a shared command (start, stop, status, logs, wait, doctor, …) must be applied to **all four trees** — `StartCommand`, `GcpStartCommand`, `AzStartCommand`, and `OciStartCommand` — until they are unified. +- **One parameterized command tree.** Shared commands live once in `commands/`, parameterized by `ProductProfile`; the `gcp`/`az`/`oci` classes are thin shims (`GcpStartCommand extends StartCommand`). Change the base command and every tree gets it. New per-product values belong in `ProductProfile`, not in command code. Never subclass a group command (duplicate subcommand registration), and always pre-initialize the `GlobalOptions` mixin with the profile in command constructors. - **Native image.** New Jackson DTOs must be registered in `reflect-config.json`; prefer passing `Map`/`JsonNode` to `printer.structured(...)`, which needs no registration. No reflection-heavy dependencies. - **Error messages.** Every `printer.error(...)` must end with a suggested next step: diff --git a/README.md b/README.md index ea5d1d5..bf78634 100644 --- a/README.md +++ b/README.md @@ -584,7 +584,7 @@ floci snapshot export -o tarball.tar.gz floci snapshot import tarball.tar.gz ``` -> GCP, Azure, and OCI snapshots (`floci gcp snapshot` / `floci az snapshot` / `floci oci snapshot`) are not yet available — they require server-side endpoints not yet implemented in Floci GCP / Floci Azure / Floci OCI. `floci snapshot export|import` (AWS) are also pending server support. +> GCP, Azure, and OCI snapshots (`floci gcp snapshot` / `floci az snapshot` / `floci oci snapshot`) require server-side endpoints not yet implemented in Floci GCP / Floci Azure / Floci OCI — until then those commands report the missing API and exit 1. `floci snapshot export|import` (AWS) are also pending server support and exit 1. ### `floci update` diff --git a/src/main/java/io/floci/cli/AzGlobalOptions.java b/src/main/java/io/floci/cli/AzGlobalOptions.java deleted file mode 100644 index 673a73d..0000000 --- a/src/main/java/io/floci/cli/AzGlobalOptions.java +++ /dev/null @@ -1,80 +0,0 @@ -package io.floci.cli; - -import io.floci.cli.docker.DockerClient; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.OutputFormat; -import io.floci.cli.output.Printer; -import picocli.CommandLine.Option; - -import java.net.URI; - -public class AzGlobalOptions { - - @Option(names = {"--endpoint"}, - description = "Floci Azure server endpoint URL", - defaultValue = "${FLOCI_AZ_ENDPOINT:-http://localhost:4577}", - paramLabel = "") - public String endpoint; - - @Option(names = {"--container"}, - description = "Floci Azure container name", - defaultValue = "${FLOCI_AZ_CONTAINER:-floci-az}", - paramLabel = "") - public String container; - - @Option(names = {"--profile"}, - description = "Config profile from ~/.floci/profiles/", - paramLabel = "") - public String profile; - - @Option(names = {"--output", "-o"}, - description = "Output format: text, json, yaml", - defaultValue = "text", - paramLabel = "text|json|yaml") - public OutputFormat output; - - @Option(names = {"--quiet", "-q"}, description = "Suppress non-error output") - public boolean quiet; - - @Option(names = {"--verbose", "-v"}, description = "Debug logging to stderr") - public boolean verbose; - - @Option(names = {"--no-color"}, description = "Disable ANSI colors") - public boolean noColor; - - public Printer printer() { - if (noColor || System.console() == null) { - Ansi.disable(); - } - return new Printer(System.out, System.err, output, quiet); - } - - public String resolvedEndpoint(DockerClient docker) { - try { - return docker.inspectContainer(container) - .map(info -> endpointFromPorts(info.ports(), endpoint)) - .orElse(endpoint); - } catch (Exception e) { - return endpoint; - } - } - - public String endpointFromPorts(String ports, String fallback) { - if (ports == null || ports.isBlank()) return fallback; - try { - int containerPort = URI.create(fallback).getPort(); - if (containerPort == -1) containerPort = 4577; - for (String mapping : ports.trim().split("\\s+")) { - int arrow = mapping.indexOf("->"); - if (arrow < 0) continue; - String hostPort = mapping.substring(0, arrow); - String rest = mapping.substring(arrow + 2); - String cPort = rest.contains("/") ? rest.substring(0, rest.indexOf('/')) : rest; - if (String.valueOf(containerPort).equals(cPort)) { - return "http://localhost:" + hostPort; - } - } - } catch (Exception ignored) {} - return fallback; - } -} diff --git a/src/main/java/io/floci/cli/CliVersion.java b/src/main/java/io/floci/cli/CliVersion.java new file mode 100644 index 0000000..a86b654 --- /dev/null +++ b/src/main/java/io/floci/cli/CliVersion.java @@ -0,0 +1,23 @@ +package io.floci.cli; + +import java.io.InputStream; +import java.util.Properties; + +/** The CLI's own version, read from version.properties stamped at build time. */ +public final class CliVersion { + + public static final String CLI_VERSION = loadVersion(); + + private static String loadVersion() { + try (InputStream is = CliVersion.class.getResourceAsStream("/io/floci/cli/version.properties")) { + if (is == null) return "unknown"; + Properties props = new Properties(); + props.load(is); + return props.getProperty("version", "unknown"); + } catch (Exception e) { + return "unknown"; + } + } + + private CliVersion() {} +} diff --git a/src/main/java/io/floci/cli/FlociCli.java b/src/main/java/io/floci/cli/FlociCli.java index 1eab879..07acdee 100644 --- a/src/main/java/io/floci/cli/FlociCli.java +++ b/src/main/java/io/floci/cli/FlociCli.java @@ -55,7 +55,7 @@ public class FlociCli implements Runnable { static class VersionProvider implements IVersionProvider { @Override public String[] getVersion() { - return new String[]{"floci " + VersionCommand.CLI_VERSION}; + return new String[]{"floci " + CliVersion.CLI_VERSION}; } } @@ -95,9 +95,9 @@ public class FlociCli implements Runnable { // itself, which already talks about versions. boolean notify = UpdateNotifier.interactiveRunEnabled() && !isUpdateCommand(effectiveArgs); if (notify) { - UpdateNotifier.pendingNotice(VersionCommand.CLI_VERSION).ifPresent(latest -> + UpdateNotifier.pendingNotice(CliVersion.CLI_VERSION).ifPresent(latest -> System.err.println(Ansi.yellow("A new release of floci is available: " - + VersionCommand.CLI_VERSION + " → " + latest + "\n" + + CliVersion.CLI_VERSION + " → " + latest + "\n" + "Run 'floci update' to install it") + "\n")); UpdateNotifier.refreshInBackground(); } diff --git a/src/main/java/io/floci/cli/GcpGlobalOptions.java b/src/main/java/io/floci/cli/GcpGlobalOptions.java deleted file mode 100644 index 88d6400..0000000 --- a/src/main/java/io/floci/cli/GcpGlobalOptions.java +++ /dev/null @@ -1,83 +0,0 @@ -package io.floci.cli; - -import io.floci.cli.docker.DockerClient; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.OutputFormat; -import io.floci.cli.output.Printer; -import picocli.CommandLine.Option; - -import java.net.URI; - -public class GcpGlobalOptions { - - /** Control-plane path prefix exposed by the floci-gcp server (differs from AWS/Azure's {@code /_floci}). */ - public static final String CONTROL_PREFIX = "/_floci-gcp"; - - @Option(names = {"--endpoint"}, - description = "Floci GCP server endpoint URL", - defaultValue = "${FLOCI_GCP_ENDPOINT:-http://localhost:4588}", - paramLabel = "") - public String endpoint; - - @Option(names = {"--container"}, - description = "Floci GCP container name", - defaultValue = "${FLOCI_GCP_CONTAINER:-floci-gcp}", - paramLabel = "") - public String container; - - @Option(names = {"--profile"}, - description = "Config profile from ~/.floci/profiles/", - paramLabel = "") - public String profile; - - @Option(names = {"--output", "-o"}, - description = "Output format: text, json, yaml", - defaultValue = "text", - paramLabel = "text|json|yaml") - public OutputFormat output; - - @Option(names = {"--quiet", "-q"}, description = "Suppress non-error output") - public boolean quiet; - - @Option(names = {"--verbose", "-v"}, description = "Debug logging to stderr") - public boolean verbose; - - @Option(names = {"--no-color"}, description = "Disable ANSI colors") - public boolean noColor; - - public Printer printer() { - if (noColor || System.console() == null) { - Ansi.disable(); - } - return new Printer(System.out, System.err, output, quiet); - } - - public String resolvedEndpoint(DockerClient docker) { - try { - return docker.inspectContainer(container) - .map(info -> endpointFromPorts(info.ports(), endpoint)) - .orElse(endpoint); - } catch (Exception e) { - return endpoint; - } - } - - public String endpointFromPorts(String ports, String fallback) { - if (ports == null || ports.isBlank()) return fallback; - try { - int containerPort = URI.create(fallback).getPort(); - if (containerPort == -1) containerPort = 4588; - for (String mapping : ports.trim().split("\\s+")) { - int arrow = mapping.indexOf("->"); - if (arrow < 0) continue; - String hostPort = mapping.substring(0, arrow); - String rest = mapping.substring(arrow + 2); - String cPort = rest.contains("/") ? rest.substring(0, rest.indexOf('/')) : rest; - if (String.valueOf(containerPort).equals(cPort)) { - return "http://localhost:" + hostPort; - } - } - } catch (Exception ignored) {} - return fallback; - } -} \ No newline at end of file diff --git a/src/main/java/io/floci/cli/GlobalOptions.java b/src/main/java/io/floci/cli/GlobalOptions.java index 66fdf4f..9b212fd 100644 --- a/src/main/java/io/floci/cli/GlobalOptions.java +++ b/src/main/java/io/floci/cli/GlobalOptions.java @@ -7,18 +7,32 @@ import io.floci.cli.output.Printer; import picocli.CommandLine.Option; import java.net.URI; +import java.util.function.UnaryOperator; +/** + * Shared global options for every product tree, parameterized by {@link ProductProfile}. + * + * The per-product defaults for {@code --endpoint} and {@code --container} are set as + * field initial values in the constructor (env var override included) instead of + * annotation {@code defaultValue}s — annotation strings are compile-time constants and + * cannot vary by product. Picocli keeps a parsed-over field's initial value when the + * option has no defaultValue, and renders it for {@code ${DEFAULT-VALUE}} in help. + * + * Commands must pre-initialize their {@code @Mixin GlobalOptions} field in their + * constructor (picocli uses a non-null field instance as-is); a command that forgets + * gets AWS defaults — covered by the per-tree parse tests. + */ public class GlobalOptions { + public final ProductProfile product; + @Option(names = {"--endpoint"}, - description = "Floci server endpoint URL", - defaultValue = "${FLOCI_ENDPOINT:-http://localhost:4566}", + description = "Floci server endpoint URL (default: ${DEFAULT-VALUE})", paramLabel = "") public String endpoint; @Option(names = {"--container"}, - description = "Floci container name", - defaultValue = "${FLOCI_CONTAINER:-floci}", + description = "Floci container name (default: ${DEFAULT-VALUE})", paramLabel = "") public String container; @@ -42,6 +56,27 @@ public class GlobalOptions { @Option(names = {"--no-color"}, description = "Disable ANSI colors") public boolean noColor; + public GlobalOptions() { + this(ProductProfile.AWS); + } + + public GlobalOptions(ProductProfile product) { + this(product, System::getenv); + } + + /** Test seam: {@code envLookup} replaces {@code System.getenv}. */ + public GlobalOptions(ProductProfile product, UnaryOperator envLookup) { + this.product = product; + this.endpoint = envOr(envLookup, product.envVar("ENDPOINT"), product.defaultEndpoint()); + this.container = envOr(envLookup, product.envVar("CONTAINER"), product.defaultContainer()); + } + + // Matches picocli's ${VAR:-fallback}: only a NULL env var falls back — empty counts as set. + private static String envOr(UnaryOperator env, String var, String fallback) { + String value = env.apply(var); + return value != null ? value : fallback; + } + public Printer printer() { if (noColor || !isStdoutTty()) { Ansi.disable(); @@ -66,7 +101,7 @@ public class GlobalOptions { if (ports == null || ports.isBlank()) return fallback; try { int containerPort = URI.create(fallback).getPort(); - if (containerPort == -1) containerPort = 4566; + if (containerPort == -1) containerPort = product.defaultPort(); for (String mapping : ports.trim().split("\\s+")) { int arrow = mapping.indexOf("->"); if (arrow < 0) continue; diff --git a/src/main/java/io/floci/cli/OciGlobalOptions.java b/src/main/java/io/floci/cli/OciGlobalOptions.java deleted file mode 100644 index e38a346..0000000 --- a/src/main/java/io/floci/cli/OciGlobalOptions.java +++ /dev/null @@ -1,83 +0,0 @@ -package io.floci.cli; - -import io.floci.cli.docker.DockerClient; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.OutputFormat; -import io.floci.cli.output.Printer; -import picocli.CommandLine.Option; - -import java.net.URI; - -public class OciGlobalOptions { - - /** Control-plane path prefix exposed by the floci-oci server (differs from AWS/Azure's {@code /_floci}). */ - public static final String CONTROL_PREFIX = "/_floci-oci"; - - @Option(names = {"--endpoint"}, - description = "Floci OCI server endpoint URL", - defaultValue = "${FLOCI_OCI_ENDPOINT:-http://localhost:4599}", - paramLabel = "") - public String endpoint; - - @Option(names = {"--container"}, - description = "Floci OCI container name", - defaultValue = "${FLOCI_OCI_CONTAINER:-floci-oci}", - paramLabel = "") - public String container; - - @Option(names = {"--profile"}, - description = "Config profile from ~/.floci/profiles/", - paramLabel = "") - public String profile; - - @Option(names = {"--output", "-o"}, - description = "Output format: text, json, yaml", - defaultValue = "text", - paramLabel = "text|json|yaml") - public OutputFormat output; - - @Option(names = {"--quiet", "-q"}, description = "Suppress non-error output") - public boolean quiet; - - @Option(names = {"--verbose", "-v"}, description = "Debug logging to stderr") - public boolean verbose; - - @Option(names = {"--no-color"}, description = "Disable ANSI colors") - public boolean noColor; - - public Printer printer() { - if (noColor || System.console() == null) { - Ansi.disable(); - } - return new Printer(System.out, System.err, output, quiet); - } - - public String resolvedEndpoint(DockerClient docker) { - try { - return docker.inspectContainer(container) - .map(info -> endpointFromPorts(info.ports(), endpoint)) - .orElse(endpoint); - } catch (Exception e) { - return endpoint; - } - } - - public String endpointFromPorts(String ports, String fallback) { - if (ports == null || ports.isBlank()) return fallback; - try { - int containerPort = URI.create(fallback).getPort(); - if (containerPort == -1) containerPort = 4599; - for (String mapping : ports.trim().split("\\s+")) { - int arrow = mapping.indexOf("->"); - if (arrow < 0) continue; - String hostPort = mapping.substring(0, arrow); - String rest = mapping.substring(arrow + 2); - String cPort = rest.contains("/") ? rest.substring(0, rest.indexOf('/')) : rest; - if (String.valueOf(containerPort).equals(cPort)) { - return "http://localhost:" + hostPort; - } - } - } catch (Exception ignored) {} - return fallback; - } -} \ No newline at end of file diff --git a/src/main/java/io/floci/cli/ProductProfile.java b/src/main/java/io/floci/cli/ProductProfile.java index 78562b3..02b36a3 100644 --- a/src/main/java/io/floci/cli/ProductProfile.java +++ b/src/main/java/io/floci/cli/ProductProfile.java @@ -1,22 +1,54 @@ package io.floci.cli; +/** + * The single source of per-product configuration for the unified command tree. + * Every value that differs between the AWS, GCP, Azure, and OCI emulators lives here; + * command classes are parameterized with one of the four constants below. + */ public record ProductProfile( - String name, - String defaultImage, + String name, // CLI group name: "aws" | "gcp" | "az" | "oci" + String displayName, // banner name: "Floci" | "Floci GCP" | "Floci Azure" | "Floci OCI" + String image, // Docker image without tag String defaultContainer, int defaultPort, - String endpointEnvVar, - String containerEnvVar, - String persistPath, - String servicesEnvVar + String envPrefix, // FLOCI | FLOCI_GCP | FLOCI_AZ | FLOCI_OCI + String controlPrefix // control-plane path prefix on the server ) { - public static final ProductProfile AWS = new ProductProfile( - "aws", "floci/floci:latest", "floci", 4566, - "FLOCI_ENDPOINT", "FLOCI_CONTAINER", "/var/lib/floci", "FLOCI_SERVICES" - ); + public String defaultEndpoint() { + return "http://localhost:" + defaultPort; + } + + public String defaultImageRef() { + return image + ":latest"; + } + + /** {@code envVar("SERVICES")} → {@code FLOCI_GCP_SERVICES} */ + public String envVar(String suffix) { + return envPrefix + "_" + suffix; + } + + /** Command prefix for hint strings: "floci" for aws (the bare tree), "floci gcp" otherwise. */ + public String commandPrefix() { + return "aws".equals(name) ? "floci" : "floci " + name; + } + + /** GitHub repository of the product's emulator server, for issue-tracker hints. */ + public String serverRepo() { + return "https://github.com/floci-io/" + ("aws".equals(name) ? "floci" : "floci-" + name); + } + + public static final ProductProfile AWS = new ProductProfile( + "aws", "Floci AWS", "floci/floci", "floci", 4566, "FLOCI", "/_floci"); + + public static final ProductProfile GCP = new ProductProfile( + "gcp", "Floci GCP", "floci/floci-gcp", "floci-gcp", 4588, "FLOCI_GCP", "/_floci-gcp"); + + // AZ intentionally shares the AWS control prefix /_floci — the floci-az server + // exposes /_floci/*, NOT /_floci-az. Do not "fix" this to match the name. public static final ProductProfile AZ = new ProductProfile( - "az", "floci/floci-az:latest", "floci-az", 4577, - "FLOCI_AZ_ENDPOINT", "FLOCI_AZ_CONTAINER", "/app/data", "FLOCI_AZ_SERVICES" - ); + "az", "Floci Azure", "floci/floci-az", "floci-az", 4577, "FLOCI_AZ", "/_floci"); + + public static final ProductProfile OCI = new ProductProfile( + "oci", "Floci OCI", "floci/floci-oci", "floci-oci", 4599, "FLOCI_OCI", "/_floci-oci"); } diff --git a/src/main/java/io/floci/cli/commands/CompletionCommand.java b/src/main/java/io/floci/cli/commands/CompletionCommand.java index 6fe2219..b22f662 100644 --- a/src/main/java/io/floci/cli/commands/CompletionCommand.java +++ b/src/main/java/io/floci/cli/commands/CompletionCommand.java @@ -18,13 +18,19 @@ public class CompletionCommand implements Callable { @Mixin GlobalOptions global; + @Spec + Model.CommandSpec spec; + @Parameters(index = "0", description = "Shell type: bash, zsh, fish, powershell", paramLabel = "bash|zsh|fish|powershell") String shell; @Override public Integer call() { Printer printer = global.printer(); - CommandLine root = new CommandLine(new io.floci.cli.FlociCli()); + // The installed shell command is always `floci`, and the root tree contains every + // product subtree — so the root script is the correct completion no matter which + // group (root, aws, gcp, az, oci) this command was invoked under. + CommandLine root = spec.root().commandLine(); String script = switch (shell.toLowerCase()) { case "bash" -> AutoComplete.bash("floci", root); diff --git a/src/main/java/io/floci/cli/commands/DoctorCommand.java b/src/main/java/io/floci/cli/commands/DoctorCommand.java index 09973db..19f5937 100644 --- a/src/main/java/io/floci/cli/commands/DoctorCommand.java +++ b/src/main/java/io/floci/cli/commands/DoctorCommand.java @@ -1,12 +1,15 @@ package io.floci.cli.commands; import io.floci.cli.GlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.doctor.Check; import io.floci.cli.doctor.CheckResult; import io.floci.cli.doctor.CheckStatus; import io.floci.cli.docker.DockerClient; import io.floci.cli.doctor.checks.AwsCliEndpointCheck; import io.floci.cli.doctor.checks.AwsCliS3PathStyleCheck; +import io.floci.cli.doctor.checks.AzCliConnectionStringCheck; +import io.floci.cli.doctor.checks.AzCliInstalledCheck; import io.floci.cli.doctor.checks.ContainerRunningCheck; import io.floci.cli.doctor.checks.DockerDaemonCheck; import io.floci.cli.doctor.checks.DockerInstalledCheck; @@ -29,13 +32,15 @@ import java.util.concurrent.Callable; @Command( name = "doctor", - description = "Run environment diagnostics for Floci", + description = "Run environment diagnostics for Floci AWS", mixinStandardHelpOptions = true ) public class DoctorCommand implements Callable { + protected final ProductProfile profile; + @Mixin - GlobalOptions global; + protected GlobalOptions global; @Option(names = {"--check"}, description = "Run only a specific check by name", paramLabel = "") String checkName; @@ -43,35 +48,50 @@ public class DoctorCommand implements Callable { @Option(names = {"--fix"}, description = "Attempt to auto-fix fixable issues") boolean fix; - public static final List DOCKER_CHECKS = List.of( - new DockerInstalledCheck(), - new DockerDaemonCheck(), - new DockerSocketCheck(), - new DockerVersionCheck(), - new PortAvailableCheck(), - new ImagePresentCheck(), - new ImageVersionCheck(), - new ContainerRunningCheck(), - new EndpointReachableCheck() - ); + /** The base environment checks every product runs, in order: Docker first, then server. */ + public static List dockerChecks(ProductProfile profile) { + return List.of( + new DockerInstalledCheck(), + new DockerDaemonCheck(), + new DockerSocketCheck(), + new DockerVersionCheck(), + new PortAvailableCheck(), + new ImagePresentCheck(profile.image()), + new ImageVersionCheck(profile.image()), + new ContainerRunningCheck(), + new EndpointReachableCheck(profile.controlPrefix()) + ); + } public static final List AWS_COMPANION_CHECKS = List.of( new AwsCliEndpointCheck(), new AwsCliS3PathStyleCheck() ); + public static final List AZ_COMPANION_CHECKS = List.of( + new AzCliInstalledCheck(), + new AzCliConnectionStringCheck() + ); + private final List allChecks; public DoctorCommand() { - this(AWS_COMPANION_CHECKS); + this(ProductProfile.AWS, AWS_COMPANION_CHECKS); } - public DoctorCommand(List companionChecks) { - List checks = new ArrayList<>(DOCKER_CHECKS); + /** Test seam and shim constructor: base docker checks for the profile + product companions. */ + public DoctorCommand(ProductProfile profile, List companionChecks) { + this.profile = profile; + this.global = new GlobalOptions(profile); + List checks = new ArrayList<>(dockerChecks(profile)); checks.addAll(companionChecks); this.allChecks = List.copyOf(checks); } + public List allChecks() { + return allChecks; + } + @Override public Integer call() { Printer printer = global.printer(); @@ -81,7 +101,7 @@ public class DoctorCommand implements Callable { boolean textMode = printer.format() == OutputFormat.text; if (textMode) { - printer.println(Ansi.bold("Floci Doctor") + " — checking your environment"); + printer.println(Ansi.bold(profile.displayName() + " Doctor") + " — checking your environment"); printer.println(""); } diff --git a/src/main/java/io/floci/cli/commands/EnvCommand.java b/src/main/java/io/floci/cli/commands/EnvCommand.java index 7fa9fb0..066d506 100644 --- a/src/main/java/io/floci/cli/commands/EnvCommand.java +++ b/src/main/java/io/floci/cli/commands/EnvCommand.java @@ -14,7 +14,7 @@ import java.util.concurrent.Callable; @Command( name = "env", - description = "Print AWS environment variables to connect to Floci", + description = "Print AWS environment variables to connect to Floci AWS", mixinStandardHelpOptions = true ) public class EnvCommand implements Callable { diff --git a/src/main/java/io/floci/cli/commands/LogsCommand.java b/src/main/java/io/floci/cli/commands/LogsCommand.java index bfa6eff..84f6594 100644 --- a/src/main/java/io/floci/cli/commands/LogsCommand.java +++ b/src/main/java/io/floci/cli/commands/LogsCommand.java @@ -1,6 +1,7 @@ package io.floci.cli.commands; import io.floci.cli.GlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.docker.DockerClient; import io.floci.cli.docker.DockerException; import io.floci.cli.output.Printer; @@ -10,13 +11,24 @@ import java.util.concurrent.Callable; @Command( name = "logs", - description = "Fetch logs from the Floci container", + description = "Fetch logs from the Floci AWS container", mixinStandardHelpOptions = true ) public class LogsCommand implements Callable { + protected final ProductProfile profile; + @Mixin - GlobalOptions global; + protected GlobalOptions global; + + public LogsCommand() { + this(ProductProfile.AWS); + } + + protected LogsCommand(ProductProfile profile) { + this.profile = profile; + this.global = new GlobalOptions(profile); + } @Option(names = {"--follow", "-f"}, description = "Follow log output") boolean follow; @@ -38,7 +50,7 @@ public class LogsCommand implements Callable { try { var info = docker.inspectContainer(global.container); if (info.isEmpty()) { - printer.error("Container '" + global.container + "' not found.\nRun 'floci start' to launch one."); + printer.error("Container '" + global.container + "' not found.\nRun '" + profile.commandPrefix() + " start' to launch one."); return 1; } } catch (DockerException e) { diff --git a/src/main/java/io/floci/cli/commands/RestartCommand.java b/src/main/java/io/floci/cli/commands/RestartCommand.java index 050d068..d1689e6 100644 --- a/src/main/java/io/floci/cli/commands/RestartCommand.java +++ b/src/main/java/io/floci/cli/commands/RestartCommand.java @@ -1,6 +1,7 @@ package io.floci.cli.commands; import io.floci.cli.GlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.output.Printer; import picocli.CommandLine.*; @@ -8,19 +9,30 @@ import java.util.concurrent.Callable; @Command( name = "restart", - description = "Stop and restart the Floci container", + description = "Stop and restart the Floci AWS container", mixinStandardHelpOptions = true ) public class RestartCommand implements Callable { + protected final ProductProfile profile; + @Mixin - GlobalOptions global; + protected GlobalOptions global; + + public RestartCommand() { + this(ProductProfile.AWS); + } + + protected RestartCommand(ProductProfile profile) { + this.profile = profile; + this.global = new GlobalOptions(profile); + } @Override public Integer call() { Printer printer = global.printer(); - StopCommand stop = new StopCommand(); + StopCommand stop = new StopCommand(profile); stop.global = global; stop.remove = false; stop.timeout = 10; @@ -30,11 +42,11 @@ public class RestartCommand implements Callable { // Minimal wait to avoid port-already-in-use races try { Thread.sleep(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } - StartCommand start = new StartCommand(); + StartCommand start = new StartCommand(profile); start.global = global; - start.port = 4566; + start.port = profile.defaultPort(); start.pull = "missing"; - start.image = "floci/floci:latest"; + start.image = profile.defaultImageRef(); start.detach = false; return start.call(); } diff --git a/src/main/java/io/floci/cli/commands/ServicesCommand.java b/src/main/java/io/floci/cli/commands/ServicesCommand.java index 03f79a2..7fb7f7a 100644 --- a/src/main/java/io/floci/cli/commands/ServicesCommand.java +++ b/src/main/java/io/floci/cli/commands/ServicesCommand.java @@ -1,6 +1,8 @@ package io.floci.cli.commands; import io.floci.cli.GlobalOptions; +import io.floci.cli.ProductProfile; +import io.floci.cli.docker.DockerClient; import io.floci.cli.http.FlociHttpClient; import io.floci.cli.output.Ansi; import io.floci.cli.output.OutputFormat; @@ -14,13 +16,24 @@ import java.util.concurrent.Callable; @Command( name = "services", - description = "List services available in the running Floci instance", + description = "List services available in the running Floci AWS instance", mixinStandardHelpOptions = true ) public class ServicesCommand implements Callable { + protected final ProductProfile profile; + @Mixin - GlobalOptions global; + protected GlobalOptions global; + + public ServicesCommand() { + this(ProductProfile.AWS); + } + + protected ServicesCommand(ProductProfile profile) { + this.profile = profile; + this.global = new GlobalOptions(profile); + } @Option(names = {"--mode"}, description = "Filter mode: docker, in-process, all (default: all)", defaultValue = "all", paramLabel = "docker|in-process|all") String mode; @@ -28,14 +41,18 @@ public class ServicesCommand implements Callable { @Override public Integer call() { Printer printer = global.printer(); - FlociHttpClient client = new FlociHttpClient(global.endpoint); + // Resolve the endpoint from the container's port mapping, like status/wait/env do, + // so a container started with --port is still found without --endpoint. + String effectiveEndpoint = global.resolvedEndpoint(new DockerClient()); + FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile.controlPrefix()); List services; try { var health = client.health(); services = Arrays.asList(health.services()); } catch (Exception e) { - printer.error("Could not reach Floci at " + global.endpoint + ".\nIs Floci running? Try 'floci start'."); + printer.error("Could not reach " + profile.displayName() + " at " + effectiveEndpoint + + ".\nIs " + profile.displayName() + " running? Try '" + profile.commandPrefix() + " start'."); return 1; } @@ -44,7 +61,7 @@ public class ServicesCommand implements Callable { return 0; } - printer.println(Ansi.bold("Floci Services") + " " + Ansi.gray("(" + services.size() + " enabled)")); + printer.println(Ansi.bold(profile.displayName() + " Services") + " " + Ansi.gray("(" + services.size() + " enabled)")); printer.println(""); for (String svc : services) { printer.println(" " + Ansi.green("✓") + " " + svc); diff --git a/src/main/java/io/floci/cli/commands/StartCommand.java b/src/main/java/io/floci/cli/commands/StartCommand.java index 7189b0a..99b76e6 100644 --- a/src/main/java/io/floci/cli/commands/StartCommand.java +++ b/src/main/java/io/floci/cli/commands/StartCommand.java @@ -1,6 +1,7 @@ package io.floci.cli.commands; import io.floci.cli.GlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.docker.DockerClient; import io.floci.cli.docker.DockerException; import io.floci.cli.output.Ansi; @@ -13,15 +14,28 @@ import java.util.concurrent.Callable; @Command( name = "start", - description = "Start the Floci container", + description = "Start the Floci AWS emulator container", mixinStandardHelpOptions = true ) public class StartCommand implements Callable { - @Mixin - GlobalOptions global; + protected final ProductProfile profile; - @Option(names = {"--port"}, description = "Host port to bind (default: 4566)", defaultValue = "4566", paramLabel = "") + @Mixin + protected GlobalOptions global; + + public StartCommand() { + this(ProductProfile.AWS); + } + + protected StartCommand(ProductProfile profile) { + this.profile = profile; + this.global = new GlobalOptions(profile); + this.port = profile.defaultPort(); + this.image = profile.defaultImageRef(); + } + + @Option(names = {"--port"}, description = "Host port to bind (default: ${DEFAULT-VALUE})", paramLabel = "") int port; @Option(names = {"--persist"}, description = "Host directory for persistent state", paramLabel = "") @@ -33,7 +47,7 @@ public class StartCommand implements Callable { @Option(names = {"--detach"}, description = "Return immediately without waiting for readiness") boolean detach; - @Option(names = {"--image"}, description = "Image reference to use (default: floci/floci:latest)", defaultValue = "floci/floci:latest", paramLabel = "") + @Option(names = {"--image"}, description = "Image reference to use (default: ${DEFAULT-VALUE})", paramLabel = "") String image; @Option(names = {"--pull"}, description = "Image pull policy: always, missing, never", defaultValue = "missing", paramLabel = "always|missing|never") @@ -56,7 +70,7 @@ public class StartCommand implements Callable { if (existing.isPresent()) { String state = existing.get().state(); if ("running".equals(state)) { - printer.error("Container '" + global.container + "' is already running.\nRun 'floci stop' first or pass --container to use a different name."); + printer.error("Container '" + global.container + "' is already running.\nRun '" + profile.commandPrefix() + " stop' first or pass --container to use a different name."); return 1; } // Remove stopped container so we can start fresh @@ -73,28 +87,28 @@ public class StartCommand implements Callable { printer.println(Ansi.gray("Checking image " + image + " (policy: " + pull + ")...")); docker.pull(image, pull); } catch (DockerException e) { - printer.error("Failed to pull image: " + e.getMessage() + "\nRun 'floci start --pull never' to skip pulling."); + printer.error("Failed to pull image: " + e.getMessage() + "\nRun '" + profile.commandPrefix() + " start --pull never' to skip pulling."); return 1; } // Build docker run arguments List args = new ArrayList<>(); args.addAll(List.of("-d", "--name", global.container)); - args.addAll(List.of("-p", port + ":4566")); + args.addAll(List.of("-p", port + ":" + profile.defaultPort())); args.addAll(DockerClient.dockerSocketRunArgs()); if (persistDir != null) { args.addAll(List.of("-v", persistDir + ":/app/data")); // The server defaults to in-memory storage; enable persistent mode so // state is actually written to the mounted directory and survives restarts. - args.addAll(List.of("-e", "FLOCI_STORAGE_MODE=persistent")); + args.addAll(List.of("-e", profile.envVar("STORAGE_MODE") + "=persistent")); } if (services != null && !services.isBlank()) { - args.addAll(List.of("-e", "FLOCI_SERVICES=" + services)); + args.addAll(List.of("-e", profile.envVar("SERVICES") + "=" + services)); } args.add(image); try { - printer.println("Starting " + Ansi.gold("Floci") + " container..."); + printer.println("Starting " + Ansi.gold(profile.displayName()) + " container..."); String id = docker.startContainer(args); printer.println(Ansi.green("Container started") + " (" + id.substring(0, Math.min(12, id.length())) + ")"); } catch (DockerException e) { @@ -103,7 +117,7 @@ public class StartCommand implements Callable { } if (detach) { - printer.println(Ansi.gray("Detached. Run 'floci wait' to poll for readiness.")); + printer.println(Ansi.gray("Detached. Run '" + profile.commandPrefix() + " wait' to poll for readiness.")); return 0; } @@ -111,8 +125,8 @@ public class StartCommand implements Callable { global.endpoint = "http://localhost:" + port; // Wait for readiness - printer.println(Ansi.gray("Waiting for Floci to be ready...")); - WaitCommand wait = new WaitCommand(); + printer.println(Ansi.gray("Waiting for " + profile.displayName() + " to be ready...")); + WaitCommand wait = new WaitCommand(profile); wait.global = global; wait.timeout = "30s"; return wait.call(); diff --git a/src/main/java/io/floci/cli/commands/StatusCommand.java b/src/main/java/io/floci/cli/commands/StatusCommand.java index 99f70cf..37b77b8 100644 --- a/src/main/java/io/floci/cli/commands/StatusCommand.java +++ b/src/main/java/io/floci/cli/commands/StatusCommand.java @@ -1,6 +1,7 @@ package io.floci.cli.commands; import io.floci.cli.GlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.docker.DockerClient; import io.floci.cli.docker.DockerException; import io.floci.cli.http.FlociHttpClient; @@ -16,13 +17,24 @@ import java.util.concurrent.Callable; @Command( name = "status", - description = "Show Floci container and server status", + description = "Show Floci AWS container and server status", mixinStandardHelpOptions = true ) public class StatusCommand implements Callable { + protected final ProductProfile profile; + @Mixin - GlobalOptions global; + protected GlobalOptions global; + + public StatusCommand() { + this(ProductProfile.AWS); + } + + protected StatusCommand(ProductProfile profile) { + this.profile = profile; + this.global = new GlobalOptions(profile); + } @Override public Integer call() { @@ -50,7 +62,7 @@ public class StatusCommand implements Callable { String serverVersion = "unavailable"; String serverEdition = ""; boolean reachable = false; - FlociHttpClient client = new FlociHttpClient(effectiveEndpoint); + FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile.controlPrefix()); try { var health = client.health(); serverVersion = health.version(); @@ -73,7 +85,7 @@ public class StatusCommand implements Callable { return 0; } - printer.println(Ansi.bold("Floci Status")); + printer.println(Ansi.bold(profile.displayName() + " Status")); printer.println(""); String stateColor = switch (containerState) { case "running" -> Ansi.green(containerState); @@ -92,7 +104,7 @@ public class StatusCommand implements Callable { if (!reachable && !"running".equals(containerState)) { printer.println(""); - printer.println(Ansi.gray("Run 'floci start' to launch Floci.")); + printer.println(Ansi.gray("Run '" + profile.commandPrefix() + " start' to launch " + profile.displayName() + ".")); } return 0; diff --git a/src/main/java/io/floci/cli/commands/StopCommand.java b/src/main/java/io/floci/cli/commands/StopCommand.java index 4526775..ace8b66 100644 --- a/src/main/java/io/floci/cli/commands/StopCommand.java +++ b/src/main/java/io/floci/cli/commands/StopCommand.java @@ -1,6 +1,7 @@ package io.floci.cli.commands; import io.floci.cli.GlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.docker.DockerClient; import io.floci.cli.docker.DockerException; import io.floci.cli.output.Ansi; @@ -11,13 +12,24 @@ import java.util.concurrent.Callable; @Command( name = "stop", - description = "Stop the Floci container", + description = "Stop the Floci AWS container", mixinStandardHelpOptions = true ) public class StopCommand implements Callable { + protected final ProductProfile profile; + @Mixin - GlobalOptions global; + protected GlobalOptions global; + + public StopCommand() { + this(ProductProfile.AWS); + } + + protected StopCommand(ProductProfile profile) { + this.profile = profile; + this.global = new GlobalOptions(profile); + } @Option(names = {"--remove", "-r"}, description = "Remove the container after stopping") boolean remove; @@ -33,7 +45,7 @@ public class StopCommand implements Callable { try { var info = docker.inspectContainer(global.container); if (info.isEmpty()) { - printer.error("Container '" + global.container + "' not found.\nRun 'floci start' to launch one."); + printer.error("Container '" + global.container + "' not found.\nRun '" + profile.commandPrefix() + " start' to launch one."); return 1; } if (!"running".equals(info.get().state())) { @@ -50,7 +62,7 @@ public class StopCommand implements Callable { } try { - printer.println("Stopping " + Ansi.gold("Floci") + " container '" + global.container + "'..."); + printer.println("Stopping " + Ansi.gold(profile.displayName()) + " container '" + global.container + "'..."); docker.stopContainer(global.container, timeout); printer.println(Ansi.green("Stopped.")); } catch (DockerException e) { diff --git a/src/main/java/io/floci/cli/commands/UpdateCommand.java b/src/main/java/io/floci/cli/commands/UpdateCommand.java index 193e52b..6d61637 100644 --- a/src/main/java/io/floci/cli/commands/UpdateCommand.java +++ b/src/main/java/io/floci/cli/commands/UpdateCommand.java @@ -2,6 +2,7 @@ package io.floci.cli.commands; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import io.floci.cli.CliVersion; import io.floci.cli.output.Ansi; import io.floci.cli.output.OutputFormat; import io.floci.cli.output.Printer; @@ -72,7 +73,7 @@ public class UpdateCommand implements Callable { } Printer printer = new Printer(System.out, System.err, OutputFormat.text, false); try { - String current = VersionCommand.CLI_VERSION; + String current = CliVersion.CLI_VERSION; boolean pinned = to != null && !to.isBlank(); String target = pinned ? to.trim() : fetchLatestVersion(); diff --git a/src/main/java/io/floci/cli/commands/VersionCommand.java b/src/main/java/io/floci/cli/commands/VersionCommand.java index bcfc9d6..9b87eac 100644 --- a/src/main/java/io/floci/cli/commands/VersionCommand.java +++ b/src/main/java/io/floci/cli/commands/VersionCommand.java @@ -1,41 +1,39 @@ package io.floci.cli.commands; +import io.floci.cli.CliVersion; import io.floci.cli.GlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.http.FlociHttpClient; import io.floci.cli.output.Ansi; import io.floci.cli.output.OutputFormat; import io.floci.cli.output.Printer; import picocli.CommandLine.*; -import java.io.InputStream; import java.util.LinkedHashMap; import java.util.Map; import java.util.Optional; -import java.util.Properties; import java.util.concurrent.Callable; @Command( name = "version", - description = "Show CLI version, connected server version, and image digest", + description = "Show CLI version, connected Floci AWS server version, and image digest", mixinStandardHelpOptions = true ) public class VersionCommand implements Callable { - public static final String CLI_VERSION = loadVersion(); - - private static String loadVersion() { - try (InputStream is = VersionCommand.class.getResourceAsStream("/io/floci/cli/version.properties")) { - if (is == null) return "unknown"; - Properties props = new Properties(); - props.load(is); - return props.getProperty("version", "unknown"); - } catch (Exception e) { - return "unknown"; - } - } + protected final ProductProfile profile; @Mixin - GlobalOptions global; + protected GlobalOptions global; + + public VersionCommand() { + this(ProductProfile.AWS); + } + + protected VersionCommand(ProductProfile profile) { + this.profile = profile; + this.global = new GlobalOptions(profile); + } @Override public Integer call() { @@ -48,7 +46,7 @@ public class VersionCommand implements Callable { var docker = new io.floci.cli.docker.DockerClient(); String effectiveEndpoint = global.resolvedEndpoint(docker); - FlociHttpClient client = new FlociHttpClient(effectiveEndpoint); + FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile.controlPrefix()); try { var info = client.info(); serverVersion = info.version(); @@ -57,11 +55,11 @@ public class VersionCommand implements Callable { // Try to read image digest from Docker (best-effort) try { - imageDigest = docker.imageDigest("floci/floci").orElse(null); + imageDigest = docker.imageDigest(profile.image()).orElse(null); } catch (Exception ignored) {} Map data = new LinkedHashMap<>(); - data.put("cli", CLI_VERSION); + data.put("cli", CliVersion.CLI_VERSION); data.put("server", Optional.ofNullable(serverVersion).orElse("unavailable")); data.put("edition", Optional.ofNullable(serverEdition).orElse("")); if (imageDigest != null) data.put("digest", imageDigest); @@ -71,11 +69,13 @@ public class VersionCommand implements Callable { return 0; } - printer.println(Ansi.bold("Floci CLI") + " " + Ansi.gold(CLI_VERSION)); + printer.println(Ansi.bold("Floci CLI") + " " + Ansi.gold(CliVersion.CLI_VERSION)); if (serverVersion != null) { printer.println("Server: " + serverVersion + (serverEdition != null ? " (" + serverEdition + ")" : "")); } else { - printer.println("Server: " + Ansi.gray("not reachable at " + global.endpoint)); + // Effective endpoint (port auto-detected) — the gcp/az/oci trees always did + // this; the old AWS copy printed the raw --endpoint value. + printer.println("Server: " + Ansi.gray("not reachable at " + effectiveEndpoint)); } if (imageDigest != null) { printer.println("Image: " + imageDigest); diff --git a/src/main/java/io/floci/cli/commands/WaitCommand.java b/src/main/java/io/floci/cli/commands/WaitCommand.java index 0014d78..0fbb741 100644 --- a/src/main/java/io/floci/cli/commands/WaitCommand.java +++ b/src/main/java/io/floci/cli/commands/WaitCommand.java @@ -1,7 +1,9 @@ package io.floci.cli.commands; import io.floci.cli.GlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.http.FlociHttpClient; +import io.floci.cli.util.Durations; import io.floci.cli.output.Ansi; import io.floci.cli.output.OutputFormat; import io.floci.cli.output.Printer; @@ -15,13 +17,24 @@ import java.util.concurrent.Callable; @Command( name = "wait", - description = "Wait until Floci is ready to accept requests", + description = "Wait until Floci AWS is ready to accept requests", mixinStandardHelpOptions = true ) public class WaitCommand implements Callable { + protected final ProductProfile profile; + @Mixin - GlobalOptions global; + protected GlobalOptions global; + + public WaitCommand() { + this(ProductProfile.AWS); + } + + protected WaitCommand(ProductProfile profile) { + this.profile = profile; + this.global = new GlobalOptions(profile); + } @Option(names = {"--timeout"}, description = "Maximum time to wait (e.g. 30s, 2m)", defaultValue = "30s", paramLabel = "") String timeout; @@ -32,9 +45,9 @@ public class WaitCommand implements Callable { @Override public Integer call() { Printer printer = global.printer(); - long timeoutMillis = parseDuration(timeout); + long timeoutMillis = Durations.parseDuration(timeout); String effectiveEndpoint = global.resolvedEndpoint(new io.floci.cli.docker.DockerClient()); - FlociHttpClient client = new FlociHttpClient(effectiveEndpoint); + FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile.controlPrefix()); Instant deadline = Instant.now().plusMillis(timeoutMillis); while (Instant.now().isBefore(deadline)) { @@ -42,7 +55,7 @@ public class WaitCommand implements Callable { if (printer.format() != OutputFormat.text) { printer.structured(Map.of("ready", true, "endpoint", effectiveEndpoint)); } else { - printer.println(Ansi.green("Floci is ready") + " (" + effectiveEndpoint + ")"); + printer.println(Ansi.green(profile.displayName() + " is ready") + " (" + effectiveEndpoint + ")"); } return 0; } @@ -53,7 +66,8 @@ public class WaitCommand implements Callable { } } - printer.error("Timed out waiting for Floci after " + timeout + ".\nIs the container running? Try 'floci status'."); + printer.error("Timed out waiting for " + profile.displayName() + " after " + timeout + + ".\nIs the container running? Try '" + profile.commandPrefix() + " status'."); return 1; } @@ -75,13 +89,4 @@ public class WaitCommand implements Callable { printer.print("\r" + Ansi.gray("Waiting... (" + remaining + "s remaining)") + " "); } - public static long parseDuration(String s) { - if (s == null || s.isBlank()) return 30_000; - s = s.trim().toLowerCase(); - if (s.endsWith("ms")) return Long.parseLong(s.substring(0, s.length() - 2)); - if (s.endsWith("s")) return Long.parseLong(s.substring(0, s.length() - 1)) * 1000; - if (s.endsWith("m")) return Long.parseLong(s.substring(0, s.length() - 1)) * 60_000; - if (s.endsWith("h")) return Long.parseLong(s.substring(0, s.length() - 1)) * 3_600_000; - return Long.parseLong(s) * 1000; - } } diff --git a/src/main/java/io/floci/cli/commands/az/AzDoctorCommand.java b/src/main/java/io/floci/cli/commands/az/AzDoctorCommand.java index e424627..503ef85 100644 --- a/src/main/java/io/floci/cli/commands/az/AzDoctorCommand.java +++ b/src/main/java/io/floci/cli/commands/az/AzDoctorCommand.java @@ -1,116 +1,19 @@ package io.floci.cli.commands.az; -import io.floci.cli.AzGlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.commands.DoctorCommand; -import io.floci.cli.doctor.Check; -import io.floci.cli.doctor.CheckResult; -import io.floci.cli.doctor.CheckStatus; -import io.floci.cli.docker.DockerClient; -import io.floci.cli.doctor.checks.AzCliConnectionStringCheck; -import io.floci.cli.doctor.checks.AzCliInstalledCheck; -import io.floci.cli.doctor.checks.ContainerRunningCheck; -import io.floci.cli.doctor.checks.DockerDaemonCheck; -import io.floci.cli.doctor.checks.DockerInstalledCheck; -import io.floci.cli.doctor.checks.DockerSocketCheck; -import io.floci.cli.doctor.checks.DockerVersionCheck; -import io.floci.cli.doctor.checks.EndpointReachableCheck; -import io.floci.cli.doctor.checks.ImagePresentCheck; -import io.floci.cli.doctor.checks.ImageVersionCheck; -import io.floci.cli.doctor.checks.PortAvailableCheck; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.OutputFormat; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; +import picocli.CommandLine.Command; -import java.util.ArrayList; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; -import java.util.concurrent.Callable; @Command( name = "doctor", description = "Run environment diagnostics for Floci Azure", mixinStandardHelpOptions = true ) -public class AzDoctorCommand implements Callable { +public class AzDoctorCommand extends DoctorCommand { - @Mixin - AzGlobalOptions global; - - @Option(names = {"--check"}, description = "Run only a specific check by name", paramLabel = "") - String checkName; - - @Option(names = {"--fix"}, description = "Attempt to auto-fix fixable issues") - boolean fix; - - public static final List AZ_COMPANION_CHECKS = List.of( - new AzCliInstalledCheck(), - new AzCliConnectionStringCheck() - ); - - private static final List ALL_CHECKS; - - static { - List checks = new ArrayList<>(); - checks.add(new DockerInstalledCheck()); - checks.add(new DockerDaemonCheck()); - checks.add(new DockerSocketCheck()); - checks.add(new DockerVersionCheck()); - checks.add(new PortAvailableCheck()); - checks.add(new ImagePresentCheck("floci/floci-az")); - checks.add(new ImageVersionCheck("floci/floci-az")); - checks.add(new ContainerRunningCheck()); - checks.add(new EndpointReachableCheck()); - checks.addAll(AZ_COMPANION_CHECKS); - ALL_CHECKS = List.copyOf(checks); - } - - @Override - public Integer call() { - Printer printer = global.printer(); - List results = new ArrayList<>(); - String effectiveEndpoint = global.resolvedEndpoint(new DockerClient()); - - boolean textMode = printer.format() == OutputFormat.text; - - if (textMode) { - printer.println(Ansi.bold("Floci Azure Doctor") + " — checking your environment"); - printer.println(""); - } - - for (Check check : ALL_CHECKS) { - CheckResult result = check.run(effectiveEndpoint, global.container); - if (checkName != null && !result.name().equals(checkName)) continue; - results.add(result); - if (textMode) DoctorCommand.printResult(printer, result); - } - - if (!textMode) { - List> structured = new ArrayList<>(); - for (CheckResult r : results) { - Map m = new LinkedHashMap<>(); - m.put("name", r.name()); - m.put("status", r.status().name()); - m.put("message", r.message()); - if (r.fix() != null) m.put("fix", r.fix()); - structured.add(m); - } - printer.structured(structured); - return 0; - } - - long fails = results.stream().filter(r -> r.status() == CheckStatus.fail).count(); - long warns = results.stream().filter(r -> r.status() == CheckStatus.warn).count(); - - printer.println(""); - if (fails == 0 && warns == 0) { - printer.println(Ansi.green("All checks passed.")); - } else { - printer.println(fails + " issue(s) found (" + fails + " fail, " + warns + " warn)." - + (fix ? "" : " Run with --fix to auto-resolve fixable issues.")); - } - - return fails > 0 ? 1 : 0; + public AzDoctorCommand() { + super(ProductProfile.AZ, AZ_COMPANION_CHECKS); } } diff --git a/src/main/java/io/floci/cli/commands/az/AzEnvCommand.java b/src/main/java/io/floci/cli/commands/az/AzEnvCommand.java index ed4f86d..99c2e1c 100644 --- a/src/main/java/io/floci/cli/commands/az/AzEnvCommand.java +++ b/src/main/java/io/floci/cli/commands/az/AzEnvCommand.java @@ -1,6 +1,7 @@ package io.floci.cli.commands.az; -import io.floci.cli.AzGlobalOptions; +import io.floci.cli.GlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.docker.DockerClient; import io.floci.cli.doctor.checks.AzCliConnectionStringCheck; import io.floci.cli.output.Ansi; @@ -31,7 +32,7 @@ public class AzEnvCommand implements Callable { Set.of("blob", "queue", "table", "functions", "app-config", "key-vault"); @Mixin - AzGlobalOptions global; + GlobalOptions global = new GlobalOptions(ProductProfile.AZ); @Option(names = {"--format"}, description = "Output mode: connection-string, sdk-vars (default: connection-string)", diff --git a/src/main/java/io/floci/cli/commands/az/AzLogsCommand.java b/src/main/java/io/floci/cli/commands/az/AzLogsCommand.java index d625616..ee8aaeb 100644 --- a/src/main/java/io/floci/cli/commands/az/AzLogsCommand.java +++ b/src/main/java/io/floci/cli/commands/az/AzLogsCommand.java @@ -1,54 +1,17 @@ package io.floci.cli.commands.az; -import io.floci.cli.AzGlobalOptions; -import io.floci.cli.docker.DockerClient; -import io.floci.cli.docker.DockerException; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.LogsCommand; +import picocli.CommandLine.Command; @Command( name = "logs", description = "Fetch logs from the Floci Azure container", mixinStandardHelpOptions = true ) -public class AzLogsCommand implements Callable { +public class AzLogsCommand extends LogsCommand { - @Mixin - AzGlobalOptions global; - - @Option(names = {"--follow", "-f"}, description = "Follow log output") - boolean follow; - - @Option(names = {"--tail"}, description = "Number of lines from the end (0 = all)", defaultValue = "100", paramLabel = "") - int tail; - - @Option(names = {"--since"}, description = "Show logs since duration (e.g. 5m, 1h) or timestamp", paramLabel = "") - String since; - - @Override - public Integer call() { - Printer printer = global.printer(); - DockerClient docker = new DockerClient(); - - try { - var info = docker.inspectContainer(global.container); - if (info.isEmpty()) { - printer.error("Container '" + global.container + "' not found.\nRun 'floci az start' to launch one."); - return 1; - } - } catch (DockerException e) { - printer.error("Failed to inspect container: " + e.getMessage()); - return 1; - } - - try { - docker.streamLogs(global.container, follow, tail, since); - return 0; - } catch (DockerException e) { - printer.error("Failed to fetch logs: " + e.getMessage()); - return 1; - } + public AzLogsCommand() { + super(ProductProfile.AZ); } } diff --git a/src/main/java/io/floci/cli/commands/az/AzRestartCommand.java b/src/main/java/io/floci/cli/commands/az/AzRestartCommand.java index b5e0444..6265974 100644 --- a/src/main/java/io/floci/cli/commands/az/AzRestartCommand.java +++ b/src/main/java/io/floci/cli/commands/az/AzRestartCommand.java @@ -1,40 +1,17 @@ package io.floci.cli.commands.az; -import io.floci.cli.AzGlobalOptions; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.RestartCommand; +import picocli.CommandLine.Command; @Command( name = "restart", description = "Stop and restart the Floci Azure container", mixinStandardHelpOptions = true ) -public class AzRestartCommand implements Callable { +public class AzRestartCommand extends RestartCommand { - @Mixin - AzGlobalOptions global; - - @Override - public Integer call() { - Printer printer = global.printer(); - - AzStopCommand stop = new AzStopCommand(); - stop.global = global; - stop.remove = false; - stop.timeout = 10; - int stopResult = stop.call(); - if (stopResult != 0) return stopResult; - - try { Thread.sleep(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } - - AzStartCommand start = new AzStartCommand(); - start.global = global; - start.port = 4577; - start.pull = "missing"; - start.image = "floci/floci-az:latest"; - start.detach = false; - return start.call(); + public AzRestartCommand() { + super(ProductProfile.AZ); } } diff --git a/src/main/java/io/floci/cli/commands/az/AzServicesCommand.java b/src/main/java/io/floci/cli/commands/az/AzServicesCommand.java index ec61217..ab85d65 100644 --- a/src/main/java/io/floci/cli/commands/az/AzServicesCommand.java +++ b/src/main/java/io/floci/cli/commands/az/AzServicesCommand.java @@ -1,55 +1,17 @@ package io.floci.cli.commands.az; -import io.floci.cli.AzGlobalOptions; -import io.floci.cli.http.FlociHttpClient; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.OutputFormat; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.ServicesCommand; +import picocli.CommandLine.Command; @Command( name = "services", description = "List services available in the running Floci Azure instance", mixinStandardHelpOptions = true ) -public class AzServicesCommand implements Callable { +public class AzServicesCommand extends ServicesCommand { - @Mixin - AzGlobalOptions global; - - @Override - public Integer call() { - Printer printer = global.printer(); - FlociHttpClient client = new FlociHttpClient(global.endpoint); - - List services; - try { - var health = client.health(); - services = Arrays.asList(health.services()); - } catch (Exception e) { - printer.error("Could not reach Floci Azure at " + global.endpoint + ".\nIs Floci Azure running? Try 'floci az start'."); - return 1; - } - - if (printer.format() != OutputFormat.text) { - printer.structured(Map.of("services", services, "count", services.size())); - return 0; - } - - printer.println(Ansi.bold("Floci Azure Services") + " " + Ansi.gray("(" + services.size() + " enabled)")); - printer.println(""); - for (String svc : services) { - printer.println(" " + Ansi.green("✓") + " " + svc); - } - if (services.isEmpty()) { - printer.println(" " + Ansi.gray("(no services reported)")); - } - - return 0; + public AzServicesCommand() { + super(ProductProfile.AZ); } } diff --git a/src/main/java/io/floci/cli/commands/az/AzStartCommand.java b/src/main/java/io/floci/cli/commands/az/AzStartCommand.java index dca1a3e..715859b 100644 --- a/src/main/java/io/floci/cli/commands/az/AzStartCommand.java +++ b/src/main/java/io/floci/cli/commands/az/AzStartCommand.java @@ -1,113 +1,17 @@ package io.floci.cli.commands.az; -import io.floci.cli.AzGlobalOptions; -import io.floci.cli.docker.DockerClient; -import io.floci.cli.docker.DockerException; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.StartCommand; +import picocli.CommandLine.Command; @Command( name = "start", description = "Start the Floci Azure emulator container", mixinStandardHelpOptions = true ) -public class AzStartCommand implements Callable { +public class AzStartCommand extends StartCommand { - @Mixin - AzGlobalOptions global; - - @Option(names = {"--port"}, description = "Host port to bind (default: 4577)", defaultValue = "4577", paramLabel = "") - int port; - - @Option(names = {"--persist"}, description = "Host directory for persistent state", paramLabel = "") - String persistDir; - - @Option(names = {"--services"}, description = "Comma-separated list of services to enable", paramLabel = "") - String services; - - @Option(names = {"--detach"}, description = "Return immediately without waiting for readiness") - boolean detach; - - @Option(names = {"--image"}, description = "Image reference to use (default: floci/floci-az:latest)", defaultValue = "floci/floci-az:latest", paramLabel = "") - String image; - - @Option(names = {"--pull"}, description = "Image pull policy: always, missing, never", defaultValue = "missing", paramLabel = "always|missing|never") - String pull; - - @Override - public Integer call() { - Printer printer = global.printer(); - DockerClient docker = new DockerClient(); - - if (!DockerClient.isInstalled()) { - printer.error("docker binary not found in PATH.\nInstall Docker Desktop from https://docs.docker.com/get-docker/"); - return 1; - } - - try { - var existing = docker.inspectContainer(global.container); - if (existing.isPresent()) { - String state = existing.get().state(); - if ("running".equals(state)) { - printer.error("Container '" + global.container + "' is already running.\nRun 'floci az stop' first or pass --container to use a different name."); - return 1; - } - printer.println(Ansi.gray("Removing stopped container '" + global.container + "'...")); - docker.removeContainer(global.container); - } - } catch (DockerException e) { - printer.error("Failed to inspect container: " + e.getMessage()); - return 1; - } - - try { - printer.println(Ansi.gray("Checking image " + image + " (policy: " + pull + ")...")); - docker.pull(image, pull); - } catch (DockerException e) { - printer.error("Failed to pull image: " + e.getMessage() + "\nRun 'floci az start --pull never' to skip pulling."); - return 1; - } - - List args = new ArrayList<>(); - args.addAll(List.of("-d", "--name", global.container)); - args.addAll(List.of("-p", port + ":4577")); - args.addAll(DockerClient.dockerSocketRunArgs()); - if (persistDir != null) { - args.addAll(List.of("-v", persistDir + ":/app/data")); - // The server defaults to in-memory storage; enable persistent mode so - // state is actually written to the mounted directory and survives restarts. - args.addAll(List.of("-e", "FLOCI_AZ_STORAGE_MODE=persistent")); - } - if (services != null && !services.isBlank()) { - args.addAll(List.of("-e", "FLOCI_AZ_SERVICES=" + services)); - } - args.add(image); - - try { - printer.println("Starting " + Ansi.gold("Floci Azure") + " container..."); - String id = docker.startContainer(args); - printer.println(Ansi.green("Container started") + " (" + id.substring(0, Math.min(12, id.length())) + ")"); - } catch (DockerException e) { - printer.error("Failed to start container: " + e.getMessage()); - return 1; - } - - if (detach) { - printer.println(Ansi.gray("Detached. Run 'floci az wait' to poll for readiness.")); - return 0; - } - - global.endpoint = "http://localhost:" + port; - - printer.println(Ansi.gray("Waiting for Floci Azure to be ready...")); - AzWaitCommand wait = new AzWaitCommand(); - wait.global = global; - wait.timeout = "30s"; - return wait.call(); + public AzStartCommand() { + super(ProductProfile.AZ); } } diff --git a/src/main/java/io/floci/cli/commands/az/AzStatusCommand.java b/src/main/java/io/floci/cli/commands/az/AzStatusCommand.java index 7751cb7..e630423 100644 --- a/src/main/java/io/floci/cli/commands/az/AzStatusCommand.java +++ b/src/main/java/io/floci/cli/commands/az/AzStatusCommand.java @@ -1,98 +1,17 @@ package io.floci.cli.commands.az; -import io.floci.cli.AzGlobalOptions; -import io.floci.cli.docker.DockerClient; -import io.floci.cli.docker.DockerException; -import io.floci.cli.http.FlociHttpClient; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.OutputFormat; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Optional; -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.StatusCommand; +import picocli.CommandLine.Command; @Command( name = "status", description = "Show Floci Azure container and server status", mixinStandardHelpOptions = true ) -public class AzStatusCommand implements Callable { +public class AzStatusCommand extends StatusCommand { - @Mixin - AzGlobalOptions global; - - @Override - public Integer call() { - Printer printer = global.printer(); - DockerClient docker = new DockerClient(); - - String containerState = "not found"; - String containerImage = ""; - String containerPorts = ""; - String effectiveEndpoint = global.endpoint; - try { - Optional info = docker.inspectContainer(global.container); - if (info.isPresent()) { - containerState = info.get().state(); - containerImage = info.get().image(); - containerPorts = info.get().ports(); - effectiveEndpoint = global.endpointFromPorts(containerPorts, global.endpoint); - } - } catch (DockerException e) { - containerState = "error: " + e.getMessage(); - } - - String serverVersion = "unavailable"; - String serverEdition = ""; - boolean reachable = false; - FlociHttpClient client = new FlociHttpClient(effectiveEndpoint); - try { - var health = client.health(); - serverVersion = health.version(); - serverEdition = health.edition(); - reachable = true; - } catch (Exception ignored) {} - - Map data = new LinkedHashMap<>(); - data.put("container", global.container); - data.put("state", containerState); - data.put("image", containerImage); - data.put("ports", containerPorts); - data.put("endpoint", effectiveEndpoint); - data.put("reachable", reachable); - data.put("version", serverVersion); - data.put("edition", serverEdition); - - if (printer.format() != OutputFormat.text) { - printer.structured(data); - return 0; - } - - printer.println(Ansi.bold("Floci Azure Status")); - printer.println(""); - String stateColor = switch (containerState) { - case "running" -> Ansi.green(containerState); - case "not found" -> Ansi.gray(containerState); - default -> Ansi.red(containerState); - }; - printer.println(" Container: " + global.container + " " + stateColor); - if (!containerImage.isBlank()) printer.println(" Image: " + containerImage); - if (!containerPorts.isBlank()) printer.println(" Ports: " + containerPorts); - printer.println(" Endpoint: " + effectiveEndpoint); - printer.println(" Reachable: " + (reachable ? Ansi.green("yes") : Ansi.red("no"))); - if (reachable) { - printer.println(" Version: " + serverVersion); - if (!serverEdition.isBlank()) printer.println(" Edition: " + serverEdition); - } - - if (!reachable && !"running".equals(containerState)) { - printer.println(""); - printer.println(Ansi.gray("Run 'floci az start' to launch Floci Azure.")); - } - - return 0; + public AzStatusCommand() { + super(ProductProfile.AZ); } } diff --git a/src/main/java/io/floci/cli/commands/az/AzStopCommand.java b/src/main/java/io/floci/cli/commands/az/AzStopCommand.java index dba11e1..f43f3ba 100644 --- a/src/main/java/io/floci/cli/commands/az/AzStopCommand.java +++ b/src/main/java/io/floci/cli/commands/az/AzStopCommand.java @@ -1,73 +1,17 @@ package io.floci.cli.commands.az; -import io.floci.cli.AzGlobalOptions; -import io.floci.cli.docker.DockerClient; -import io.floci.cli.docker.DockerException; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.StopCommand; +import picocli.CommandLine.Command; @Command( name = "stop", description = "Stop the Floci Azure container", mixinStandardHelpOptions = true ) -public class AzStopCommand implements Callable { +public class AzStopCommand extends StopCommand { - @Mixin - AzGlobalOptions global; - - @Option(names = {"--remove", "-r"}, description = "Remove the container after stopping") - boolean remove; - - @Option(names = {"--timeout"}, description = "Seconds to wait before forcefully killing (default: 10)", defaultValue = "10", paramLabel = "") - int timeout; - - @Override - public Integer call() { - Printer printer = global.printer(); - DockerClient docker = new DockerClient(); - - try { - var info = docker.inspectContainer(global.container); - if (info.isEmpty()) { - printer.error("Container '" + global.container + "' not found.\nRun 'floci az start' to launch one."); - return 1; - } - if (!"running".equals(info.get().state())) { - printer.println(Ansi.yellow("Container '" + global.container + "' is not running (state: " + info.get().state() + ")")); - if (remove) { - docker.removeContainer(global.container); - printer.println("Container removed."); - } - return 0; - } - } catch (DockerException e) { - printer.error("Failed to inspect container: " + e.getMessage()); - return 1; - } - - try { - printer.println("Stopping " + Ansi.gold("Floci Azure") + " container '" + global.container + "'..."); - docker.stopContainer(global.container, timeout); - printer.println(Ansi.green("Stopped.")); - } catch (DockerException e) { - printer.error("Failed to stop container: " + e.getMessage()); - return 1; - } - - if (remove) { - try { - docker.removeContainer(global.container); - printer.println("Container removed."); - } catch (DockerException e) { - printer.error("Failed to remove container: " + e.getMessage()); - return 1; - } - } - - return 0; + public AzStopCommand() { + super(ProductProfile.AZ); } } diff --git a/src/main/java/io/floci/cli/commands/az/AzVersionCommand.java b/src/main/java/io/floci/cli/commands/az/AzVersionCommand.java index 2015208..81e3617 100644 --- a/src/main/java/io/floci/cli/commands/az/AzVersionCommand.java +++ b/src/main/java/io/floci/cli/commands/az/AzVersionCommand.java @@ -1,72 +1,17 @@ package io.floci.cli.commands.az; -import io.floci.cli.AzGlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.commands.VersionCommand; -import io.floci.cli.docker.DockerClient; -import io.floci.cli.http.FlociHttpClient; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.OutputFormat; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Optional; -import java.util.concurrent.Callable; +import picocli.CommandLine.Command; @Command( name = "version", description = "Show CLI version, connected Floci Azure server version, and image digest", mixinStandardHelpOptions = true ) -public class AzVersionCommand implements Callable { +public class AzVersionCommand extends VersionCommand { - @Mixin - AzGlobalOptions global; - - @Override - public Integer call() { - Printer printer = global.printer(); - - String serverVersion = null; - String serverEdition = null; - String imageDigest = null; - - DockerClient docker = new DockerClient(); - String effectiveEndpoint = global.resolvedEndpoint(docker); - - FlociHttpClient client = new FlociHttpClient(effectiveEndpoint); - try { - var info = client.info(); - serverVersion = info.version(); - serverEdition = info.edition(); - } catch (Exception ignored) {} - - try { - imageDigest = docker.imageDigest("floci/floci-az").orElse(null); - } catch (Exception ignored) {} - - Map data = new LinkedHashMap<>(); - data.put("cli", VersionCommand.CLI_VERSION); - data.put("server", Optional.ofNullable(serverVersion).orElse("unavailable")); - data.put("edition", Optional.ofNullable(serverEdition).orElse("")); - if (imageDigest != null) data.put("digest", imageDigest); - - if (printer.format() != OutputFormat.text) { - printer.structured(data); - return 0; - } - - printer.println(Ansi.bold("Floci CLI") + " " + Ansi.gold(VersionCommand.CLI_VERSION)); - if (serverVersion != null) { - printer.println("Server: " + serverVersion + (serverEdition != null ? " (" + serverEdition + ")" : "")); - } else { - printer.println("Server: " + Ansi.gray("not reachable at " + effectiveEndpoint)); - } - if (imageDigest != null) { - printer.println("Image: " + imageDigest); - } - - return 0; + public AzVersionCommand() { + super(ProductProfile.AZ); } } diff --git a/src/main/java/io/floci/cli/commands/az/AzWaitCommand.java b/src/main/java/io/floci/cli/commands/az/AzWaitCommand.java index a06bc23..478b228 100644 --- a/src/main/java/io/floci/cli/commands/az/AzWaitCommand.java +++ b/src/main/java/io/floci/cli/commands/az/AzWaitCommand.java @@ -1,73 +1,17 @@ package io.floci.cli.commands.az; -import io.floci.cli.AzGlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.commands.WaitCommand; -import io.floci.cli.http.FlociHttpClient; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.OutputFormat; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.time.Duration; -import java.time.Instant; -import java.util.Map; -import java.util.concurrent.Callable; +import picocli.CommandLine.Command; @Command( name = "wait", description = "Wait until Floci Azure is ready to accept requests", mixinStandardHelpOptions = true ) -public class AzWaitCommand implements Callable { +public class AzWaitCommand extends WaitCommand { - @Mixin - AzGlobalOptions global; - - @Option(names = {"--timeout"}, description = "Maximum time to wait (e.g. 30s, 2m)", defaultValue = "30s", paramLabel = "") - String timeout; - - @Option(names = {"--service"}, description = "Wait until a specific service is enabled", paramLabel = "") - String service; - - @Override - public Integer call() { - Printer printer = global.printer(); - long timeoutMillis = WaitCommand.parseDuration(timeout); - String effectiveEndpoint = global.resolvedEndpoint(new io.floci.cli.docker.DockerClient()); - FlociHttpClient client = new FlociHttpClient(effectiveEndpoint); - Instant deadline = Instant.now().plusMillis(timeoutMillis); - - while (Instant.now().isBefore(deadline)) { - if (isReady(client, service)) { - if (printer.format() != OutputFormat.text) { - printer.structured(Map.of("ready", true, "endpoint", effectiveEndpoint)); - } else { - printer.println(Ansi.green("Floci Azure is ready") + " (" + effectiveEndpoint + ")"); - } - return 0; - } - long remaining = Duration.between(Instant.now(), deadline).toSeconds(); - printer.print("\r" + Ansi.gray("Waiting... (" + remaining + "s remaining)") + " "); - try { Thread.sleep(500); } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; - } - } - - printer.error("Timed out waiting for Floci Azure after " + timeout + ".\nIs the container running? Try 'floci az status'."); - return 1; - } - - private boolean isReady(FlociHttpClient client, String requiredService) { - try { - var health = client.health(); - if (requiredService == null) return true; - for (String s : health.services()) { - if (s.equalsIgnoreCase(requiredService)) return true; - } - return false; - } catch (Exception e) { - return false; - } + public AzWaitCommand() { + super(ProductProfile.AZ); } } diff --git a/src/main/java/io/floci/cli/commands/az/config/AzConfigProfileCommand.java b/src/main/java/io/floci/cli/commands/az/config/AzConfigProfileCommand.java index 1d3167c..6468e99 100644 --- a/src/main/java/io/floci/cli/commands/az/config/AzConfigProfileCommand.java +++ b/src/main/java/io/floci/cli/commands/az/config/AzConfigProfileCommand.java @@ -1,115 +1,17 @@ package io.floci.cli.commands.az.config; -import io.floci.cli.AzGlobalOptions; -import io.floci.cli.config.Profile; -import io.floci.cli.config.ProfileStore; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.io.IOException; -import java.util.List; -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.config.ConfigProfileCommand; +import picocli.CommandLine.Command; @Command( name = "profile", description = "Manage Floci Azure configuration profiles", - mixinStandardHelpOptions = true, - subcommands = {HelpCommand.class} + mixinStandardHelpOptions = true ) -public class AzConfigProfileCommand implements Callable { +public class AzConfigProfileCommand extends ConfigProfileCommand { - @Mixin - AzGlobalOptions global; - - @Parameters(index = "0", description = "Action: list, show, create, delete", paramLabel = "list|show|create|delete") - String action; - - @Parameters(index = "1", description = "Profile name (required for show, create, delete)", arity = "0..1", paramLabel = "") - String name; - - @Override - public Integer call() { - Printer printer = global.printer(); - ProfileStore store = new ProfileStore(); - - return switch (action.toLowerCase()) { - case "list" -> listProfiles(printer, store); - case "show" -> showProfile(printer, store); - case "create" -> createProfile(printer, store); - case "delete" -> deleteProfile(printer, store); - default -> { - printer.error("Unknown action '" + action + "'. Use: list, show, create, delete"); - yield 1; - } - }; - } - - private int listProfiles(Printer printer, ProfileStore store) { - try { - List profiles = store.list(); - if (profiles.isEmpty()) { - printer.println(Ansi.gray("No profiles found. Create one with: floci az config profile create ")); - return 0; - } - printer.println(Ansi.bold("Profiles:")); - for (Profile p : profiles) { - printer.println(" " + p.name + Ansi.gray(" " + (p.endpoint != null ? p.endpoint : ""))); - } - return 0; - } catch (IOException e) { - printer.error("Could not list profiles: " + e.getMessage()); - return 1; - } - } - - private int showProfile(Printer printer, ProfileStore store) { - if (name == null) { printer.error("Profile name required."); return 1; } - try { - var profile = store.get(name); - if (profile.isEmpty()) { printer.error("Profile '" + name + "' not found."); return 1; } - Profile p = profile.get(); - printer.println(Ansi.bold("Profile: ") + p.name); - if (p.endpoint != null) printer.println(" endpoint: " + p.endpoint); - if (p.container != null) printer.println(" container: " + p.container); - if (p.image != null) printer.println(" image: " + p.image); - if (p.port != null) printer.println(" port: " + p.port); - if (p.persistDir != null) printer.println(" persistDir: " + p.persistDir); - if (p.services != null) printer.println(" services: " + p.services); - return 0; - } catch (IOException e) { - printer.error("Could not read profile: " + e.getMessage()); - return 1; - } - } - - private int createProfile(Printer printer, ProfileStore store) { - if (name == null) { printer.error("Profile name required."); return 1; } - try { - if (store.get(name).isPresent()) { - printer.error("Profile '" + name + "' already exists. Delete it first or edit " + store.profileFile(name)); - return 1; - } - Profile p = new Profile(name); - store.save(p); - printer.println(Ansi.green("Created") + " profile '" + name + "' at " + store.profileFile(name)); - printer.println(Ansi.gray("Edit the file to customize endpoint, container, image, etc.")); - return 0; - } catch (IOException e) { - printer.error("Could not create profile: " + e.getMessage()); - return 1; - } - } - - private int deleteProfile(Printer printer, ProfileStore store) { - if (name == null) { printer.error("Profile name required."); return 1; } - try { - if (!store.delete(name)) { printer.error("Profile '" + name + "' not found."); return 1; } - printer.println(Ansi.green("Deleted") + " profile '" + name + "'"); - return 0; - } catch (IOException e) { - printer.error("Could not delete profile: " + e.getMessage()); - return 1; - } + public AzConfigProfileCommand() { + super(ProductProfile.AZ); } } diff --git a/src/main/java/io/floci/cli/commands/az/config/AzConfigShowCommand.java b/src/main/java/io/floci/cli/commands/az/config/AzConfigShowCommand.java index 6340534..7a7896b 100644 --- a/src/main/java/io/floci/cli/commands/az/config/AzConfigShowCommand.java +++ b/src/main/java/io/floci/cli/commands/az/config/AzConfigShowCommand.java @@ -1,70 +1,17 @@ package io.floci.cli.commands.az.config; -import io.floci.cli.AzGlobalOptions; -import io.floci.cli.config.Profile; -import io.floci.cli.config.ProfileStore; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.OutputFormat; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Optional; -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.config.ConfigShowCommand; +import picocli.CommandLine.Command; @Command( name = "show", description = "Show the active Floci Azure configuration", mixinStandardHelpOptions = true ) -public class AzConfigShowCommand implements Callable { +public class AzConfigShowCommand extends ConfigShowCommand { - @Mixin - AzGlobalOptions global; - - @Override - public Integer call() { - Printer printer = global.printer(); - ProfileStore store = new ProfileStore(); - - Profile active = new Profile(global.profile != null ? global.profile : "default"); - active.endpoint = global.endpoint; - active.container = global.container; - active.output = global.output != null ? global.output.name() : "text"; - - if (global.profile != null) { - try { - Optional loaded = store.get(global.profile); - if (loaded.isPresent()) { - active = loaded.get(); - } else { - printer.warn("Profile '" + global.profile + "' not found. Using defaults."); - } - } catch (IOException e) { - printer.warn("Could not load profile: " + e.getMessage()); - } - } - - Map data = new LinkedHashMap<>(); - data.put("profile", active.name); - data.put("endpoint", active.endpoint != null ? active.endpoint : global.endpoint); - data.put("container", active.container != null ? active.container : global.container); - data.put("output", active.output); - - if (printer.format() != OutputFormat.text) { - printer.structured(data); - return 0; - } - - printer.println(Ansi.bold("Active Configuration (Floci Azure)")); - printer.println(""); - printer.println(" Profile: " + active.name); - printer.println(" Endpoint: " + (active.endpoint != null ? active.endpoint : global.endpoint)); - printer.println(" Container: " + (active.container != null ? active.container : global.container)); - printer.println(" Output: " + (active.output != null ? active.output : "text")); - - return 0; + public AzConfigShowCommand() { + super(ProductProfile.AZ); } } diff --git a/src/main/java/io/floci/cli/commands/az/config/AzConfigValidateCommand.java b/src/main/java/io/floci/cli/commands/az/config/AzConfigValidateCommand.java index a326f79..78617e7 100644 --- a/src/main/java/io/floci/cli/commands/az/config/AzConfigValidateCommand.java +++ b/src/main/java/io/floci/cli/commands/az/config/AzConfigValidateCommand.java @@ -1,91 +1,17 @@ package io.floci.cli.commands.az.config; -import io.floci.cli.AzGlobalOptions; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.Callable; -import java.util.regex.Pattern; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.config.ConfigValidateCommand; +import picocli.CommandLine.Command; @Command( name = "validate", description = "Validate a docker-compose.yml file for Floci Azure compatibility", mixinStandardHelpOptions = true ) -public class AzConfigValidateCommand implements Callable { +public class AzConfigValidateCommand extends ConfigValidateCommand { - @Mixin - AzGlobalOptions global; - - @Option(names = {"--file", "-f"}, description = "Path to docker-compose file", paramLabel = "") - String file; - - @Override - public Integer call() { - Printer printer = global.printer(); - - Path composeFile = resolveComposeFile(); - if (composeFile == null) { - printer.error("No docker-compose file found. Pass --file or run in a directory with docker-compose.yml."); - return 1; - } - - String content; - try { - content = Files.readString(composeFile); - } catch (IOException e) { - printer.error("Could not read " + composeFile + ": " + e.getMessage()); - return 1; - } - - printer.println(Ansi.bold("Validating") + " " + composeFile); - printer.println(""); - - List issues = new ArrayList<>(); - List warnings = new ArrayList<>(); - - if (!content.contains("/var/run/docker.sock")) { - warnings.add("Missing /var/run/docker.sock volume mount — required for Azure Functions"); - } - - if (!Pattern.compile("4577\\s*:\\s*4577").matcher(content).find()) { - issues.add("Port mapping 4577:4577 not found — Floci Azure listens on 4577 by default"); - } - - if (!content.contains("floci/floci-az") && !content.contains("ghcr.io/floci-io/floci-az")) { - warnings.add("Floci Azure image reference not detected — ensure your service uses floci/floci-az"); - } - - for (String issue : issues) { - printer.println(" " + Ansi.red("✗") + " " + issue); - } - for (String warn : warnings) { - printer.println(" " + Ansi.yellow("⚠") + " " + warn); - } - if (issues.isEmpty() && warnings.isEmpty()) { - printer.println(" " + Ansi.green("✓") + " Compose file looks good"); - } - - printer.println(""); - if (!issues.isEmpty()) { - printer.println(issues.size() + " error(s) found."); - return 1; - } - return 0; - } - - private Path resolveComposeFile() { - if (file != null) return Path.of(file); - for (String name : List.of("docker-compose.yml", "docker-compose.yaml", "compose.yml", "compose.yaml")) { - Path p = Path.of(name); - if (Files.exists(p)) return p; - } - return null; + public AzConfigValidateCommand() { + super(ProductProfile.AZ, false, "Azure Functions"); } } diff --git a/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotCommand.java b/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotCommand.java index 7729791..c607b82 100644 --- a/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotCommand.java +++ b/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotCommand.java @@ -5,7 +5,7 @@ import picocli.CommandLine.*; @Command( name = "snapshot", - description = "Manage Floci Azure state snapshots (coming soon)", + description = "Manage Floci Azure state snapshots", mixinStandardHelpOptions = true, subcommands = { AzSnapshotSaveCommand.class, diff --git a/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotDeleteCommand.java b/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotDeleteCommand.java index 9a3493e..f34c7e1 100644 --- a/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotDeleteCommand.java +++ b/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotDeleteCommand.java @@ -1,28 +1,17 @@ package io.floci.cli.commands.az.snapshot; -import io.floci.cli.AzGlobalOptions; -import io.floci.cli.output.Ansi; -import picocli.CommandLine.*; - -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.snapshot.SnapshotDeleteCommand; +import picocli.CommandLine.Command; @Command( name = "delete", description = "Delete a Floci Azure snapshot", mixinStandardHelpOptions = true ) -public class AzSnapshotDeleteCommand implements Callable { +public class AzSnapshotDeleteCommand extends SnapshotDeleteCommand { - @Mixin - AzGlobalOptions global; - - @Parameters(index = "0", description = "Snapshot name", paramLabel = "") - String name; - - @Override - public Integer call() { - global.printer().println(Ansi.yellow("Snapshots are not yet available for Floci Azure.") + - "\nTrack progress: https://github.com/floci-io/floci-az/issues"); - return 0; + public AzSnapshotDeleteCommand() { + super(ProductProfile.AZ); } } diff --git a/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotExportCommand.java b/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotExportCommand.java index 6dfbb89..80d58b1 100644 --- a/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotExportCommand.java +++ b/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotExportCommand.java @@ -1,31 +1,17 @@ package io.floci.cli.commands.az.snapshot; -import io.floci.cli.AzGlobalOptions; -import io.floci.cli.output.Ansi; -import picocli.CommandLine.*; - -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.snapshot.SnapshotExportCommand; +import picocli.CommandLine.Command; @Command( name = "export", description = "Export a Floci Azure snapshot to a tarball file", mixinStandardHelpOptions = true ) -public class AzSnapshotExportCommand implements Callable { +public class AzSnapshotExportCommand extends SnapshotExportCommand { - @Mixin - AzGlobalOptions global; - - @Parameters(index = "0", description = "Snapshot name", paramLabel = "") - String name; - - @Option(names = {"--file", "-f"}, description = "Destination file path", required = true, paramLabel = "") - String outputFile; - - @Override - public Integer call() { - global.printer().println(Ansi.yellow("Snapshots are not yet available for Floci Azure.") + - "\nTrack progress: https://github.com/floci-io/floci-az/issues"); - return 0; + public AzSnapshotExportCommand() { + super(ProductProfile.AZ); } } diff --git a/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotImportCommand.java b/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotImportCommand.java index e471c19..fadb6d9 100644 --- a/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotImportCommand.java +++ b/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotImportCommand.java @@ -1,31 +1,17 @@ package io.floci.cli.commands.az.snapshot; -import io.floci.cli.AzGlobalOptions; -import io.floci.cli.output.Ansi; -import picocli.CommandLine.*; - -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.snapshot.SnapshotImportCommand; +import picocli.CommandLine.Command; @Command( name = "import", description = "Import a Floci Azure snapshot from a tarball file", mixinStandardHelpOptions = true ) -public class AzSnapshotImportCommand implements Callable { +public class AzSnapshotImportCommand extends SnapshotImportCommand { - @Mixin - AzGlobalOptions global; - - @Option(names = {"--file", "-f"}, description = "Source tarball path", required = true, paramLabel = "") - String inputFile; - - @Option(names = {"--name"}, description = "Name for the imported snapshot", paramLabel = "") - String name; - - @Override - public Integer call() { - global.printer().println(Ansi.yellow("Snapshots are not yet available for Floci Azure.") + - "\nTrack progress: https://github.com/floci-io/floci-az/issues"); - return 0; + public AzSnapshotImportCommand() { + super(ProductProfile.AZ); } } diff --git a/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotListCommand.java b/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotListCommand.java index ebc592d..138871b 100644 --- a/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotListCommand.java +++ b/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotListCommand.java @@ -1,34 +1,17 @@ package io.floci.cli.commands.az.snapshot; -import io.floci.cli.AzGlobalOptions; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.OutputFormat; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.List; -import java.util.Map; -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.snapshot.SnapshotListCommand; +import picocli.CommandLine.Command; @Command( name = "list", description = "List available Floci Azure snapshots", mixinStandardHelpOptions = true ) -public class AzSnapshotListCommand implements Callable { +public class AzSnapshotListCommand extends SnapshotListCommand { - @Mixin - AzGlobalOptions global; - - @Override - public Integer call() { - Printer printer = global.printer(); - if (printer.format() != OutputFormat.text) { - printer.structured(Map.of("snapshots", List.of(), "note", "not yet available")); - return 0; - } - printer.println(Ansi.gray("No snapshots found.") + - " (Snapshot support for Floci Azure is coming — https://github.com/floci-io/floci-az/issues)"); - return 0; + public AzSnapshotListCommand() { + super(ProductProfile.AZ); } } diff --git a/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotLoadCommand.java b/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotLoadCommand.java index 996e2e9..f4af673 100644 --- a/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotLoadCommand.java +++ b/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotLoadCommand.java @@ -1,28 +1,17 @@ package io.floci.cli.commands.az.snapshot; -import io.floci.cli.AzGlobalOptions; -import io.floci.cli.output.Ansi; -import picocli.CommandLine.*; - -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.snapshot.SnapshotLoadCommand; +import picocli.CommandLine.Command; @Command( name = "load", - description = "Restore a saved Floci Azure snapshot", + description = "Load a Floci Azure snapshot", mixinStandardHelpOptions = true ) -public class AzSnapshotLoadCommand implements Callable { +public class AzSnapshotLoadCommand extends SnapshotLoadCommand { - @Mixin - AzGlobalOptions global; - - @Parameters(index = "0", description = "Snapshot name", paramLabel = "") - String name; - - @Override - public Integer call() { - global.printer().println(Ansi.yellow("Snapshots are not yet available for Floci Azure.") + - "\nTrack progress: https://github.com/floci-io/floci-az/issues"); - return 0; + public AzSnapshotLoadCommand() { + super(ProductProfile.AZ); } } diff --git a/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotSaveCommand.java b/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotSaveCommand.java index 9375590..ca632bc 100644 --- a/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotSaveCommand.java +++ b/src/main/java/io/floci/cli/commands/az/snapshot/AzSnapshotSaveCommand.java @@ -1,32 +1,17 @@ package io.floci.cli.commands.az.snapshot; -import io.floci.cli.AzGlobalOptions; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.snapshot.SnapshotSaveCommand; +import picocli.CommandLine.Command; @Command( name = "save", description = "Save the current Floci Azure state as a named snapshot", mixinStandardHelpOptions = true ) -public class AzSnapshotSaveCommand implements Callable { +public class AzSnapshotSaveCommand extends SnapshotSaveCommand { - @Mixin - AzGlobalOptions global; - - @Parameters(index = "0", description = "Snapshot name", paramLabel = "") - String name; - - @Option(names = {"--message", "-m"}, description = "Optional description", paramLabel = "") - String message; - - @Override - public Integer call() { - global.printer().println(Ansi.yellow("Snapshots are not yet available for Floci Azure.") + - "\nTrack progress: https://github.com/floci-io/floci-az/issues"); - return 0; + public AzSnapshotSaveCommand() { + super(ProductProfile.AZ); } } diff --git a/src/main/java/io/floci/cli/commands/config/ConfigProfileCommand.java b/src/main/java/io/floci/cli/commands/config/ConfigProfileCommand.java index ae43479..97a33d1 100644 --- a/src/main/java/io/floci/cli/commands/config/ConfigProfileCommand.java +++ b/src/main/java/io/floci/cli/commands/config/ConfigProfileCommand.java @@ -1,6 +1,7 @@ package io.floci.cli.commands.config; import io.floci.cli.GlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.config.Profile; import io.floci.cli.config.ProfileStore; import io.floci.cli.output.Ansi; @@ -13,14 +14,25 @@ import java.util.concurrent.Callable; @Command( name = "profile", - description = "Manage Floci configuration profiles", + description = "Manage Floci AWS configuration profiles", mixinStandardHelpOptions = true, subcommands = {HelpCommand.class} ) public class ConfigProfileCommand implements Callable { + protected final ProductProfile profile; + @Mixin - GlobalOptions global; + protected GlobalOptions global; + + public ConfigProfileCommand() { + this(ProductProfile.AWS); + } + + protected ConfigProfileCommand(ProductProfile profile) { + this.profile = profile; + this.global = new GlobalOptions(profile); + } @Parameters(index = "0", description = "Action: list, show, create, delete", paramLabel = "list|show|create|delete") String action; @@ -49,7 +61,7 @@ public class ConfigProfileCommand implements Callable { try { List profiles = store.list(); if (profiles.isEmpty()) { - printer.println(Ansi.gray("No profiles found. Create one with: floci config profile create ")); + printer.println(Ansi.gray("No profiles found. Create one with: " + profile.commandPrefix() + " config profile create ")); return 0; } printer.println(Ansi.bold("Profiles:")); diff --git a/src/main/java/io/floci/cli/commands/config/ConfigShowCommand.java b/src/main/java/io/floci/cli/commands/config/ConfigShowCommand.java index 8e59a8f..e0444b2 100644 --- a/src/main/java/io/floci/cli/commands/config/ConfigShowCommand.java +++ b/src/main/java/io/floci/cli/commands/config/ConfigShowCommand.java @@ -1,6 +1,7 @@ package io.floci.cli.commands.config; import io.floci.cli.GlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.config.Profile; import io.floci.cli.config.ProfileStore; import io.floci.cli.output.Ansi; @@ -16,13 +17,24 @@ import java.util.concurrent.Callable; @Command( name = "show", - description = "Show the active configuration", + description = "Show the active Floci AWS configuration", mixinStandardHelpOptions = true ) public class ConfigShowCommand implements Callable { + protected final ProductProfile profile; + @Mixin - GlobalOptions global; + protected GlobalOptions global; + + public ConfigShowCommand() { + this(ProductProfile.AWS); + } + + protected ConfigShowCommand(ProductProfile profile) { + this.profile = profile; + this.global = new GlobalOptions(profile); + } @Override public Integer call() { @@ -58,7 +70,7 @@ public class ConfigShowCommand implements Callable { return 0; } - printer.println(Ansi.bold("Active Configuration")); + printer.println(Ansi.bold("Active Configuration (" + profile.displayName() + ")")); printer.println(""); printer.println(" Profile: " + active.name); printer.println(" Endpoint: " + (active.endpoint != null ? active.endpoint : global.endpoint)); diff --git a/src/main/java/io/floci/cli/commands/config/ConfigValidateCommand.java b/src/main/java/io/floci/cli/commands/config/ConfigValidateCommand.java index 604caff..b672df7 100644 --- a/src/main/java/io/floci/cli/commands/config/ConfigValidateCommand.java +++ b/src/main/java/io/floci/cli/commands/config/ConfigValidateCommand.java @@ -1,6 +1,7 @@ package io.floci.cli.commands.config; import io.floci.cli.GlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.output.Ansi; import io.floci.cli.output.Printer; import picocli.CommandLine.*; @@ -15,13 +16,28 @@ import java.util.regex.Pattern; @Command( name = "validate", - description = "Validate a docker-compose.yml file for Floci compatibility", + description = "Validate a docker-compose.yml file for Floci AWS compatibility", mixinStandardHelpOptions = true ) public class ConfigValidateCommand implements Callable { + protected final ProductProfile profile; + private final boolean dockerSockRequired; + private final String dockerSockNeededFor; + @Mixin - GlobalOptions global; + protected GlobalOptions global; + + public ConfigValidateCommand() { + this(ProductProfile.AWS, true, "Lambda, EC2, EKS, MSK, ECR, CodeBuild"); + } + + protected ConfigValidateCommand(ProductProfile profile, boolean dockerSockRequired, String dockerSockNeededFor) { + this.profile = profile; + this.dockerSockRequired = dockerSockRequired; + this.dockerSockNeededFor = dockerSockNeededFor; + this.global = new GlobalOptions(profile); + } @Option(names = {"--file", "-f"}, description = "Path to docker-compose file", paramLabel = "") String file; @@ -50,19 +66,24 @@ public class ConfigValidateCommand implements Callable { List issues = new ArrayList<>(); List warnings = new ArrayList<>(); - // Check for docker.sock mount + // Check for docker.sock mount (an error where the product needs it for core services) if (!content.contains("/var/run/docker.sock")) { - issues.add("Missing /var/run/docker.sock volume mount — required for Lambda, EC2, EKS, MSK, ECR, CodeBuild"); + (dockerSockRequired ? issues : warnings) + .add("Missing /var/run/docker.sock volume mount — required for " + dockerSockNeededFor); } // Check port mapping - if (!Pattern.compile("4566\\s*:\\s*4566").matcher(content).find()) { - issues.add("Port mapping 4566:4566 not found — Floci listens on 4566 by default"); + int port = profile.defaultPort(); + if (!Pattern.compile(port + "\\s*:\\s*" + port).matcher(content).find()) { + issues.add("Port mapping " + port + ":" + port + " not found — " + profile.displayName() + + " listens on " + port + " by default"); } - // Check for floci image - if (!content.contains("floci/floci") && !content.contains("ghcr.io/floci-io/floci")) { - warnings.add("Floci image reference not detected — ensure your service uses floci/floci or ghcr.io/floci-io/floci"); + // Check for the product image + String ghcrImage = "ghcr.io/floci-io/" + profile.image().substring(profile.image().indexOf('/') + 1); + if (!content.contains(profile.image()) && !content.contains(ghcrImage)) { + warnings.add(profile.displayName() + " image reference not detected — ensure your service uses " + + profile.image() + " or " + ghcrImage); } for (String issue : issues) { diff --git a/src/main/java/io/floci/cli/commands/gcp/GcpDoctorCommand.java b/src/main/java/io/floci/cli/commands/gcp/GcpDoctorCommand.java index 20c6b38..78c601d 100644 --- a/src/main/java/io/floci/cli/commands/gcp/GcpDoctorCommand.java +++ b/src/main/java/io/floci/cli/commands/gcp/GcpDoctorCommand.java @@ -1,108 +1,19 @@ package io.floci.cli.commands.gcp; -import io.floci.cli.GcpGlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.commands.DoctorCommand; -import io.floci.cli.doctor.Check; -import io.floci.cli.doctor.CheckResult; -import io.floci.cli.doctor.CheckStatus; -import io.floci.cli.docker.DockerClient; -import io.floci.cli.doctor.checks.ContainerRunningCheck; -import io.floci.cli.doctor.checks.DockerDaemonCheck; -import io.floci.cli.doctor.checks.DockerInstalledCheck; -import io.floci.cli.doctor.checks.DockerSocketCheck; -import io.floci.cli.doctor.checks.DockerVersionCheck; -import io.floci.cli.doctor.checks.EndpointReachableCheck; -import io.floci.cli.doctor.checks.ImagePresentCheck; -import io.floci.cli.doctor.checks.ImageVersionCheck; -import io.floci.cli.doctor.checks.PortAvailableCheck; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.OutputFormat; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; +import picocli.CommandLine.Command; -import java.util.ArrayList; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; -import java.util.concurrent.Callable; @Command( name = "doctor", description = "Run environment diagnostics for Floci GCP", mixinStandardHelpOptions = true ) -public class GcpDoctorCommand implements Callable { +public class GcpDoctorCommand extends DoctorCommand { - @Mixin - GcpGlobalOptions global; - - @Option(names = {"--check"}, description = "Run only a specific check by name", paramLabel = "") - String checkName; - - @Option(names = {"--fix"}, description = "Attempt to auto-fix fixable issues") - boolean fix; - - private static final List ALL_CHECKS; - - static { - List checks = new ArrayList<>(); - checks.add(new DockerInstalledCheck()); - checks.add(new DockerDaemonCheck()); - checks.add(new DockerSocketCheck()); - checks.add(new DockerVersionCheck()); - checks.add(new PortAvailableCheck()); - checks.add(new ImagePresentCheck("floci/floci-gcp")); - checks.add(new ImageVersionCheck("floci/floci-gcp")); - checks.add(new ContainerRunningCheck()); - checks.add(new EndpointReachableCheck(io.floci.cli.GcpGlobalOptions.CONTROL_PREFIX)); - ALL_CHECKS = List.copyOf(checks); - } - - @Override - public Integer call() { - Printer printer = global.printer(); - List results = new ArrayList<>(); - String effectiveEndpoint = global.resolvedEndpoint(new DockerClient()); - - boolean textMode = printer.format() == OutputFormat.text; - - if (textMode) { - printer.println(Ansi.bold("Floci GCP Doctor") + " — checking your environment"); - printer.println(""); - } - - for (Check check : ALL_CHECKS) { - CheckResult result = check.run(effectiveEndpoint, global.container); - if (checkName != null && !result.name().equals(checkName)) continue; - results.add(result); - if (textMode) DoctorCommand.printResult(printer, result); - } - - if (!textMode) { - List> structured = new ArrayList<>(); - for (CheckResult r : results) { - Map m = new LinkedHashMap<>(); - m.put("name", r.name()); - m.put("status", r.status().name()); - m.put("message", r.message()); - if (r.fix() != null) m.put("fix", r.fix()); - structured.add(m); - } - printer.structured(structured); - return 0; - } - - long fails = results.stream().filter(r -> r.status() == CheckStatus.fail).count(); - long warns = results.stream().filter(r -> r.status() == CheckStatus.warn).count(); - - printer.println(""); - if (fails == 0 && warns == 0) { - printer.println(Ansi.green("All checks passed.")); - } else { - printer.println(fails + " issue(s) found (" + fails + " fail, " + warns + " warn)." - + (fix ? "" : " Run with --fix to auto-resolve fixable issues.")); - } - - return fails > 0 ? 1 : 0; + public GcpDoctorCommand() { + super(ProductProfile.GCP, List.of()); } } diff --git a/src/main/java/io/floci/cli/commands/gcp/GcpEnvCommand.java b/src/main/java/io/floci/cli/commands/gcp/GcpEnvCommand.java index d473960..eb18de6 100644 --- a/src/main/java/io/floci/cli/commands/gcp/GcpEnvCommand.java +++ b/src/main/java/io/floci/cli/commands/gcp/GcpEnvCommand.java @@ -1,6 +1,7 @@ package io.floci.cli.commands.gcp; -import io.floci.cli.GcpGlobalOptions; +import io.floci.cli.GlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.docker.DockerClient; import io.floci.cli.output.Ansi; import io.floci.cli.output.OutputFormat; @@ -26,7 +27,7 @@ public class GcpEnvCommand implements Callable { Set.of("gcs", "pubsub", "firestore", "datastore", "secretmanager", "iam"); @Mixin - GcpGlobalOptions global; + GlobalOptions global = new GlobalOptions(ProductProfile.GCP); @Option(names = {"--service"}, description = "Comma-separated services: gcs,pubsub,firestore,datastore,secretmanager,iam", diff --git a/src/main/java/io/floci/cli/commands/gcp/GcpLogsCommand.java b/src/main/java/io/floci/cli/commands/gcp/GcpLogsCommand.java index a62b1ad..bc485e5 100644 --- a/src/main/java/io/floci/cli/commands/gcp/GcpLogsCommand.java +++ b/src/main/java/io/floci/cli/commands/gcp/GcpLogsCommand.java @@ -1,54 +1,17 @@ package io.floci.cli.commands.gcp; -import io.floci.cli.GcpGlobalOptions; -import io.floci.cli.docker.DockerClient; -import io.floci.cli.docker.DockerException; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.LogsCommand; +import picocli.CommandLine.Command; @Command( name = "logs", description = "Fetch logs from the Floci GCP container", mixinStandardHelpOptions = true ) -public class GcpLogsCommand implements Callable { +public class GcpLogsCommand extends LogsCommand { - @Mixin - GcpGlobalOptions global; - - @Option(names = {"--follow", "-f"}, description = "Follow log output") - boolean follow; - - @Option(names = {"--tail"}, description = "Number of lines from the end (0 = all)", defaultValue = "100", paramLabel = "") - int tail; - - @Option(names = {"--since"}, description = "Show logs since duration (e.g. 5m, 1h) or timestamp", paramLabel = "") - String since; - - @Override - public Integer call() { - Printer printer = global.printer(); - DockerClient docker = new DockerClient(); - - try { - var info = docker.inspectContainer(global.container); - if (info.isEmpty()) { - printer.error("Container '" + global.container + "' not found.\nRun 'floci gcp start' to launch one."); - return 1; - } - } catch (DockerException e) { - printer.error("Failed to inspect container: " + e.getMessage()); - return 1; - } - - try { - docker.streamLogs(global.container, follow, tail, since); - return 0; - } catch (DockerException e) { - printer.error("Failed to fetch logs: " + e.getMessage()); - return 1; - } + public GcpLogsCommand() { + super(ProductProfile.GCP); } -} \ No newline at end of file +} diff --git a/src/main/java/io/floci/cli/commands/gcp/GcpRestartCommand.java b/src/main/java/io/floci/cli/commands/gcp/GcpRestartCommand.java index 092de64..79fbd42 100644 --- a/src/main/java/io/floci/cli/commands/gcp/GcpRestartCommand.java +++ b/src/main/java/io/floci/cli/commands/gcp/GcpRestartCommand.java @@ -1,40 +1,17 @@ package io.floci.cli.commands.gcp; -import io.floci.cli.GcpGlobalOptions; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.RestartCommand; +import picocli.CommandLine.Command; @Command( name = "restart", description = "Stop and restart the Floci GCP container", mixinStandardHelpOptions = true ) -public class GcpRestartCommand implements Callable { +public class GcpRestartCommand extends RestartCommand { - @Mixin - GcpGlobalOptions global; - - @Override - public Integer call() { - Printer printer = global.printer(); - - GcpStopCommand stop = new GcpStopCommand(); - stop.global = global; - stop.remove = false; - stop.timeout = 10; - int stopResult = stop.call(); - if (stopResult != 0) return stopResult; - - try { Thread.sleep(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } - - GcpStartCommand start = new GcpStartCommand(); - start.global = global; - start.port = 4588; - start.pull = "missing"; - start.image = "floci/floci-gcp:latest"; - start.detach = false; - return start.call(); + public GcpRestartCommand() { + super(ProductProfile.GCP); } -} \ No newline at end of file +} diff --git a/src/main/java/io/floci/cli/commands/gcp/GcpServicesCommand.java b/src/main/java/io/floci/cli/commands/gcp/GcpServicesCommand.java index ee4495b..af23dcd 100644 --- a/src/main/java/io/floci/cli/commands/gcp/GcpServicesCommand.java +++ b/src/main/java/io/floci/cli/commands/gcp/GcpServicesCommand.java @@ -1,55 +1,17 @@ package io.floci.cli.commands.gcp; -import io.floci.cli.GcpGlobalOptions; -import io.floci.cli.http.FlociHttpClient; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.OutputFormat; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.ServicesCommand; +import picocli.CommandLine.Command; @Command( name = "services", description = "List services available in the running Floci GCP instance", mixinStandardHelpOptions = true ) -public class GcpServicesCommand implements Callable { +public class GcpServicesCommand extends ServicesCommand { - @Mixin - GcpGlobalOptions global; - - @Override - public Integer call() { - Printer printer = global.printer(); - FlociHttpClient client = new FlociHttpClient(global.endpoint, GcpGlobalOptions.CONTROL_PREFIX); - - List services; - try { - var health = client.health(); - services = Arrays.asList(health.services()); - } catch (Exception e) { - printer.error("Could not reach Floci GCP at " + global.endpoint + ".\nIs Floci GCP running? Try 'floci gcp start'."); - return 1; - } - - if (printer.format() != OutputFormat.text) { - printer.structured(Map.of("services", services, "count", services.size())); - return 0; - } - - printer.println(Ansi.bold("Floci GCP Services") + " " + Ansi.gray("(" + services.size() + " enabled)")); - printer.println(""); - for (String svc : services) { - printer.println(" " + Ansi.green("✓") + " " + svc); - } - if (services.isEmpty()) { - printer.println(" " + Ansi.gray("(no services reported)")); - } - - return 0; + public GcpServicesCommand() { + super(ProductProfile.GCP); } } diff --git a/src/main/java/io/floci/cli/commands/gcp/GcpStartCommand.java b/src/main/java/io/floci/cli/commands/gcp/GcpStartCommand.java index ec2038d..c03cff4 100644 --- a/src/main/java/io/floci/cli/commands/gcp/GcpStartCommand.java +++ b/src/main/java/io/floci/cli/commands/gcp/GcpStartCommand.java @@ -1,113 +1,17 @@ package io.floci.cli.commands.gcp; -import io.floci.cli.GcpGlobalOptions; -import io.floci.cli.docker.DockerClient; -import io.floci.cli.docker.DockerException; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.StartCommand; +import picocli.CommandLine.Command; @Command( name = "start", description = "Start the Floci GCP emulator container", mixinStandardHelpOptions = true ) -public class GcpStartCommand implements Callable { +public class GcpStartCommand extends StartCommand { - @Mixin - GcpGlobalOptions global; - - @Option(names = {"--port"}, description = "Host port to bind (default: 4588)", defaultValue = "4588", paramLabel = "") - int port; - - @Option(names = {"--persist"}, description = "Host directory for persistent state", paramLabel = "") - String persistDir; - - @Option(names = {"--services"}, description = "Comma-separated list of services to enable", paramLabel = "") - String services; - - @Option(names = {"--detach"}, description = "Return immediately without waiting for readiness") - boolean detach; - - @Option(names = {"--image"}, description = "Image reference to use (default: floci/floci-gcp:latest)", defaultValue = "floci/floci-gcp:latest", paramLabel = "") - String image; - - @Option(names = {"--pull"}, description = "Image pull policy: always, missing, never", defaultValue = "missing", paramLabel = "always|missing|never") - String pull; - - @Override - public Integer call() { - Printer printer = global.printer(); - DockerClient docker = new DockerClient(); - - if (!DockerClient.isInstalled()) { - printer.error("docker binary not found in PATH.\nInstall Docker Desktop from https://docs.docker.com/get-docker/"); - return 1; - } - - try { - var existing = docker.inspectContainer(global.container); - if (existing.isPresent()) { - String state = existing.get().state(); - if ("running".equals(state)) { - printer.error("Container '" + global.container + "' is already running.\nRun 'floci gcp stop' first or pass --container to use a different name."); - return 1; - } - printer.println(Ansi.gray("Removing stopped container '" + global.container + "'...")); - docker.removeContainer(global.container); - } - } catch (DockerException e) { - printer.error("Failed to inspect container: " + e.getMessage()); - return 1; - } - - try { - printer.println(Ansi.gray("Checking image " + image + " (policy: " + pull + ")...")); - docker.pull(image, pull); - } catch (DockerException e) { - printer.error("Failed to pull image: " + e.getMessage() + "\nRun 'floci gcp start --pull never' to skip pulling."); - return 1; - } - - List args = new ArrayList<>(); - args.addAll(List.of("-d", "--name", global.container)); - args.addAll(List.of("-p", port + ":4588")); - args.addAll(DockerClient.dockerSocketRunArgs()); - if (persistDir != null) { - args.addAll(List.of("-v", persistDir + ":/app/data")); - // The server defaults to in-memory storage; enable persistent mode so - // state is actually written to the mounted directory and survives restarts. - args.addAll(List.of("-e", "FLOCI_GCP_STORAGE_MODE=persistent")); - } - if (services != null && !services.isBlank()) { - args.addAll(List.of("-e", "FLOCI_GCP_SERVICES=" + services)); - } - args.add(image); - - try { - printer.println("Starting " + Ansi.gold("Floci GCP") + " container..."); - String id = docker.startContainer(args); - printer.println(Ansi.green("Container started") + " (" + id.substring(0, Math.min(12, id.length())) + ")"); - } catch (DockerException e) { - printer.error("Failed to start container: " + e.getMessage()); - return 1; - } - - if (detach) { - printer.println(Ansi.gray("Detached. Run 'floci gcp wait' to poll for readiness.")); - return 0; - } - - global.endpoint = "http://localhost:" + port; - - printer.println(Ansi.gray("Waiting for Floci GCP to be ready...")); - GcpWaitCommand wait = new GcpWaitCommand(); - wait.global = global; - wait.timeout = "30s"; - return wait.call(); + public GcpStartCommand() { + super(ProductProfile.GCP); } -} \ No newline at end of file +} diff --git a/src/main/java/io/floci/cli/commands/gcp/GcpStatusCommand.java b/src/main/java/io/floci/cli/commands/gcp/GcpStatusCommand.java index ec380d7..b0106a8 100644 --- a/src/main/java/io/floci/cli/commands/gcp/GcpStatusCommand.java +++ b/src/main/java/io/floci/cli/commands/gcp/GcpStatusCommand.java @@ -1,98 +1,17 @@ package io.floci.cli.commands.gcp; -import io.floci.cli.GcpGlobalOptions; -import io.floci.cli.docker.DockerClient; -import io.floci.cli.docker.DockerException; -import io.floci.cli.http.FlociHttpClient; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.OutputFormat; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Optional; -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.StatusCommand; +import picocli.CommandLine.Command; @Command( name = "status", description = "Show Floci GCP container and server status", mixinStandardHelpOptions = true ) -public class GcpStatusCommand implements Callable { +public class GcpStatusCommand extends StatusCommand { - @Mixin - GcpGlobalOptions global; - - @Override - public Integer call() { - Printer printer = global.printer(); - DockerClient docker = new DockerClient(); - - String containerState = "not found"; - String containerImage = ""; - String containerPorts = ""; - String effectiveEndpoint = global.endpoint; - try { - Optional info = docker.inspectContainer(global.container); - if (info.isPresent()) { - containerState = info.get().state(); - containerImage = info.get().image(); - containerPorts = info.get().ports(); - effectiveEndpoint = global.endpointFromPorts(containerPorts, global.endpoint); - } - } catch (DockerException e) { - containerState = "error: " + e.getMessage(); - } - - String serverVersion = "unavailable"; - String serverEdition = ""; - boolean reachable = false; - FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, GcpGlobalOptions.CONTROL_PREFIX); - try { - var health = client.health(); - serverVersion = health.version(); - serverEdition = health.edition(); - reachable = true; - } catch (Exception ignored) {} - - Map data = new LinkedHashMap<>(); - data.put("container", global.container); - data.put("state", containerState); - data.put("image", containerImage); - data.put("ports", containerPorts); - data.put("endpoint", effectiveEndpoint); - data.put("reachable", reachable); - data.put("version", serverVersion); - data.put("edition", serverEdition); - - if (printer.format() != OutputFormat.text) { - printer.structured(data); - return 0; - } - - printer.println(Ansi.bold("Floci GCP Status")); - printer.println(""); - String stateColor = switch (containerState) { - case "running" -> Ansi.green(containerState); - case "not found" -> Ansi.gray(containerState); - default -> Ansi.red(containerState); - }; - printer.println(" Container: " + global.container + " " + stateColor); - if (!containerImage.isBlank()) printer.println(" Image: " + containerImage); - if (!containerPorts.isBlank()) printer.println(" Ports: " + containerPorts); - printer.println(" Endpoint: " + effectiveEndpoint); - printer.println(" Reachable: " + (reachable ? Ansi.green("yes") : Ansi.red("no"))); - if (reachable) { - printer.println(" Version: " + serverVersion); - if (!serverEdition.isBlank()) printer.println(" Edition: " + serverEdition); - } - - if (!reachable && !"running".equals(containerState)) { - printer.println(""); - printer.println(Ansi.gray("Run 'floci gcp start' to launch Floci GCP.")); - } - - return 0; + public GcpStatusCommand() { + super(ProductProfile.GCP); } -} \ No newline at end of file +} diff --git a/src/main/java/io/floci/cli/commands/gcp/GcpStopCommand.java b/src/main/java/io/floci/cli/commands/gcp/GcpStopCommand.java index e7ad184..f0b3f76 100644 --- a/src/main/java/io/floci/cli/commands/gcp/GcpStopCommand.java +++ b/src/main/java/io/floci/cli/commands/gcp/GcpStopCommand.java @@ -1,73 +1,17 @@ package io.floci.cli.commands.gcp; -import io.floci.cli.GcpGlobalOptions; -import io.floci.cli.docker.DockerClient; -import io.floci.cli.docker.DockerException; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.StopCommand; +import picocli.CommandLine.Command; @Command( name = "stop", description = "Stop the Floci GCP container", mixinStandardHelpOptions = true ) -public class GcpStopCommand implements Callable { +public class GcpStopCommand extends StopCommand { - @Mixin - GcpGlobalOptions global; - - @Option(names = {"--remove", "-r"}, description = "Remove the container after stopping") - boolean remove; - - @Option(names = {"--timeout"}, description = "Seconds to wait before forcefully killing (default: 10)", defaultValue = "10", paramLabel = "") - int timeout; - - @Override - public Integer call() { - Printer printer = global.printer(); - DockerClient docker = new DockerClient(); - - try { - var info = docker.inspectContainer(global.container); - if (info.isEmpty()) { - printer.error("Container '" + global.container + "' not found.\nRun 'floci gcp start' to launch one."); - return 1; - } - if (!"running".equals(info.get().state())) { - printer.println(Ansi.yellow("Container '" + global.container + "' is not running (state: " + info.get().state() + ")")); - if (remove) { - docker.removeContainer(global.container); - printer.println("Container removed."); - } - return 0; - } - } catch (DockerException e) { - printer.error("Failed to inspect container: " + e.getMessage()); - return 1; - } - - try { - printer.println("Stopping " + Ansi.gold("Floci GCP") + " container '" + global.container + "'..."); - docker.stopContainer(global.container, timeout); - printer.println(Ansi.green("Stopped.")); - } catch (DockerException e) { - printer.error("Failed to stop container: " + e.getMessage()); - return 1; - } - - if (remove) { - try { - docker.removeContainer(global.container); - printer.println("Container removed."); - } catch (DockerException e) { - printer.error("Failed to remove container: " + e.getMessage()); - return 1; - } - } - - return 0; + public GcpStopCommand() { + super(ProductProfile.GCP); } -} \ No newline at end of file +} diff --git a/src/main/java/io/floci/cli/commands/gcp/GcpVersionCommand.java b/src/main/java/io/floci/cli/commands/gcp/GcpVersionCommand.java index c97fb80..1ca815c 100644 --- a/src/main/java/io/floci/cli/commands/gcp/GcpVersionCommand.java +++ b/src/main/java/io/floci/cli/commands/gcp/GcpVersionCommand.java @@ -1,72 +1,17 @@ package io.floci.cli.commands.gcp; -import io.floci.cli.GcpGlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.commands.VersionCommand; -import io.floci.cli.docker.DockerClient; -import io.floci.cli.http.FlociHttpClient; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.OutputFormat; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Optional; -import java.util.concurrent.Callable; +import picocli.CommandLine.Command; @Command( name = "version", description = "Show CLI version, connected Floci GCP server version, and image digest", mixinStandardHelpOptions = true ) -public class GcpVersionCommand implements Callable { +public class GcpVersionCommand extends VersionCommand { - @Mixin - GcpGlobalOptions global; - - @Override - public Integer call() { - Printer printer = global.printer(); - - String serverVersion = null; - String serverEdition = null; - String imageDigest = null; - - DockerClient docker = new DockerClient(); - String effectiveEndpoint = global.resolvedEndpoint(docker); - - FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, GcpGlobalOptions.CONTROL_PREFIX); - try { - var info = client.info(); - serverVersion = info.version(); - serverEdition = info.edition(); - } catch (Exception ignored) {} - - try { - imageDigest = docker.imageDigest("floci/floci-gcp").orElse(null); - } catch (Exception ignored) {} - - Map data = new LinkedHashMap<>(); - data.put("cli", VersionCommand.CLI_VERSION); - data.put("server", Optional.ofNullable(serverVersion).orElse("unavailable")); - data.put("edition", Optional.ofNullable(serverEdition).orElse("")); - if (imageDigest != null) data.put("digest", imageDigest); - - if (printer.format() != OutputFormat.text) { - printer.structured(data); - return 0; - } - - printer.println(Ansi.bold("Floci CLI") + " " + Ansi.gold(VersionCommand.CLI_VERSION)); - if (serverVersion != null) { - printer.println("Server: " + serverVersion + (serverEdition != null ? " (" + serverEdition + ")" : "")); - } else { - printer.println("Server: " + Ansi.gray("not reachable at " + effectiveEndpoint)); - } - if (imageDigest != null) { - printer.println("Image: " + imageDigest); - } - - return 0; + public GcpVersionCommand() { + super(ProductProfile.GCP); } } diff --git a/src/main/java/io/floci/cli/commands/gcp/GcpWaitCommand.java b/src/main/java/io/floci/cli/commands/gcp/GcpWaitCommand.java index 3261ffa..c43ec36 100644 --- a/src/main/java/io/floci/cli/commands/gcp/GcpWaitCommand.java +++ b/src/main/java/io/floci/cli/commands/gcp/GcpWaitCommand.java @@ -1,73 +1,17 @@ package io.floci.cli.commands.gcp; -import io.floci.cli.GcpGlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.commands.WaitCommand; -import io.floci.cli.http.FlociHttpClient; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.OutputFormat; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.time.Duration; -import java.time.Instant; -import java.util.Map; -import java.util.concurrent.Callable; +import picocli.CommandLine.Command; @Command( name = "wait", description = "Wait until Floci GCP is ready to accept requests", mixinStandardHelpOptions = true ) -public class GcpWaitCommand implements Callable { +public class GcpWaitCommand extends WaitCommand { - @Mixin - GcpGlobalOptions global; - - @Option(names = {"--timeout"}, description = "Maximum time to wait (e.g. 30s, 2m)", defaultValue = "30s", paramLabel = "") - String timeout; - - @Option(names = {"--service"}, description = "Wait until a specific service is enabled", paramLabel = "") - String service; - - @Override - public Integer call() { - Printer printer = global.printer(); - long timeoutMillis = WaitCommand.parseDuration(timeout); - String effectiveEndpoint = global.resolvedEndpoint(new io.floci.cli.docker.DockerClient()); - FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, GcpGlobalOptions.CONTROL_PREFIX); - Instant deadline = Instant.now().plusMillis(timeoutMillis); - - while (Instant.now().isBefore(deadline)) { - if (isReady(client, service)) { - if (printer.format() != OutputFormat.text) { - printer.structured(Map.of("ready", true, "endpoint", effectiveEndpoint)); - } else { - printer.println(Ansi.green("Floci GCP is ready") + " (" + effectiveEndpoint + ")"); - } - return 0; - } - long remaining = Duration.between(Instant.now(), deadline).toSeconds(); - printer.print("\r" + Ansi.gray("Waiting... (" + remaining + "s remaining)") + " "); - try { Thread.sleep(500); } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; - } - } - - printer.error("Timed out waiting for Floci GCP after " + timeout + ".\nIs the container running? Try 'floci gcp status'."); - return 1; + public GcpWaitCommand() { + super(ProductProfile.GCP); } - - private boolean isReady(FlociHttpClient client, String requiredService) { - try { - var health = client.health(); - if (requiredService == null) return true; - for (String s : health.services()) { - if (s.equalsIgnoreCase(requiredService)) return true; - } - return false; - } catch (Exception e) { - return false; - } - } -} \ No newline at end of file +} diff --git a/src/main/java/io/floci/cli/commands/gcp/config/GcpConfigProfileCommand.java b/src/main/java/io/floci/cli/commands/gcp/config/GcpConfigProfileCommand.java index 1f2877c..be4fb19 100644 --- a/src/main/java/io/floci/cli/commands/gcp/config/GcpConfigProfileCommand.java +++ b/src/main/java/io/floci/cli/commands/gcp/config/GcpConfigProfileCommand.java @@ -1,115 +1,17 @@ package io.floci.cli.commands.gcp.config; -import io.floci.cli.GcpGlobalOptions; -import io.floci.cli.config.Profile; -import io.floci.cli.config.ProfileStore; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.io.IOException; -import java.util.List; -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.config.ConfigProfileCommand; +import picocli.CommandLine.Command; @Command( name = "profile", description = "Manage Floci GCP configuration profiles", - mixinStandardHelpOptions = true, - subcommands = {HelpCommand.class} + mixinStandardHelpOptions = true ) -public class GcpConfigProfileCommand implements Callable { +public class GcpConfigProfileCommand extends ConfigProfileCommand { - @Mixin - GcpGlobalOptions global; - - @Parameters(index = "0", description = "Action: list, show, create, delete", paramLabel = "list|show|create|delete") - String action; - - @Parameters(index = "1", description = "Profile name (required for show, create, delete)", arity = "0..1", paramLabel = "") - String name; - - @Override - public Integer call() { - Printer printer = global.printer(); - ProfileStore store = new ProfileStore(); - - return switch (action.toLowerCase()) { - case "list" -> listProfiles(printer, store); - case "show" -> showProfile(printer, store); - case "create" -> createProfile(printer, store); - case "delete" -> deleteProfile(printer, store); - default -> { - printer.error("Unknown action '" + action + "'. Use: list, show, create, delete"); - yield 1; - } - }; - } - - private int listProfiles(Printer printer, ProfileStore store) { - try { - List profiles = store.list(); - if (profiles.isEmpty()) { - printer.println(Ansi.gray("No profiles found. Create one with: floci gcp config profile create ")); - return 0; - } - printer.println(Ansi.bold("Profiles:")); - for (Profile p : profiles) { - printer.println(" " + p.name + Ansi.gray(" " + (p.endpoint != null ? p.endpoint : ""))); - } - return 0; - } catch (IOException e) { - printer.error("Could not list profiles: " + e.getMessage()); - return 1; - } - } - - private int showProfile(Printer printer, ProfileStore store) { - if (name == null) { printer.error("Profile name required."); return 1; } - try { - var profile = store.get(name); - if (profile.isEmpty()) { printer.error("Profile '" + name + "' not found."); return 1; } - Profile p = profile.get(); - printer.println(Ansi.bold("Profile: ") + p.name); - if (p.endpoint != null) printer.println(" endpoint: " + p.endpoint); - if (p.container != null) printer.println(" container: " + p.container); - if (p.image != null) printer.println(" image: " + p.image); - if (p.port != null) printer.println(" port: " + p.port); - if (p.persistDir != null) printer.println(" persistDir: " + p.persistDir); - if (p.services != null) printer.println(" services: " + p.services); - return 0; - } catch (IOException e) { - printer.error("Could not read profile: " + e.getMessage()); - return 1; - } - } - - private int createProfile(Printer printer, ProfileStore store) { - if (name == null) { printer.error("Profile name required."); return 1; } - try { - if (store.get(name).isPresent()) { - printer.error("Profile '" + name + "' already exists. Delete it first or edit " + store.profileFile(name)); - return 1; - } - Profile p = new Profile(name); - store.save(p); - printer.println(Ansi.green("Created") + " profile '" + name + "' at " + store.profileFile(name)); - printer.println(Ansi.gray("Edit the file to customize endpoint, container, image, etc.")); - return 0; - } catch (IOException e) { - printer.error("Could not create profile: " + e.getMessage()); - return 1; - } - } - - private int deleteProfile(Printer printer, ProfileStore store) { - if (name == null) { printer.error("Profile name required."); return 1; } - try { - if (!store.delete(name)) { printer.error("Profile '" + name + "' not found."); return 1; } - printer.println(Ansi.green("Deleted") + " profile '" + name + "'"); - return 0; - } catch (IOException e) { - printer.error("Could not delete profile: " + e.getMessage()); - return 1; - } + public GcpConfigProfileCommand() { + super(ProductProfile.GCP); } } diff --git a/src/main/java/io/floci/cli/commands/gcp/config/GcpConfigShowCommand.java b/src/main/java/io/floci/cli/commands/gcp/config/GcpConfigShowCommand.java index 715bd7e..0415650 100644 --- a/src/main/java/io/floci/cli/commands/gcp/config/GcpConfigShowCommand.java +++ b/src/main/java/io/floci/cli/commands/gcp/config/GcpConfigShowCommand.java @@ -1,70 +1,17 @@ package io.floci.cli.commands.gcp.config; -import io.floci.cli.GcpGlobalOptions; -import io.floci.cli.config.Profile; -import io.floci.cli.config.ProfileStore; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.OutputFormat; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Optional; -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.config.ConfigShowCommand; +import picocli.CommandLine.Command; @Command( name = "show", description = "Show the active Floci GCP configuration", mixinStandardHelpOptions = true ) -public class GcpConfigShowCommand implements Callable { +public class GcpConfigShowCommand extends ConfigShowCommand { - @Mixin - GcpGlobalOptions global; - - @Override - public Integer call() { - Printer printer = global.printer(); - ProfileStore store = new ProfileStore(); - - Profile active = new Profile(global.profile != null ? global.profile : "default"); - active.endpoint = global.endpoint; - active.container = global.container; - active.output = global.output != null ? global.output.name() : "text"; - - if (global.profile != null) { - try { - Optional loaded = store.get(global.profile); - if (loaded.isPresent()) { - active = loaded.get(); - } else { - printer.warn("Profile '" + global.profile + "' not found. Using defaults."); - } - } catch (IOException e) { - printer.warn("Could not load profile: " + e.getMessage()); - } - } - - Map data = new LinkedHashMap<>(); - data.put("profile", active.name); - data.put("endpoint", active.endpoint != null ? active.endpoint : global.endpoint); - data.put("container", active.container != null ? active.container : global.container); - data.put("output", active.output); - - if (printer.format() != OutputFormat.text) { - printer.structured(data); - return 0; - } - - printer.println(Ansi.bold("Active Configuration (Floci GCP)")); - printer.println(""); - printer.println(" Profile: " + active.name); - printer.println(" Endpoint: " + (active.endpoint != null ? active.endpoint : global.endpoint)); - printer.println(" Container: " + (active.container != null ? active.container : global.container)); - printer.println(" Output: " + (active.output != null ? active.output : "text")); - - return 0; + public GcpConfigShowCommand() { + super(ProductProfile.GCP); } } diff --git a/src/main/java/io/floci/cli/commands/gcp/config/GcpConfigValidateCommand.java b/src/main/java/io/floci/cli/commands/gcp/config/GcpConfigValidateCommand.java index 2abd17b..dc5bd6c 100644 --- a/src/main/java/io/floci/cli/commands/gcp/config/GcpConfigValidateCommand.java +++ b/src/main/java/io/floci/cli/commands/gcp/config/GcpConfigValidateCommand.java @@ -1,91 +1,17 @@ package io.floci.cli.commands.gcp.config; -import io.floci.cli.GcpGlobalOptions; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.Callable; -import java.util.regex.Pattern; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.config.ConfigValidateCommand; +import picocli.CommandLine.Command; @Command( name = "validate", description = "Validate a docker-compose.yml file for Floci GCP compatibility", mixinStandardHelpOptions = true ) -public class GcpConfigValidateCommand implements Callable { +public class GcpConfigValidateCommand extends ConfigValidateCommand { - @Mixin - GcpGlobalOptions global; - - @Option(names = {"--file", "-f"}, description = "Path to docker-compose file", paramLabel = "") - String file; - - @Override - public Integer call() { - Printer printer = global.printer(); - - Path composeFile = resolveComposeFile(); - if (composeFile == null) { - printer.error("No docker-compose file found. Pass --file or run in a directory with docker-compose.yml."); - return 1; - } - - String content; - try { - content = Files.readString(composeFile); - } catch (IOException e) { - printer.error("Could not read " + composeFile + ": " + e.getMessage()); - return 1; - } - - printer.println(Ansi.bold("Validating") + " " + composeFile); - printer.println(""); - - List issues = new ArrayList<>(); - List warnings = new ArrayList<>(); - - if (!content.contains("/var/run/docker.sock")) { - warnings.add("Missing /var/run/docker.sock volume mount — required for Kafka/Redpanda support"); - } - - if (!Pattern.compile("4588\\s*:\\s*4588").matcher(content).find()) { - issues.add("Port mapping 4588:4588 not found — Floci GCP listens on 4588 by default"); - } - - if (!content.contains("floci/floci-gcp") && !content.contains("ghcr.io/floci-io/floci-gcp")) { - warnings.add("Floci GCP image reference not detected — ensure your service uses floci/floci-gcp"); - } - - for (String issue : issues) { - printer.println(" " + Ansi.red("✗") + " " + issue); - } - for (String warn : warnings) { - printer.println(" " + Ansi.yellow("⚠") + " " + warn); - } - if (issues.isEmpty() && warnings.isEmpty()) { - printer.println(" " + Ansi.green("✓") + " Compose file looks good"); - } - - printer.println(""); - if (!issues.isEmpty()) { - printer.println(issues.size() + " error(s) found."); - return 1; - } - return 0; - } - - private Path resolveComposeFile() { - if (file != null) return Path.of(file); - for (String name : List.of("docker-compose.yml", "docker-compose.yaml", "compose.yml", "compose.yaml")) { - Path p = Path.of(name); - if (Files.exists(p)) return p; - } - return null; + public GcpConfigValidateCommand() { + super(ProductProfile.GCP, false, "Kafka/Redpanda support"); } } diff --git a/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotCommand.java b/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotCommand.java index b7d2fb2..c5bf242 100644 --- a/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotCommand.java +++ b/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotCommand.java @@ -5,7 +5,7 @@ import picocli.CommandLine.*; @Command( name = "snapshot", - description = "Manage Floci GCP state snapshots (coming soon)", + description = "Manage Floci GCP state snapshots", mixinStandardHelpOptions = true, subcommands = { GcpSnapshotSaveCommand.class, diff --git a/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotDeleteCommand.java b/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotDeleteCommand.java index a0df377..a421e5c 100644 --- a/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotDeleteCommand.java +++ b/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotDeleteCommand.java @@ -1,28 +1,17 @@ package io.floci.cli.commands.gcp.snapshot; -import io.floci.cli.GcpGlobalOptions; -import io.floci.cli.output.Ansi; -import picocli.CommandLine.*; - -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.snapshot.SnapshotDeleteCommand; +import picocli.CommandLine.Command; @Command( name = "delete", description = "Delete a Floci GCP snapshot", mixinStandardHelpOptions = true ) -public class GcpSnapshotDeleteCommand implements Callable { +public class GcpSnapshotDeleteCommand extends SnapshotDeleteCommand { - @Mixin - GcpGlobalOptions global; - - @Parameters(index = "0", description = "Snapshot name", paramLabel = "") - String name; - - @Override - public Integer call() { - global.printer().println(Ansi.yellow("Snapshots are not yet available for Floci GCP.") + - "\nTrack progress: https://github.com/floci-io/floci-gcp/issues"); - return 0; + public GcpSnapshotDeleteCommand() { + super(ProductProfile.GCP); } } diff --git a/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotExportCommand.java b/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotExportCommand.java index cc7df03..5ae506a 100644 --- a/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotExportCommand.java +++ b/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotExportCommand.java @@ -1,31 +1,17 @@ package io.floci.cli.commands.gcp.snapshot; -import io.floci.cli.GcpGlobalOptions; -import io.floci.cli.output.Ansi; -import picocli.CommandLine.*; - -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.snapshot.SnapshotExportCommand; +import picocli.CommandLine.Command; @Command( name = "export", description = "Export a Floci GCP snapshot to a tarball file", mixinStandardHelpOptions = true ) -public class GcpSnapshotExportCommand implements Callable { +public class GcpSnapshotExportCommand extends SnapshotExportCommand { - @Mixin - GcpGlobalOptions global; - - @Parameters(index = "0", description = "Snapshot name", paramLabel = "") - String name; - - @Option(names = {"--file", "-f"}, description = "Destination file path", required = true, paramLabel = "") - String outputFile; - - @Override - public Integer call() { - global.printer().println(Ansi.yellow("Snapshots are not yet available for Floci GCP.") + - "\nTrack progress: https://github.com/floci-io/floci-gcp/issues"); - return 0; + public GcpSnapshotExportCommand() { + super(ProductProfile.GCP); } } diff --git a/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotImportCommand.java b/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotImportCommand.java index 58e9aea..d6f8602 100644 --- a/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotImportCommand.java +++ b/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotImportCommand.java @@ -1,31 +1,17 @@ package io.floci.cli.commands.gcp.snapshot; -import io.floci.cli.GcpGlobalOptions; -import io.floci.cli.output.Ansi; -import picocli.CommandLine.*; - -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.snapshot.SnapshotImportCommand; +import picocli.CommandLine.Command; @Command( name = "import", description = "Import a Floci GCP snapshot from a tarball file", mixinStandardHelpOptions = true ) -public class GcpSnapshotImportCommand implements Callable { +public class GcpSnapshotImportCommand extends SnapshotImportCommand { - @Mixin - GcpGlobalOptions global; - - @Parameters(index = "0", description = "Snapshot name", paramLabel = "") - String name; - - @Option(names = {"--file", "-f"}, description = "Source file path", required = true, paramLabel = "") - String inputFile; - - @Override - public Integer call() { - global.printer().println(Ansi.yellow("Snapshots are not yet available for Floci GCP.") + - "\nTrack progress: https://github.com/floci-io/floci-gcp/issues"); - return 0; + public GcpSnapshotImportCommand() { + super(ProductProfile.GCP); } } diff --git a/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotListCommand.java b/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotListCommand.java index a30b667..e8ea0b4 100644 --- a/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotListCommand.java +++ b/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotListCommand.java @@ -1,34 +1,17 @@ package io.floci.cli.commands.gcp.snapshot; -import io.floci.cli.GcpGlobalOptions; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.OutputFormat; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.List; -import java.util.Map; -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.snapshot.SnapshotListCommand; +import picocli.CommandLine.Command; @Command( name = "list", description = "List available Floci GCP snapshots", mixinStandardHelpOptions = true ) -public class GcpSnapshotListCommand implements Callable { +public class GcpSnapshotListCommand extends SnapshotListCommand { - @Mixin - GcpGlobalOptions global; - - @Override - public Integer call() { - Printer printer = global.printer(); - if (printer.format() != OutputFormat.text) { - printer.structured(Map.of("snapshots", List.of(), "note", "not yet available")); - return 0; - } - printer.println(Ansi.gray("No snapshots found.") + - " (Snapshot support for Floci GCP is coming — https://github.com/floci-io/floci-gcp/issues)"); - return 0; + public GcpSnapshotListCommand() { + super(ProductProfile.GCP); } } diff --git a/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotLoadCommand.java b/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotLoadCommand.java index 7be227f..e8b4969 100644 --- a/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotLoadCommand.java +++ b/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotLoadCommand.java @@ -1,28 +1,17 @@ package io.floci.cli.commands.gcp.snapshot; -import io.floci.cli.GcpGlobalOptions; -import io.floci.cli.output.Ansi; -import picocli.CommandLine.*; - -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.snapshot.SnapshotLoadCommand; +import picocli.CommandLine.Command; @Command( name = "load", description = "Load a Floci GCP snapshot", mixinStandardHelpOptions = true ) -public class GcpSnapshotLoadCommand implements Callable { +public class GcpSnapshotLoadCommand extends SnapshotLoadCommand { - @Mixin - GcpGlobalOptions global; - - @Parameters(index = "0", description = "Snapshot name", paramLabel = "") - String name; - - @Override - public Integer call() { - global.printer().println(Ansi.yellow("Snapshots are not yet available for Floci GCP.") + - "\nTrack progress: https://github.com/floci-io/floci-gcp/issues"); - return 0; + public GcpSnapshotLoadCommand() { + super(ProductProfile.GCP); } } diff --git a/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotSaveCommand.java b/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotSaveCommand.java index b2aa506..8962e3b 100644 --- a/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotSaveCommand.java +++ b/src/main/java/io/floci/cli/commands/gcp/snapshot/GcpSnapshotSaveCommand.java @@ -1,31 +1,17 @@ package io.floci.cli.commands.gcp.snapshot; -import io.floci.cli.GcpGlobalOptions; -import io.floci.cli.output.Ansi; -import picocli.CommandLine.*; - -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.snapshot.SnapshotSaveCommand; +import picocli.CommandLine.Command; @Command( name = "save", description = "Save the current Floci GCP state as a named snapshot", mixinStandardHelpOptions = true ) -public class GcpSnapshotSaveCommand implements Callable { +public class GcpSnapshotSaveCommand extends SnapshotSaveCommand { - @Mixin - GcpGlobalOptions global; - - @Parameters(index = "0", description = "Snapshot name", paramLabel = "") - String name; - - @Option(names = {"--message", "-m"}, description = "Optional description", paramLabel = "") - String message; - - @Override - public Integer call() { - global.printer().println(Ansi.yellow("Snapshots are not yet available for Floci GCP.") + - "\nTrack progress: https://github.com/floci-io/floci-gcp/issues"); - return 0; + public GcpSnapshotSaveCommand() { + super(ProductProfile.GCP); } } diff --git a/src/main/java/io/floci/cli/commands/oci/OciDoctorCommand.java b/src/main/java/io/floci/cli/commands/oci/OciDoctorCommand.java index d77d987..f95e646 100644 --- a/src/main/java/io/floci/cli/commands/oci/OciDoctorCommand.java +++ b/src/main/java/io/floci/cli/commands/oci/OciDoctorCommand.java @@ -1,108 +1,19 @@ package io.floci.cli.commands.oci; -import io.floci.cli.OciGlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.commands.DoctorCommand; -import io.floci.cli.doctor.Check; -import io.floci.cli.doctor.CheckResult; -import io.floci.cli.doctor.CheckStatus; -import io.floci.cli.docker.DockerClient; -import io.floci.cli.doctor.checks.ContainerRunningCheck; -import io.floci.cli.doctor.checks.DockerDaemonCheck; -import io.floci.cli.doctor.checks.DockerInstalledCheck; -import io.floci.cli.doctor.checks.DockerSocketCheck; -import io.floci.cli.doctor.checks.DockerVersionCheck; -import io.floci.cli.doctor.checks.EndpointReachableCheck; -import io.floci.cli.doctor.checks.ImagePresentCheck; -import io.floci.cli.doctor.checks.ImageVersionCheck; -import io.floci.cli.doctor.checks.PortAvailableCheck; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.OutputFormat; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; +import picocli.CommandLine.Command; -import java.util.ArrayList; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; -import java.util.concurrent.Callable; @Command( name = "doctor", description = "Run environment diagnostics for Floci OCI", mixinStandardHelpOptions = true ) -public class OciDoctorCommand implements Callable { +public class OciDoctorCommand extends DoctorCommand { - @Mixin - OciGlobalOptions global; - - @Option(names = {"--check"}, description = "Run only a specific check by name", paramLabel = "") - String checkName; - - @Option(names = {"--fix"}, description = "Attempt to auto-fix fixable issues") - boolean fix; - - private static final List ALL_CHECKS; - - static { - List checks = new ArrayList<>(); - checks.add(new DockerInstalledCheck()); - checks.add(new DockerDaemonCheck()); - checks.add(new DockerSocketCheck()); - checks.add(new DockerVersionCheck()); - checks.add(new PortAvailableCheck()); - checks.add(new ImagePresentCheck("floci/floci-oci")); - checks.add(new ImageVersionCheck("floci/floci-oci")); - checks.add(new ContainerRunningCheck()); - checks.add(new EndpointReachableCheck(io.floci.cli.OciGlobalOptions.CONTROL_PREFIX)); - ALL_CHECKS = List.copyOf(checks); - } - - @Override - public Integer call() { - Printer printer = global.printer(); - List results = new ArrayList<>(); - String effectiveEndpoint = global.resolvedEndpoint(new DockerClient()); - - boolean textMode = printer.format() == OutputFormat.text; - - if (textMode) { - printer.println(Ansi.bold("Floci OCI Doctor") + " — checking your environment"); - printer.println(""); - } - - for (Check check : ALL_CHECKS) { - CheckResult result = check.run(effectiveEndpoint, global.container); - if (checkName != null && !result.name().equals(checkName)) continue; - results.add(result); - if (textMode) DoctorCommand.printResult(printer, result); - } - - if (!textMode) { - List> structured = new ArrayList<>(); - for (CheckResult r : results) { - Map m = new LinkedHashMap<>(); - m.put("name", r.name()); - m.put("status", r.status().name()); - m.put("message", r.message()); - if (r.fix() != null) m.put("fix", r.fix()); - structured.add(m); - } - printer.structured(structured); - return 0; - } - - long fails = results.stream().filter(r -> r.status() == CheckStatus.fail).count(); - long warns = results.stream().filter(r -> r.status() == CheckStatus.warn).count(); - - printer.println(""); - if (fails == 0 && warns == 0) { - printer.println(Ansi.green("All checks passed.")); - } else { - printer.println(fails + " issue(s) found (" + fails + " fail, " + warns + " warn)." - + (fix ? "" : " Run with --fix to auto-resolve fixable issues.")); - } - - return fails > 0 ? 1 : 0; + public OciDoctorCommand() { + super(ProductProfile.OCI, List.of()); } } diff --git a/src/main/java/io/floci/cli/commands/oci/OciEnvCommand.java b/src/main/java/io/floci/cli/commands/oci/OciEnvCommand.java index 98c5365..19032cf 100644 --- a/src/main/java/io/floci/cli/commands/oci/OciEnvCommand.java +++ b/src/main/java/io/floci/cli/commands/oci/OciEnvCommand.java @@ -1,6 +1,7 @@ package io.floci.cli.commands.oci; -import io.floci.cli.OciGlobalOptions; +import io.floci.cli.GlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.docker.DockerClient; import io.floci.cli.output.Ansi; import io.floci.cli.output.OutputFormat; @@ -20,7 +21,7 @@ import java.util.concurrent.Callable; public class OciEnvCommand implements Callable { @Mixin - OciGlobalOptions global; + GlobalOptions global = new GlobalOptions(ProductProfile.OCI); @Option(names = {"--shell"}, description = "Shell format: bash, fish, powershell (default: bash)", diff --git a/src/main/java/io/floci/cli/commands/oci/OciLogsCommand.java b/src/main/java/io/floci/cli/commands/oci/OciLogsCommand.java index 1de7376..445e0fe 100644 --- a/src/main/java/io/floci/cli/commands/oci/OciLogsCommand.java +++ b/src/main/java/io/floci/cli/commands/oci/OciLogsCommand.java @@ -1,54 +1,17 @@ package io.floci.cli.commands.oci; -import io.floci.cli.OciGlobalOptions; -import io.floci.cli.docker.DockerClient; -import io.floci.cli.docker.DockerException; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.LogsCommand; +import picocli.CommandLine.Command; @Command( name = "logs", description = "Fetch logs from the Floci OCI container", mixinStandardHelpOptions = true ) -public class OciLogsCommand implements Callable { +public class OciLogsCommand extends LogsCommand { - @Mixin - OciGlobalOptions global; - - @Option(names = {"--follow", "-f"}, description = "Follow log output") - boolean follow; - - @Option(names = {"--tail"}, description = "Number of lines from the end (0 = all)", defaultValue = "100", paramLabel = "") - int tail; - - @Option(names = {"--since"}, description = "Show logs since duration (e.g. 5m, 1h) or timestamp", paramLabel = "") - String since; - - @Override - public Integer call() { - Printer printer = global.printer(); - DockerClient docker = new DockerClient(); - - try { - var info = docker.inspectContainer(global.container); - if (info.isEmpty()) { - printer.error("Container '" + global.container + "' not found.\nRun 'floci oci start' to launch one."); - return 1; - } - } catch (DockerException e) { - printer.error("Failed to inspect container: " + e.getMessage()); - return 1; - } - - try { - docker.streamLogs(global.container, follow, tail, since); - return 0; - } catch (DockerException e) { - printer.error("Failed to fetch logs: " + e.getMessage()); - return 1; - } + public OciLogsCommand() { + super(ProductProfile.OCI); } -} \ No newline at end of file +} diff --git a/src/main/java/io/floci/cli/commands/oci/OciRestartCommand.java b/src/main/java/io/floci/cli/commands/oci/OciRestartCommand.java index 44140d0..b54c9c2 100644 --- a/src/main/java/io/floci/cli/commands/oci/OciRestartCommand.java +++ b/src/main/java/io/floci/cli/commands/oci/OciRestartCommand.java @@ -1,40 +1,17 @@ package io.floci.cli.commands.oci; -import io.floci.cli.OciGlobalOptions; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.RestartCommand; +import picocli.CommandLine.Command; @Command( name = "restart", description = "Stop and restart the Floci OCI container", mixinStandardHelpOptions = true ) -public class OciRestartCommand implements Callable { +public class OciRestartCommand extends RestartCommand { - @Mixin - OciGlobalOptions global; - - @Override - public Integer call() { - Printer printer = global.printer(); - - OciStopCommand stop = new OciStopCommand(); - stop.global = global; - stop.remove = false; - stop.timeout = 10; - int stopResult = stop.call(); - if (stopResult != 0) return stopResult; - - try { Thread.sleep(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } - - OciStartCommand start = new OciStartCommand(); - start.global = global; - start.port = 4599; - start.pull = "missing"; - start.image = "floci/floci-oci:latest"; - start.detach = false; - return start.call(); + public OciRestartCommand() { + super(ProductProfile.OCI); } -} \ No newline at end of file +} diff --git a/src/main/java/io/floci/cli/commands/oci/OciServicesCommand.java b/src/main/java/io/floci/cli/commands/oci/OciServicesCommand.java index ab3d01f..3fe32c2 100644 --- a/src/main/java/io/floci/cli/commands/oci/OciServicesCommand.java +++ b/src/main/java/io/floci/cli/commands/oci/OciServicesCommand.java @@ -1,59 +1,17 @@ package io.floci.cli.commands.oci; -import io.floci.cli.OciGlobalOptions; -import io.floci.cli.docker.DockerClient; -import io.floci.cli.http.FlociHttpClient; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.OutputFormat; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.ServicesCommand; +import picocli.CommandLine.Command; @Command( name = "services", description = "List services available in the running Floci OCI instance", mixinStandardHelpOptions = true ) -public class OciServicesCommand implements Callable { +public class OciServicesCommand extends ServicesCommand { - @Mixin - OciGlobalOptions global; - - @Override - public Integer call() { - Printer printer = global.printer(); - // Resolve the endpoint from the container's port mapping, like status/wait/env do, - // so a container started with --port is still found without --endpoint. - String effectiveEndpoint = global.resolvedEndpoint(new DockerClient()); - FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, OciGlobalOptions.CONTROL_PREFIX); - - List services; - try { - var health = client.health(); - services = Arrays.asList(health.services()); - } catch (Exception e) { - printer.error("Could not reach Floci OCI at " + effectiveEndpoint + ".\nIs Floci OCI running? Try 'floci oci start'."); - return 1; - } - - if (printer.format() != OutputFormat.text) { - printer.structured(Map.of("services", services, "count", services.size())); - return 0; - } - - printer.println(Ansi.bold("Floci OCI Services") + " " + Ansi.gray("(" + services.size() + " enabled)")); - printer.println(""); - for (String svc : services) { - printer.println(" " + Ansi.green("✓") + " " + svc); - } - if (services.isEmpty()) { - printer.println(" " + Ansi.gray("(no services reported)")); - } - - return 0; + public OciServicesCommand() { + super(ProductProfile.OCI); } } diff --git a/src/main/java/io/floci/cli/commands/oci/OciSetupCommand.java b/src/main/java/io/floci/cli/commands/oci/OciSetupCommand.java index 3f65340..edb1a4a 100644 --- a/src/main/java/io/floci/cli/commands/oci/OciSetupCommand.java +++ b/src/main/java/io/floci/cli/commands/oci/OciSetupCommand.java @@ -1,6 +1,7 @@ package io.floci.cli.commands.oci; -import io.floci.cli.OciGlobalOptions; +import io.floci.cli.GlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.output.Ansi; import io.floci.cli.output.OutputFormat; import io.floci.cli.output.Printer; @@ -36,7 +37,7 @@ public class OciSetupCommand implements Callable { static final String KEY_FILE_NAME = "floci_key.pem"; @Mixin - OciGlobalOptions global; + GlobalOptions global = new GlobalOptions(ProductProfile.OCI); @Option(names = {"--profile-name"}, description = "Profile section name to write in ~/.oci/config (default: FLOCI)", diff --git a/src/main/java/io/floci/cli/commands/oci/OciStartCommand.java b/src/main/java/io/floci/cli/commands/oci/OciStartCommand.java index 581d77a..7b60917 100644 --- a/src/main/java/io/floci/cli/commands/oci/OciStartCommand.java +++ b/src/main/java/io/floci/cli/commands/oci/OciStartCommand.java @@ -1,113 +1,17 @@ package io.floci.cli.commands.oci; -import io.floci.cli.OciGlobalOptions; -import io.floci.cli.docker.DockerClient; -import io.floci.cli.docker.DockerException; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.StartCommand; +import picocli.CommandLine.Command; @Command( name = "start", description = "Start the Floci OCI emulator container", mixinStandardHelpOptions = true ) -public class OciStartCommand implements Callable { +public class OciStartCommand extends StartCommand { - @Mixin - OciGlobalOptions global; - - @Option(names = {"--port"}, description = "Host port to bind (default: 4599)", defaultValue = "4599", paramLabel = "") - int port; - - @Option(names = {"--persist"}, description = "Host directory for persistent state", paramLabel = "") - String persistDir; - - @Option(names = {"--services"}, description = "Comma-separated list of services to enable", paramLabel = "") - String services; - - @Option(names = {"--detach"}, description = "Return immediately without waiting for readiness") - boolean detach; - - @Option(names = {"--image"}, description = "Image reference to use (default: floci/floci-oci:latest)", defaultValue = "floci/floci-oci:latest", paramLabel = "") - String image; - - @Option(names = {"--pull"}, description = "Image pull policy: always, missing, never", defaultValue = "missing", paramLabel = "always|missing|never") - String pull; - - @Override - public Integer call() { - Printer printer = global.printer(); - DockerClient docker = new DockerClient(); - - if (!DockerClient.isInstalled()) { - printer.error("docker binary not found in PATH.\nInstall Docker Desktop from https://docs.docker.com/get-docker/"); - return 1; - } - - try { - var existing = docker.inspectContainer(global.container); - if (existing.isPresent()) { - String state = existing.get().state(); - if ("running".equals(state)) { - printer.error("Container '" + global.container + "' is already running.\nRun 'floci oci stop' first or pass --container to use a different name."); - return 1; - } - printer.println(Ansi.gray("Removing stopped container '" + global.container + "'...")); - docker.removeContainer(global.container); - } - } catch (DockerException e) { - printer.error("Failed to inspect container: " + e.getMessage()); - return 1; - } - - try { - printer.println(Ansi.gray("Checking image " + image + " (policy: " + pull + ")...")); - docker.pull(image, pull); - } catch (DockerException e) { - printer.error("Failed to pull image: " + e.getMessage() + "\nRun 'floci oci start --pull never' to skip pulling."); - return 1; - } - - List args = new ArrayList<>(); - args.addAll(List.of("-d", "--name", global.container)); - args.addAll(List.of("-p", port + ":4599")); - args.addAll(DockerClient.dockerSocketRunArgs()); - if (persistDir != null) { - args.addAll(List.of("-v", persistDir + ":/app/data")); - // The server defaults to in-memory storage; enable persistent mode so - // state is actually written to the mounted directory and survives restarts. - args.addAll(List.of("-e", "FLOCI_OCI_STORAGE_MODE=persistent")); - } - if (services != null && !services.isBlank()) { - args.addAll(List.of("-e", "FLOCI_OCI_SERVICES=" + services)); - } - args.add(image); - - try { - printer.println("Starting " + Ansi.gold("Floci OCI") + " container..."); - String id = docker.startContainer(args); - printer.println(Ansi.green("Container started") + " (" + id.substring(0, Math.min(12, id.length())) + ")"); - } catch (DockerException e) { - printer.error("Failed to start container: " + e.getMessage()); - return 1; - } - - if (detach) { - printer.println(Ansi.gray("Detached. Run 'floci oci wait' to poll for readiness.")); - return 0; - } - - global.endpoint = "http://localhost:" + port; - - printer.println(Ansi.gray("Waiting for Floci OCI to be ready...")); - OciWaitCommand wait = new OciWaitCommand(); - wait.global = global; - wait.timeout = "30s"; - return wait.call(); + public OciStartCommand() { + super(ProductProfile.OCI); } -} \ No newline at end of file +} diff --git a/src/main/java/io/floci/cli/commands/oci/OciStatusCommand.java b/src/main/java/io/floci/cli/commands/oci/OciStatusCommand.java index 6641bbf..46b3e1f 100644 --- a/src/main/java/io/floci/cli/commands/oci/OciStatusCommand.java +++ b/src/main/java/io/floci/cli/commands/oci/OciStatusCommand.java @@ -1,98 +1,17 @@ package io.floci.cli.commands.oci; -import io.floci.cli.OciGlobalOptions; -import io.floci.cli.docker.DockerClient; -import io.floci.cli.docker.DockerException; -import io.floci.cli.http.FlociHttpClient; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.OutputFormat; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Optional; -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.StatusCommand; +import picocli.CommandLine.Command; @Command( name = "status", description = "Show Floci OCI container and server status", mixinStandardHelpOptions = true ) -public class OciStatusCommand implements Callable { +public class OciStatusCommand extends StatusCommand { - @Mixin - OciGlobalOptions global; - - @Override - public Integer call() { - Printer printer = global.printer(); - DockerClient docker = new DockerClient(); - - String containerState = "not found"; - String containerImage = ""; - String containerPorts = ""; - String effectiveEndpoint = global.endpoint; - try { - Optional info = docker.inspectContainer(global.container); - if (info.isPresent()) { - containerState = info.get().state(); - containerImage = info.get().image(); - containerPorts = info.get().ports(); - effectiveEndpoint = global.endpointFromPorts(containerPorts, global.endpoint); - } - } catch (DockerException e) { - containerState = "error: " + e.getMessage(); - } - - String serverVersion = "unavailable"; - String serverEdition = ""; - boolean reachable = false; - FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, OciGlobalOptions.CONTROL_PREFIX); - try { - var health = client.health(); - serverVersion = health.version(); - serverEdition = health.edition(); - reachable = true; - } catch (Exception ignored) {} - - Map data = new LinkedHashMap<>(); - data.put("container", global.container); - data.put("state", containerState); - data.put("image", containerImage); - data.put("ports", containerPorts); - data.put("endpoint", effectiveEndpoint); - data.put("reachable", reachable); - data.put("version", serverVersion); - data.put("edition", serverEdition); - - if (printer.format() != OutputFormat.text) { - printer.structured(data); - return 0; - } - - printer.println(Ansi.bold("Floci OCI Status")); - printer.println(""); - String stateColor = switch (containerState) { - case "running" -> Ansi.green(containerState); - case "not found" -> Ansi.gray(containerState); - default -> Ansi.red(containerState); - }; - printer.println(" Container: " + global.container + " " + stateColor); - if (!containerImage.isBlank()) printer.println(" Image: " + containerImage); - if (!containerPorts.isBlank()) printer.println(" Ports: " + containerPorts); - printer.println(" Endpoint: " + effectiveEndpoint); - printer.println(" Reachable: " + (reachable ? Ansi.green("yes") : Ansi.red("no"))); - if (reachable) { - printer.println(" Version: " + serverVersion); - if (!serverEdition.isBlank()) printer.println(" Edition: " + serverEdition); - } - - if (!reachable && !"running".equals(containerState)) { - printer.println(""); - printer.println(Ansi.gray("Run 'floci oci start' to launch Floci OCI.")); - } - - return 0; + public OciStatusCommand() { + super(ProductProfile.OCI); } -} \ No newline at end of file +} diff --git a/src/main/java/io/floci/cli/commands/oci/OciStopCommand.java b/src/main/java/io/floci/cli/commands/oci/OciStopCommand.java index cf024ba..27b4da3 100644 --- a/src/main/java/io/floci/cli/commands/oci/OciStopCommand.java +++ b/src/main/java/io/floci/cli/commands/oci/OciStopCommand.java @@ -1,73 +1,17 @@ package io.floci.cli.commands.oci; -import io.floci.cli.OciGlobalOptions; -import io.floci.cli.docker.DockerClient; -import io.floci.cli.docker.DockerException; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.StopCommand; +import picocli.CommandLine.Command; @Command( name = "stop", description = "Stop the Floci OCI container", mixinStandardHelpOptions = true ) -public class OciStopCommand implements Callable { +public class OciStopCommand extends StopCommand { - @Mixin - OciGlobalOptions global; - - @Option(names = {"--remove", "-r"}, description = "Remove the container after stopping") - boolean remove; - - @Option(names = {"--timeout"}, description = "Seconds to wait before forcefully killing (default: 10)", defaultValue = "10", paramLabel = "") - int timeout; - - @Override - public Integer call() { - Printer printer = global.printer(); - DockerClient docker = new DockerClient(); - - try { - var info = docker.inspectContainer(global.container); - if (info.isEmpty()) { - printer.error("Container '" + global.container + "' not found.\nRun 'floci oci start' to launch one."); - return 1; - } - if (!"running".equals(info.get().state())) { - printer.println(Ansi.yellow("Container '" + global.container + "' is not running (state: " + info.get().state() + ")")); - if (remove) { - docker.removeContainer(global.container); - printer.println("Container removed."); - } - return 0; - } - } catch (DockerException e) { - printer.error("Failed to inspect container: " + e.getMessage()); - return 1; - } - - try { - printer.println("Stopping " + Ansi.gold("Floci OCI") + " container '" + global.container + "'..."); - docker.stopContainer(global.container, timeout); - printer.println(Ansi.green("Stopped.")); - } catch (DockerException e) { - printer.error("Failed to stop container: " + e.getMessage()); - return 1; - } - - if (remove) { - try { - docker.removeContainer(global.container); - printer.println("Container removed."); - } catch (DockerException e) { - printer.error("Failed to remove container: " + e.getMessage()); - return 1; - } - } - - return 0; + public OciStopCommand() { + super(ProductProfile.OCI); } -} \ No newline at end of file +} diff --git a/src/main/java/io/floci/cli/commands/oci/OciVersionCommand.java b/src/main/java/io/floci/cli/commands/oci/OciVersionCommand.java index 23cfe9b..c978a87 100644 --- a/src/main/java/io/floci/cli/commands/oci/OciVersionCommand.java +++ b/src/main/java/io/floci/cli/commands/oci/OciVersionCommand.java @@ -1,72 +1,17 @@ package io.floci.cli.commands.oci; -import io.floci.cli.OciGlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.commands.VersionCommand; -import io.floci.cli.docker.DockerClient; -import io.floci.cli.http.FlociHttpClient; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.OutputFormat; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Optional; -import java.util.concurrent.Callable; +import picocli.CommandLine.Command; @Command( name = "version", description = "Show CLI version, connected Floci OCI server version, and image digest", mixinStandardHelpOptions = true ) -public class OciVersionCommand implements Callable { +public class OciVersionCommand extends VersionCommand { - @Mixin - OciGlobalOptions global; - - @Override - public Integer call() { - Printer printer = global.printer(); - - String serverVersion = null; - String serverEdition = null; - String imageDigest = null; - - DockerClient docker = new DockerClient(); - String effectiveEndpoint = global.resolvedEndpoint(docker); - - FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, OciGlobalOptions.CONTROL_PREFIX); - try { - var info = client.info(); - serverVersion = info.version(); - serverEdition = info.edition(); - } catch (Exception ignored) {} - - try { - imageDigest = docker.imageDigest("floci/floci-oci").orElse(null); - } catch (Exception ignored) {} - - Map data = new LinkedHashMap<>(); - data.put("cli", VersionCommand.CLI_VERSION); - data.put("server", Optional.ofNullable(serverVersion).orElse("unavailable")); - data.put("edition", Optional.ofNullable(serverEdition).orElse("")); - if (imageDigest != null) data.put("digest", imageDigest); - - if (printer.format() != OutputFormat.text) { - printer.structured(data); - return 0; - } - - printer.println(Ansi.bold("Floci CLI") + " " + Ansi.gold(VersionCommand.CLI_VERSION)); - if (serverVersion != null) { - printer.println("Server: " + serverVersion + (serverEdition != null ? " (" + serverEdition + ")" : "")); - } else { - printer.println("Server: " + Ansi.gray("not reachable at " + effectiveEndpoint)); - } - if (imageDigest != null) { - printer.println("Image: " + imageDigest); - } - - return 0; + public OciVersionCommand() { + super(ProductProfile.OCI); } } diff --git a/src/main/java/io/floci/cli/commands/oci/OciWaitCommand.java b/src/main/java/io/floci/cli/commands/oci/OciWaitCommand.java index ce37c72..3e04262 100644 --- a/src/main/java/io/floci/cli/commands/oci/OciWaitCommand.java +++ b/src/main/java/io/floci/cli/commands/oci/OciWaitCommand.java @@ -1,73 +1,17 @@ package io.floci.cli.commands.oci; -import io.floci.cli.OciGlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.commands.WaitCommand; -import io.floci.cli.http.FlociHttpClient; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.OutputFormat; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.time.Duration; -import java.time.Instant; -import java.util.Map; -import java.util.concurrent.Callable; +import picocli.CommandLine.Command; @Command( name = "wait", description = "Wait until Floci OCI is ready to accept requests", mixinStandardHelpOptions = true ) -public class OciWaitCommand implements Callable { +public class OciWaitCommand extends WaitCommand { - @Mixin - OciGlobalOptions global; - - @Option(names = {"--timeout"}, description = "Maximum time to wait (e.g. 30s, 2m)", defaultValue = "30s", paramLabel = "") - String timeout; - - @Option(names = {"--service"}, description = "Wait until a specific service is enabled", paramLabel = "") - String service; - - @Override - public Integer call() { - Printer printer = global.printer(); - long timeoutMillis = WaitCommand.parseDuration(timeout); - String effectiveEndpoint = global.resolvedEndpoint(new io.floci.cli.docker.DockerClient()); - FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, OciGlobalOptions.CONTROL_PREFIX); - Instant deadline = Instant.now().plusMillis(timeoutMillis); - - while (Instant.now().isBefore(deadline)) { - if (isReady(client, service)) { - if (printer.format() != OutputFormat.text) { - printer.structured(Map.of("ready", true, "endpoint", effectiveEndpoint)); - } else { - printer.println(Ansi.green("Floci OCI is ready") + " (" + effectiveEndpoint + ")"); - } - return 0; - } - long remaining = Duration.between(Instant.now(), deadline).toSeconds(); - printer.print("\r" + Ansi.gray("Waiting... (" + remaining + "s remaining)") + " "); - try { Thread.sleep(500); } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - break; - } - } - - printer.error("Timed out waiting for Floci OCI after " + timeout + ".\nIs the container running? Try 'floci oci status'."); - return 1; + public OciWaitCommand() { + super(ProductProfile.OCI); } - - private boolean isReady(FlociHttpClient client, String requiredService) { - try { - var health = client.health(); - if (requiredService == null) return true; - for (String s : health.services()) { - if (s.equalsIgnoreCase(requiredService)) return true; - } - return false; - } catch (Exception e) { - return false; - } - } -} \ No newline at end of file +} diff --git a/src/main/java/io/floci/cli/commands/oci/config/OciConfigProfileCommand.java b/src/main/java/io/floci/cli/commands/oci/config/OciConfigProfileCommand.java index d66c0a8..d8e4fd5 100644 --- a/src/main/java/io/floci/cli/commands/oci/config/OciConfigProfileCommand.java +++ b/src/main/java/io/floci/cli/commands/oci/config/OciConfigProfileCommand.java @@ -1,115 +1,17 @@ package io.floci.cli.commands.oci.config; -import io.floci.cli.OciGlobalOptions; -import io.floci.cli.config.Profile; -import io.floci.cli.config.ProfileStore; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.io.IOException; -import java.util.List; -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.config.ConfigProfileCommand; +import picocli.CommandLine.Command; @Command( name = "profile", description = "Manage Floci OCI configuration profiles", - mixinStandardHelpOptions = true, - subcommands = {HelpCommand.class} + mixinStandardHelpOptions = true ) -public class OciConfigProfileCommand implements Callable { +public class OciConfigProfileCommand extends ConfigProfileCommand { - @Mixin - OciGlobalOptions global; - - @Parameters(index = "0", description = "Action: list, show, create, delete", paramLabel = "list|show|create|delete") - String action; - - @Parameters(index = "1", description = "Profile name (required for show, create, delete)", arity = "0..1", paramLabel = "") - String name; - - @Override - public Integer call() { - Printer printer = global.printer(); - ProfileStore store = new ProfileStore(); - - return switch (action.toLowerCase()) { - case "list" -> listProfiles(printer, store); - case "show" -> showProfile(printer, store); - case "create" -> createProfile(printer, store); - case "delete" -> deleteProfile(printer, store); - default -> { - printer.error("Unknown action '" + action + "'. Use: list, show, create, delete"); - yield 1; - } - }; - } - - private int listProfiles(Printer printer, ProfileStore store) { - try { - List profiles = store.list(); - if (profiles.isEmpty()) { - printer.println(Ansi.gray("No profiles found. Create one with: floci oci config profile create ")); - return 0; - } - printer.println(Ansi.bold("Profiles:")); - for (Profile p : profiles) { - printer.println(" " + p.name + Ansi.gray(" " + (p.endpoint != null ? p.endpoint : ""))); - } - return 0; - } catch (IOException e) { - printer.error("Could not list profiles: " + e.getMessage()); - return 1; - } - } - - private int showProfile(Printer printer, ProfileStore store) { - if (name == null) { printer.error("Profile name required."); return 1; } - try { - var profile = store.get(name); - if (profile.isEmpty()) { printer.error("Profile '" + name + "' not found."); return 1; } - Profile p = profile.get(); - printer.println(Ansi.bold("Profile: ") + p.name); - if (p.endpoint != null) printer.println(" endpoint: " + p.endpoint); - if (p.container != null) printer.println(" container: " + p.container); - if (p.image != null) printer.println(" image: " + p.image); - if (p.port != null) printer.println(" port: " + p.port); - if (p.persistDir != null) printer.println(" persistDir: " + p.persistDir); - if (p.services != null) printer.println(" services: " + p.services); - return 0; - } catch (IOException e) { - printer.error("Could not read profile: " + e.getMessage()); - return 1; - } - } - - private int createProfile(Printer printer, ProfileStore store) { - if (name == null) { printer.error("Profile name required."); return 1; } - try { - if (store.get(name).isPresent()) { - printer.error("Profile '" + name + "' already exists. Delete it first or edit " + store.profileFile(name)); - return 1; - } - Profile p = new Profile(name); - store.save(p); - printer.println(Ansi.green("Created") + " profile '" + name + "' at " + store.profileFile(name)); - printer.println(Ansi.gray("Edit the file to customize endpoint, container, image, etc.")); - return 0; - } catch (IOException e) { - printer.error("Could not create profile: " + e.getMessage()); - return 1; - } - } - - private int deleteProfile(Printer printer, ProfileStore store) { - if (name == null) { printer.error("Profile name required."); return 1; } - try { - if (!store.delete(name)) { printer.error("Profile '" + name + "' not found."); return 1; } - printer.println(Ansi.green("Deleted") + " profile '" + name + "'"); - return 0; - } catch (IOException e) { - printer.error("Could not delete profile: " + e.getMessage()); - return 1; - } + public OciConfigProfileCommand() { + super(ProductProfile.OCI); } } diff --git a/src/main/java/io/floci/cli/commands/oci/config/OciConfigShowCommand.java b/src/main/java/io/floci/cli/commands/oci/config/OciConfigShowCommand.java index 027dceb..24fd942 100644 --- a/src/main/java/io/floci/cli/commands/oci/config/OciConfigShowCommand.java +++ b/src/main/java/io/floci/cli/commands/oci/config/OciConfigShowCommand.java @@ -1,70 +1,17 @@ package io.floci.cli.commands.oci.config; -import io.floci.cli.OciGlobalOptions; -import io.floci.cli.config.Profile; -import io.floci.cli.config.ProfileStore; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.OutputFormat; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Optional; -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.config.ConfigShowCommand; +import picocli.CommandLine.Command; @Command( name = "show", description = "Show the active Floci OCI configuration", mixinStandardHelpOptions = true ) -public class OciConfigShowCommand implements Callable { +public class OciConfigShowCommand extends ConfigShowCommand { - @Mixin - OciGlobalOptions global; - - @Override - public Integer call() { - Printer printer = global.printer(); - ProfileStore store = new ProfileStore(); - - Profile active = new Profile(global.profile != null ? global.profile : "default"); - active.endpoint = global.endpoint; - active.container = global.container; - active.output = global.output != null ? global.output.name() : "text"; - - if (global.profile != null) { - try { - Optional loaded = store.get(global.profile); - if (loaded.isPresent()) { - active = loaded.get(); - } else { - printer.warn("Profile '" + global.profile + "' not found. Using defaults."); - } - } catch (IOException e) { - printer.warn("Could not load profile: " + e.getMessage()); - } - } - - Map data = new LinkedHashMap<>(); - data.put("profile", active.name); - data.put("endpoint", active.endpoint != null ? active.endpoint : global.endpoint); - data.put("container", active.container != null ? active.container : global.container); - data.put("output", active.output); - - if (printer.format() != OutputFormat.text) { - printer.structured(data); - return 0; - } - - printer.println(Ansi.bold("Active Configuration (Floci OCI)")); - printer.println(""); - printer.println(" Profile: " + active.name); - printer.println(" Endpoint: " + (active.endpoint != null ? active.endpoint : global.endpoint)); - printer.println(" Container: " + (active.container != null ? active.container : global.container)); - printer.println(" Output: " + (active.output != null ? active.output : "text")); - - return 0; + public OciConfigShowCommand() { + super(ProductProfile.OCI); } } diff --git a/src/main/java/io/floci/cli/commands/oci/config/OciConfigValidateCommand.java b/src/main/java/io/floci/cli/commands/oci/config/OciConfigValidateCommand.java index b13754b..676c3d9 100644 --- a/src/main/java/io/floci/cli/commands/oci/config/OciConfigValidateCommand.java +++ b/src/main/java/io/floci/cli/commands/oci/config/OciConfigValidateCommand.java @@ -1,91 +1,17 @@ package io.floci.cli.commands.oci.config; -import io.floci.cli.OciGlobalOptions; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.Callable; -import java.util.regex.Pattern; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.config.ConfigValidateCommand; +import picocli.CommandLine.Command; @Command( name = "validate", description = "Validate a docker-compose.yml file for Floci OCI compatibility", mixinStandardHelpOptions = true ) -public class OciConfigValidateCommand implements Callable { +public class OciConfigValidateCommand extends ConfigValidateCommand { - @Mixin - OciGlobalOptions global; - - @Option(names = {"--file", "-f"}, description = "Path to docker-compose file", paramLabel = "") - String file; - - @Override - public Integer call() { - Printer printer = global.printer(); - - Path composeFile = resolveComposeFile(); - if (composeFile == null) { - printer.error("No docker-compose file found. Pass --file or run in a directory with docker-compose.yml."); - return 1; - } - - String content; - try { - content = Files.readString(composeFile); - } catch (IOException e) { - printer.error("Could not read " + composeFile + ": " + e.getMessage()); - return 1; - } - - printer.println(Ansi.bold("Validating") + " " + composeFile); - printer.println(""); - - List issues = new ArrayList<>(); - List warnings = new ArrayList<>(); - - if (!content.contains("/var/run/docker.sock")) { - warnings.add("Missing /var/run/docker.sock volume mount — required for Kafka/Redpanda support"); - } - - if (!Pattern.compile("4599\\s*:\\s*4599").matcher(content).find()) { - issues.add("Port mapping 4599:4599 not found — Floci OCI listens on 4599 by default"); - } - - if (!content.contains("floci/floci-oci") && !content.contains("ghcr.io/floci-io/floci-oci")) { - warnings.add("Floci OCI image reference not detected — ensure your service uses floci/floci-oci"); - } - - for (String issue : issues) { - printer.println(" " + Ansi.red("✗") + " " + issue); - } - for (String warn : warnings) { - printer.println(" " + Ansi.yellow("⚠") + " " + warn); - } - if (issues.isEmpty() && warnings.isEmpty()) { - printer.println(" " + Ansi.green("✓") + " Compose file looks good"); - } - - printer.println(""); - if (!issues.isEmpty()) { - printer.println(issues.size() + " error(s) found."); - return 1; - } - return 0; - } - - private Path resolveComposeFile() { - if (file != null) return Path.of(file); - for (String name : List.of("docker-compose.yml", "docker-compose.yaml", "compose.yml", "compose.yaml")) { - Path p = Path.of(name); - if (Files.exists(p)) return p; - } - return null; + public OciConfigValidateCommand() { + super(ProductProfile.OCI, false, "Functions (Fn Project sidecar)"); } } diff --git a/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotCommand.java b/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotCommand.java index 17e8cae..0ea5810 100644 --- a/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotCommand.java +++ b/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotCommand.java @@ -5,7 +5,7 @@ import picocli.CommandLine.*; @Command( name = "snapshot", - description = "Manage Floci OCI state snapshots (coming soon)", + description = "Manage Floci OCI state snapshots", mixinStandardHelpOptions = true, subcommands = { OciSnapshotSaveCommand.class, diff --git a/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotDeleteCommand.java b/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotDeleteCommand.java index 0669160..4a6a5dc 100644 --- a/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotDeleteCommand.java +++ b/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotDeleteCommand.java @@ -1,28 +1,17 @@ package io.floci.cli.commands.oci.snapshot; -import io.floci.cli.OciGlobalOptions; -import io.floci.cli.output.Ansi; -import picocli.CommandLine.*; - -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.snapshot.SnapshotDeleteCommand; +import picocli.CommandLine.Command; @Command( name = "delete", description = "Delete a Floci OCI snapshot", mixinStandardHelpOptions = true ) -public class OciSnapshotDeleteCommand implements Callable { +public class OciSnapshotDeleteCommand extends SnapshotDeleteCommand { - @Mixin - OciGlobalOptions global; - - @Parameters(index = "0", description = "Snapshot name", paramLabel = "") - String name; - - @Override - public Integer call() { - global.printer().println(Ansi.yellow("Snapshots are not yet available for Floci OCI.") + - "\nTrack progress: https://github.com/floci-io/floci-oci/issues"); - return 0; + public OciSnapshotDeleteCommand() { + super(ProductProfile.OCI); } } diff --git a/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotExportCommand.java b/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotExportCommand.java index cf4ce92..afc7359 100644 --- a/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotExportCommand.java +++ b/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotExportCommand.java @@ -1,31 +1,17 @@ package io.floci.cli.commands.oci.snapshot; -import io.floci.cli.OciGlobalOptions; -import io.floci.cli.output.Ansi; -import picocli.CommandLine.*; - -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.snapshot.SnapshotExportCommand; +import picocli.CommandLine.Command; @Command( name = "export", description = "Export a Floci OCI snapshot to a tarball file", mixinStandardHelpOptions = true ) -public class OciSnapshotExportCommand implements Callable { +public class OciSnapshotExportCommand extends SnapshotExportCommand { - @Mixin - OciGlobalOptions global; - - @Parameters(index = "0", description = "Snapshot name", paramLabel = "") - String name; - - @Option(names = {"--file", "-f"}, description = "Destination file path", required = true, paramLabel = "") - String outputFile; - - @Override - public Integer call() { - global.printer().println(Ansi.yellow("Snapshots are not yet available for Floci OCI.") + - "\nTrack progress: https://github.com/floci-io/floci-oci/issues"); - return 0; + public OciSnapshotExportCommand() { + super(ProductProfile.OCI); } } diff --git a/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotImportCommand.java b/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotImportCommand.java index c6b4c8f..d5fc152 100644 --- a/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotImportCommand.java +++ b/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotImportCommand.java @@ -1,31 +1,17 @@ package io.floci.cli.commands.oci.snapshot; -import io.floci.cli.OciGlobalOptions; -import io.floci.cli.output.Ansi; -import picocli.CommandLine.*; - -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.snapshot.SnapshotImportCommand; +import picocli.CommandLine.Command; @Command( name = "import", description = "Import a Floci OCI snapshot from a tarball file", mixinStandardHelpOptions = true ) -public class OciSnapshotImportCommand implements Callable { +public class OciSnapshotImportCommand extends SnapshotImportCommand { - @Mixin - OciGlobalOptions global; - - @Parameters(index = "0", description = "Snapshot name", paramLabel = "") - String name; - - @Option(names = {"--file", "-f"}, description = "Source file path", required = true, paramLabel = "") - String inputFile; - - @Override - public Integer call() { - global.printer().println(Ansi.yellow("Snapshots are not yet available for Floci OCI.") + - "\nTrack progress: https://github.com/floci-io/floci-oci/issues"); - return 0; + public OciSnapshotImportCommand() { + super(ProductProfile.OCI); } } diff --git a/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotListCommand.java b/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotListCommand.java index 7f9df90..e3d4c20 100644 --- a/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotListCommand.java +++ b/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotListCommand.java @@ -1,34 +1,17 @@ package io.floci.cli.commands.oci.snapshot; -import io.floci.cli.OciGlobalOptions; -import io.floci.cli.output.Ansi; -import io.floci.cli.output.OutputFormat; -import io.floci.cli.output.Printer; -import picocli.CommandLine.*; - -import java.util.List; -import java.util.Map; -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.snapshot.SnapshotListCommand; +import picocli.CommandLine.Command; @Command( name = "list", description = "List available Floci OCI snapshots", mixinStandardHelpOptions = true ) -public class OciSnapshotListCommand implements Callable { +public class OciSnapshotListCommand extends SnapshotListCommand { - @Mixin - OciGlobalOptions global; - - @Override - public Integer call() { - Printer printer = global.printer(); - if (printer.format() != OutputFormat.text) { - printer.structured(Map.of("snapshots", List.of(), "note", "not yet available")); - return 0; - } - printer.println(Ansi.gray("No snapshots found.") + - " (Snapshot support for Floci OCI is coming — https://github.com/floci-io/floci-oci/issues)"); - return 0; + public OciSnapshotListCommand() { + super(ProductProfile.OCI); } } diff --git a/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotLoadCommand.java b/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotLoadCommand.java index c446226..4331146 100644 --- a/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotLoadCommand.java +++ b/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotLoadCommand.java @@ -1,28 +1,17 @@ package io.floci.cli.commands.oci.snapshot; -import io.floci.cli.OciGlobalOptions; -import io.floci.cli.output.Ansi; -import picocli.CommandLine.*; - -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.snapshot.SnapshotLoadCommand; +import picocli.CommandLine.Command; @Command( name = "load", description = "Load a Floci OCI snapshot", mixinStandardHelpOptions = true ) -public class OciSnapshotLoadCommand implements Callable { +public class OciSnapshotLoadCommand extends SnapshotLoadCommand { - @Mixin - OciGlobalOptions global; - - @Parameters(index = "0", description = "Snapshot name", paramLabel = "") - String name; - - @Override - public Integer call() { - global.printer().println(Ansi.yellow("Snapshots are not yet available for Floci OCI.") + - "\nTrack progress: https://github.com/floci-io/floci-oci/issues"); - return 0; + public OciSnapshotLoadCommand() { + super(ProductProfile.OCI); } } diff --git a/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotSaveCommand.java b/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotSaveCommand.java index 133c80a..4b580d1 100644 --- a/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotSaveCommand.java +++ b/src/main/java/io/floci/cli/commands/oci/snapshot/OciSnapshotSaveCommand.java @@ -1,31 +1,17 @@ package io.floci.cli.commands.oci.snapshot; -import io.floci.cli.OciGlobalOptions; -import io.floci.cli.output.Ansi; -import picocli.CommandLine.*; - -import java.util.concurrent.Callable; +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.snapshot.SnapshotSaveCommand; +import picocli.CommandLine.Command; @Command( name = "save", description = "Save the current Floci OCI state as a named snapshot", mixinStandardHelpOptions = true ) -public class OciSnapshotSaveCommand implements Callable { +public class OciSnapshotSaveCommand extends SnapshotSaveCommand { - @Mixin - OciGlobalOptions global; - - @Parameters(index = "0", description = "Snapshot name", paramLabel = "") - String name; - - @Option(names = {"--message", "-m"}, description = "Optional description", paramLabel = "") - String message; - - @Override - public Integer call() { - global.printer().println(Ansi.yellow("Snapshots are not yet available for Floci OCI.") + - "\nTrack progress: https://github.com/floci-io/floci-oci/issues"); - return 0; + public OciSnapshotSaveCommand() { + super(ProductProfile.OCI); } } diff --git a/src/main/java/io/floci/cli/commands/snapshot/SnapshotCommand.java b/src/main/java/io/floci/cli/commands/snapshot/SnapshotCommand.java index 7a65c9b..aecbf7c 100644 --- a/src/main/java/io/floci/cli/commands/snapshot/SnapshotCommand.java +++ b/src/main/java/io/floci/cli/commands/snapshot/SnapshotCommand.java @@ -5,7 +5,7 @@ import picocli.CommandLine.*; @Command( name = "snapshot", - description = "Manage Floci state snapshots", + description = "Manage Floci AWS state snapshots", mixinStandardHelpOptions = true, subcommands = { SnapshotSaveCommand.class, diff --git a/src/main/java/io/floci/cli/commands/snapshot/SnapshotDeleteCommand.java b/src/main/java/io/floci/cli/commands/snapshot/SnapshotDeleteCommand.java index 432f035..f63e302 100644 --- a/src/main/java/io/floci/cli/commands/snapshot/SnapshotDeleteCommand.java +++ b/src/main/java/io/floci/cli/commands/snapshot/SnapshotDeleteCommand.java @@ -1,6 +1,8 @@ package io.floci.cli.commands.snapshot; import io.floci.cli.GlobalOptions; +import io.floci.cli.ProductProfile; +import io.floci.cli.docker.DockerClient; import io.floci.cli.http.FlociException; import io.floci.cli.http.FlociHttpClient; import io.floci.cli.output.Ansi; @@ -11,13 +13,24 @@ import java.util.concurrent.Callable; @Command( name = "delete", - description = "Delete a snapshot", + description = "Delete a Floci AWS snapshot", mixinStandardHelpOptions = true ) public class SnapshotDeleteCommand implements Callable { + protected final ProductProfile profile; + @Mixin - GlobalOptions global; + protected GlobalOptions global; + + public SnapshotDeleteCommand() { + this(ProductProfile.AWS); + } + + protected SnapshotDeleteCommand(ProductProfile profile) { + this.profile = profile; + this.global = new GlobalOptions(profile); + } @Parameters(index = "0", description = "Snapshot name", paramLabel = "") String name; @@ -25,7 +38,10 @@ public class SnapshotDeleteCommand implements Callable { @Override public Integer call() { Printer printer = global.printer(); - FlociHttpClient client = new FlociHttpClient(global.endpoint); + // Resolve the endpoint from the container's port mapping, like status/services/wait do, + // so a container started with --port is still found without --endpoint. + String effectiveEndpoint = global.resolvedEndpoint(new DockerClient()); + FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile.controlPrefix()); try { client.deleteSnapshot(name); printer.println(Ansi.green("Snapshot deleted:") + " " + name); diff --git a/src/main/java/io/floci/cli/commands/snapshot/SnapshotExportCommand.java b/src/main/java/io/floci/cli/commands/snapshot/SnapshotExportCommand.java index f1e4f71..5e9dfdc 100644 --- a/src/main/java/io/floci/cli/commands/snapshot/SnapshotExportCommand.java +++ b/src/main/java/io/floci/cli/commands/snapshot/SnapshotExportCommand.java @@ -1,6 +1,7 @@ package io.floci.cli.commands.snapshot; import io.floci.cli.GlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.output.Printer; import picocli.CommandLine.*; @@ -8,13 +9,24 @@ import java.util.concurrent.Callable; @Command( name = "export", - description = "Export a snapshot to a tarball file", + description = "Export a Floci AWS snapshot to a tarball file", mixinStandardHelpOptions = true ) public class SnapshotExportCommand implements Callable { + protected final ProductProfile profile; + @Mixin - GlobalOptions global; + protected GlobalOptions global; + + public SnapshotExportCommand() { + this(ProductProfile.AWS); + } + + protected SnapshotExportCommand(ProductProfile profile) { + this.profile = profile; + this.global = new GlobalOptions(profile); + } @Parameters(index = "0", description = "Snapshot name", paramLabel = "") String name; @@ -27,7 +39,7 @@ public class SnapshotExportCommand implements Callable { Printer printer = global.printer(); // TODO: implement GET /_floci/snapshots//export streaming to file printer.error("snapshot export requires floci-server >= 1.6.0\n" + - "See TODO.md for the required server-side API contract."); + "Track progress: " + profile.serverRepo() + "/issues"); return 1; } } diff --git a/src/main/java/io/floci/cli/commands/snapshot/SnapshotImportCommand.java b/src/main/java/io/floci/cli/commands/snapshot/SnapshotImportCommand.java index 887e55d..a19c1e6 100644 --- a/src/main/java/io/floci/cli/commands/snapshot/SnapshotImportCommand.java +++ b/src/main/java/io/floci/cli/commands/snapshot/SnapshotImportCommand.java @@ -1,6 +1,7 @@ package io.floci.cli.commands.snapshot; import io.floci.cli.GlobalOptions; +import io.floci.cli.ProductProfile; import io.floci.cli.output.Printer; import picocli.CommandLine.*; @@ -8,13 +9,24 @@ import java.util.concurrent.Callable; @Command( name = "import", - description = "Import a snapshot from a tarball file", + description = "Import a Floci AWS snapshot from a tarball file", mixinStandardHelpOptions = true ) public class SnapshotImportCommand implements Callable { + protected final ProductProfile profile; + @Mixin - GlobalOptions global; + protected GlobalOptions global; + + public SnapshotImportCommand() { + this(ProductProfile.AWS); + } + + protected SnapshotImportCommand(ProductProfile profile) { + this.profile = profile; + this.global = new GlobalOptions(profile); + } @Option(names = {"--file", "-f"}, description = "Source tarball path", required = true, paramLabel = "") String inputFile; @@ -27,7 +39,7 @@ public class SnapshotImportCommand implements Callable { Printer printer = global.printer(); // TODO: implement POST /_floci/snapshots/import with multipart upload printer.error("snapshot import requires floci-server >= 1.6.0\n" + - "See TODO.md for the required server-side API contract."); + "Track progress: " + profile.serverRepo() + "/issues"); return 1; } } diff --git a/src/main/java/io/floci/cli/commands/snapshot/SnapshotListCommand.java b/src/main/java/io/floci/cli/commands/snapshot/SnapshotListCommand.java index 143c97d..1f0ebf8 100644 --- a/src/main/java/io/floci/cli/commands/snapshot/SnapshotListCommand.java +++ b/src/main/java/io/floci/cli/commands/snapshot/SnapshotListCommand.java @@ -1,6 +1,8 @@ package io.floci.cli.commands.snapshot; import io.floci.cli.GlobalOptions; +import io.floci.cli.ProductProfile; +import io.floci.cli.docker.DockerClient; import io.floci.cli.http.FlociException; import io.floci.cli.http.FlociHttpClient; import io.floci.cli.output.Ansi; @@ -12,18 +14,32 @@ import java.util.concurrent.Callable; @Command( name = "list", - description = "List available snapshots", + description = "List available Floci AWS snapshots", mixinStandardHelpOptions = true ) public class SnapshotListCommand implements Callable { + protected final ProductProfile profile; + @Mixin - GlobalOptions global; + protected GlobalOptions global; + + public SnapshotListCommand() { + this(ProductProfile.AWS); + } + + protected SnapshotListCommand(ProductProfile profile) { + this.profile = profile; + this.global = new GlobalOptions(profile); + } @Override public Integer call() { Printer printer = global.printer(); - FlociHttpClient client = new FlociHttpClient(global.endpoint); + // Resolve the endpoint from the container's port mapping, like status/services/wait do, + // so a container started with --port is still found without --endpoint. + String effectiveEndpoint = global.resolvedEndpoint(new DockerClient()); + FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile.controlPrefix()); try { var node = client.listSnapshots(); if (printer.format() != OutputFormat.text) { @@ -40,7 +56,7 @@ public class SnapshotListCommand implements Callable { } catch (FlociException e) { if (e.getMessage().contains("404") || e.getMessage().contains("501")) { printer.error("Snapshot API not available on this server version.\n" + - "See TODO.md for the required server-side API contract."); + "Track progress: " + profile.serverRepo() + "/issues"); } else { printer.error("Failed to list snapshots: " + e.getMessage()); } diff --git a/src/main/java/io/floci/cli/commands/snapshot/SnapshotLoadCommand.java b/src/main/java/io/floci/cli/commands/snapshot/SnapshotLoadCommand.java index d771091..82ab9f5 100644 --- a/src/main/java/io/floci/cli/commands/snapshot/SnapshotLoadCommand.java +++ b/src/main/java/io/floci/cli/commands/snapshot/SnapshotLoadCommand.java @@ -1,6 +1,8 @@ package io.floci.cli.commands.snapshot; import io.floci.cli.GlobalOptions; +import io.floci.cli.ProductProfile; +import io.floci.cli.docker.DockerClient; import io.floci.cli.http.FlociException; import io.floci.cli.http.FlociHttpClient; import io.floci.cli.output.Ansi; @@ -11,13 +13,24 @@ import java.util.concurrent.Callable; @Command( name = "load", - description = "Restore a saved snapshot", + description = "Load a Floci AWS snapshot", mixinStandardHelpOptions = true ) public class SnapshotLoadCommand implements Callable { + protected final ProductProfile profile; + @Mixin - GlobalOptions global; + protected GlobalOptions global; + + public SnapshotLoadCommand() { + this(ProductProfile.AWS); + } + + protected SnapshotLoadCommand(ProductProfile profile) { + this.profile = profile; + this.global = new GlobalOptions(profile); + } @Parameters(index = "0", description = "Snapshot name", paramLabel = "") String name; @@ -25,7 +38,10 @@ public class SnapshotLoadCommand implements Callable { @Override public Integer call() { Printer printer = global.printer(); - FlociHttpClient client = new FlociHttpClient(global.endpoint); + // Resolve the endpoint from the container's port mapping, like status/services/wait do, + // so a container started with --port is still found without --endpoint. + String effectiveEndpoint = global.resolvedEndpoint(new DockerClient()); + FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile.controlPrefix()); try { client.loadSnapshot(name); printer.println(Ansi.green("Snapshot loaded:") + " " + name); @@ -33,7 +49,7 @@ public class SnapshotLoadCommand implements Callable { } catch (FlociException e) { if (e.getMessage().contains("404") || e.getMessage().contains("501")) { printer.error("Snapshot API not available on this server version.\n" + - "See TODO.md for the required server-side API contract."); + "Track progress: " + profile.serverRepo() + "/issues"); } else { printer.error("Failed to load snapshot: " + e.getMessage()); } diff --git a/src/main/java/io/floci/cli/commands/snapshot/SnapshotSaveCommand.java b/src/main/java/io/floci/cli/commands/snapshot/SnapshotSaveCommand.java index 6603ebf..5aaa60c 100644 --- a/src/main/java/io/floci/cli/commands/snapshot/SnapshotSaveCommand.java +++ b/src/main/java/io/floci/cli/commands/snapshot/SnapshotSaveCommand.java @@ -1,6 +1,8 @@ package io.floci.cli.commands.snapshot; import io.floci.cli.GlobalOptions; +import io.floci.cli.ProductProfile; +import io.floci.cli.docker.DockerClient; import io.floci.cli.http.FlociException; import io.floci.cli.http.FlociHttpClient; import io.floci.cli.output.Ansi; @@ -11,13 +13,24 @@ import java.util.concurrent.Callable; @Command( name = "save", - description = "Save the current Floci state as a named snapshot", + description = "Save the current Floci AWS state as a named snapshot", mixinStandardHelpOptions = true ) public class SnapshotSaveCommand implements Callable { + protected final ProductProfile profile; + @Mixin - GlobalOptions global; + protected GlobalOptions global; + + public SnapshotSaveCommand() { + this(ProductProfile.AWS); + } + + protected SnapshotSaveCommand(ProductProfile profile) { + this.profile = profile; + this.global = new GlobalOptions(profile); + } @Parameters(index = "0", description = "Snapshot name", paramLabel = "") String name; @@ -28,7 +41,10 @@ public class SnapshotSaveCommand implements Callable { @Override public Integer call() { Printer printer = global.printer(); - FlociHttpClient client = new FlociHttpClient(global.endpoint); + // Resolve the endpoint from the container's port mapping, like status/services/wait do, + // so a container started with --port is still found without --endpoint. + String effectiveEndpoint = global.resolvedEndpoint(new DockerClient()); + FlociHttpClient client = new FlociHttpClient(effectiveEndpoint, profile.controlPrefix()); try { client.postSnapshot(name); printer.println(Ansi.green("Snapshot saved:") + " " + name); @@ -36,7 +52,7 @@ public class SnapshotSaveCommand implements Callable { } catch (FlociException e) { if (e.getMessage().contains("404") || e.getMessage().contains("501")) { printer.error("Snapshot API not available on this server version.\n" + - "See TODO.md for the required server-side API contract."); + "Track progress: " + profile.serverRepo() + "/issues"); } else { printer.error("Failed to save snapshot: " + e.getMessage()); } diff --git a/src/main/java/io/floci/cli/util/Durations.java b/src/main/java/io/floci/cli/util/Durations.java new file mode 100644 index 0000000..3e6e4ed --- /dev/null +++ b/src/main/java/io/floci/cli/util/Durations.java @@ -0,0 +1,18 @@ +package io.floci.cli.util; + +/** Parses human-friendly duration strings like {@code 30s}, {@code 2m}, {@code 500ms}. */ +public final class Durations { + + /** Returns milliseconds; a bare number is seconds; null/blank defaults to 30s. */ + public static long parseDuration(String s) { + if (s == null || s.isBlank()) return 30_000; + s = s.trim().toLowerCase(); + if (s.endsWith("ms")) return Long.parseLong(s.substring(0, s.length() - 2)); + if (s.endsWith("s")) return Long.parseLong(s.substring(0, s.length() - 1)) * 1000; + if (s.endsWith("m")) return Long.parseLong(s.substring(0, s.length() - 1)) * 60_000; + if (s.endsWith("h")) return Long.parseLong(s.substring(0, s.length() - 1)) * 3_600_000; + return Long.parseLong(s) * 1000; + } + + private Durations() {} +} diff --git a/src/test/java/io/floci/cli/unit/DoctorCheckListTest.java b/src/test/java/io/floci/cli/unit/DoctorCheckListTest.java new file mode 100644 index 0000000..78cdd65 --- /dev/null +++ b/src/test/java/io/floci/cli/unit/DoctorCheckListTest.java @@ -0,0 +1,45 @@ +package io.floci.cli.unit; + +import io.floci.cli.ProductProfile; +import io.floci.cli.commands.DoctorCommand; +import io.floci.cli.doctor.Check; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +/** Pins the per-product doctor check composition and order (Docker checks first, then companions). */ +class DoctorCheckListTest { + + private static final List BASE = List.of( + "DockerInstalledCheck", "DockerDaemonCheck", "DockerSocketCheck", "DockerVersionCheck", + "PortAvailableCheck", "ImagePresentCheck", "ImageVersionCheck", + "ContainerRunningCheck", "EndpointReachableCheck"); + + private static List names(List checks) { + return checks.stream().map(c -> c.getClass().getSimpleName()).toList(); + } + + @Test + void awsHasBaseChecksPlusAwsCliCompanions() { + var names = names(new DoctorCommand().allChecks()); + assertEquals(BASE.size() + 2, names.size()); + assertEquals(BASE, names.subList(0, BASE.size())); + assertEquals(List.of("AwsCliEndpointCheck", "AwsCliS3PathStyleCheck"), names.subList(BASE.size(), names.size())); + } + + @Test + void azHasBaseChecksPlusAzCliCompanions() { + var names = names(new DoctorCommand(ProductProfile.AZ, DoctorCommand.AZ_COMPANION_CHECKS).allChecks()); + assertEquals(BASE, names.subList(0, BASE.size())); + assertEquals(List.of("AzCliInstalledCheck", "AzCliConnectionStringCheck"), names.subList(BASE.size(), names.size())); + } + + @Test + void gcpAndOciHaveExactlyTheBaseChecks() { + for (ProductProfile p : List.of(ProductProfile.GCP, ProductProfile.OCI)) { + assertEquals(BASE, names(new DoctorCommand(p, List.of()).allChecks()), p.name()); + } + } +} diff --git a/src/test/java/io/floci/cli/unit/GlobalOptionsTest.java b/src/test/java/io/floci/cli/unit/GlobalOptionsTest.java new file mode 100644 index 0000000..796ab5d --- /dev/null +++ b/src/test/java/io/floci/cli/unit/GlobalOptionsTest.java @@ -0,0 +1,79 @@ +package io.floci.cli.unit; + +import io.floci.cli.GlobalOptions; +import io.floci.cli.ProductProfile; +import org.junit.jupiter.api.Test; + +import java.util.Map; +import java.util.function.UnaryOperator; + +import static org.junit.jupiter.api.Assertions.*; + +class GlobalOptionsTest { + + private static UnaryOperator env(Map vars) { + return vars::get; + } + + @Test + void productDefaultsWhenEnvUnset() { + GlobalOptions gcp = new GlobalOptions(ProductProfile.GCP, env(Map.of())); + assertEquals("http://localhost:4588", gcp.endpoint); + assertEquals("floci-gcp", gcp.container); + + GlobalOptions oci = new GlobalOptions(ProductProfile.OCI, env(Map.of())); + assertEquals("http://localhost:4599", oci.endpoint); + assertEquals("floci-oci", oci.container); + } + + @Test + void envVarOverridesDefault() { + GlobalOptions az = new GlobalOptions(ProductProfile.AZ, + env(Map.of("FLOCI_AZ_ENDPOINT", "http://remote:9999", "FLOCI_AZ_CONTAINER", "custom"))); + assertEquals("http://remote:9999", az.endpoint); + assertEquals("custom", az.container); + } + + @Test + void wrongProductEnvVarIsIgnored() { + GlobalOptions gcp = new GlobalOptions(ProductProfile.GCP, + env(Map.of("FLOCI_ENDPOINT", "http://aws-only:1"))); + assertEquals("http://localhost:4588", gcp.endpoint); + } + + @Test + void emptyEnvVarCountsAsSet() { + // Matches picocli ${VAR:-fallback} semantics: only null falls back. + GlobalOptions aws = new GlobalOptions(ProductProfile.AWS, + env(Map.of("FLOCI_ENDPOINT", ""))); + assertEquals("", aws.endpoint); + } + + @Test + void noArgConstructorIsAws() { + GlobalOptions options = new GlobalOptions(); + assertEquals(ProductProfile.AWS, options.product); + // Cannot assert endpoint literal here: real env may set FLOCI_ENDPOINT. + } + + @Test + void endpointFromPortsUsesProductFallbackPortForPortlessEndpoint() { + // Endpoint URL without a port → the product's default port must be matched. + for (ProductProfile p : new ProductProfile[]{ + ProductProfile.AWS, ProductProfile.GCP, ProductProfile.AZ, ProductProfile.OCI}) { + GlobalOptions options = new GlobalOptions(p, env(Map.of())); + String result = options.endpointFromPorts( + "12345->" + p.defaultPort() + "/tcp", "http://localhost"); + assertEquals("http://localhost:12345", result, p.name()); + } + } + + @Test + void endpointFromPortsMatchesExplicitPort() { + GlobalOptions gcp = new GlobalOptions(ProductProfile.GCP, env(Map.of())); + assertEquals("http://localhost:9999", + gcp.endpointFromPorts("9999->4588/tcp", "http://localhost:4588")); + assertEquals("http://localhost:4588", + gcp.endpointFromPorts("9999->1111/tcp", "http://localhost:4588")); + } +} diff --git a/src/test/java/io/floci/cli/unit/ProductProfileTest.java b/src/test/java/io/floci/cli/unit/ProductProfileTest.java new file mode 100644 index 0000000..42efa79 --- /dev/null +++ b/src/test/java/io/floci/cli/unit/ProductProfileTest.java @@ -0,0 +1,61 @@ +package io.floci.cli.unit; + +import io.floci.cli.ProductProfile; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/** Pins every field of the four product constants — the drift firewall for the unified tree. */ +class ProductProfileTest { + + @Test + void aws() { + assertEquals("aws", ProductProfile.AWS.name()); + assertEquals("Floci AWS", ProductProfile.AWS.displayName()); + assertEquals("floci/floci", ProductProfile.AWS.image()); + assertEquals("floci/floci:latest", ProductProfile.AWS.defaultImageRef()); + assertEquals("floci", ProductProfile.AWS.defaultContainer()); + assertEquals(4566, ProductProfile.AWS.defaultPort()); + assertEquals("http://localhost:4566", ProductProfile.AWS.defaultEndpoint()); + assertEquals("FLOCI_ENDPOINT", ProductProfile.AWS.envVar("ENDPOINT")); + assertEquals("/_floci", ProductProfile.AWS.controlPrefix()); + assertEquals("floci", ProductProfile.AWS.commandPrefix()); + } + + @Test + void gcp() { + assertEquals("gcp", ProductProfile.GCP.name()); + assertEquals("Floci GCP", ProductProfile.GCP.displayName()); + assertEquals("floci/floci-gcp", ProductProfile.GCP.image()); + assertEquals("floci-gcp", ProductProfile.GCP.defaultContainer()); + assertEquals(4588, ProductProfile.GCP.defaultPort()); + assertEquals("FLOCI_GCP_CONTAINER", ProductProfile.GCP.envVar("CONTAINER")); + assertEquals("/_floci-gcp", ProductProfile.GCP.controlPrefix()); + assertEquals("floci gcp", ProductProfile.GCP.commandPrefix()); + } + + @Test + void az() { + assertEquals("az", ProductProfile.AZ.name()); + assertEquals("Floci Azure", ProductProfile.AZ.displayName()); + assertEquals("floci/floci-az", ProductProfile.AZ.image()); + assertEquals("floci-az", ProductProfile.AZ.defaultContainer()); + assertEquals(4577, ProductProfile.AZ.defaultPort()); + assertEquals("FLOCI_AZ_ENDPOINT", ProductProfile.AZ.envVar("ENDPOINT")); + // The floci-az server exposes /_floci/* (same as AWS), NOT /_floci-az. + assertEquals("/_floci", ProductProfile.AZ.controlPrefix()); + assertEquals("floci az", ProductProfile.AZ.commandPrefix()); + } + + @Test + void oci() { + assertEquals("oci", ProductProfile.OCI.name()); + assertEquals("Floci OCI", ProductProfile.OCI.displayName()); + assertEquals("floci/floci-oci", ProductProfile.OCI.image()); + assertEquals("floci-oci", ProductProfile.OCI.defaultContainer()); + assertEquals(4599, ProductProfile.OCI.defaultPort()); + assertEquals("FLOCI_OCI_ENDPOINT", ProductProfile.OCI.envVar("ENDPOINT")); + assertEquals("/_floci-oci", ProductProfile.OCI.controlPrefix()); + assertEquals("floci oci", ProductProfile.OCI.commandPrefix()); + } +} diff --git a/src/test/java/io/floci/cli/unit/ProductTreesParsingTest.java b/src/test/java/io/floci/cli/unit/ProductTreesParsingTest.java new file mode 100644 index 0000000..7889215 --- /dev/null +++ b/src/test/java/io/floci/cli/unit/ProductTreesParsingTest.java @@ -0,0 +1,122 @@ +package io.floci.cli.unit; + +import io.floci.cli.FlociCli; +import org.junit.jupiter.api.Test; +import picocli.CommandLine; +import picocli.CommandLine.Model.CommandSpec; +import picocli.CommandLine.ParseResult; + +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Characterization tests pinning the CLI surface of all four product trees. + * Written against the copied-tree implementation; they must keep passing + * unchanged through the ProductProfile unification refactor. + */ +class ProductTreesParsingTest { + + private static final Set LIFECYCLE_COMMANDS = Set.of( + "start", "stop", "restart", "status", "logs", "wait", + "version", "services", "doctor", "env", "config", "snapshot", "completion", "help"); + + private static CommandSpec leafSpec(String... args) { + ParseResult r = new CommandLine(new FlociCli()).parseArgs(args); + while (r.hasSubcommand()) r = r.subcommand(); + return r.commandSpec(); + } + + private static void assertDefaults(String expectedEndpoint, String expectedContainer, String... args) { + CommandSpec spec = leafSpec(args); + assertEquals(expectedEndpoint, spec.findOption("--endpoint").getValue()); + assertEquals(expectedContainer, spec.findOption("--container").getValue()); + } + + private static void assertStartDefaults(String port, String image, String... args) { + CommandSpec spec = leafSpec(args); + assertEquals(Integer.valueOf(port), spec.findOption("--port").getValue()); + assertEquals(image, spec.findOption("--image").getValue()); + } + + @Test + void awsTreeDefaults() { + assertDefaults("http://localhost:4566", "floci", "status"); + assertDefaults("http://localhost:4566", "floci", "aws", "status"); + assertStartDefaults("4566", "floci/floci:latest", "start"); + assertStartDefaults("4566", "floci/floci:latest", "aws", "start"); + } + + @Test + void gcpTreeDefaults() { + assertDefaults("http://localhost:4588", "floci-gcp", "gcp", "status"); + assertStartDefaults("4588", "floci/floci-gcp:latest", "gcp", "start"); + } + + @Test + void azTreeDefaults() { + assertDefaults("http://localhost:4577", "floci-az", "az", "status"); + assertStartDefaults("4577", "floci/floci-az:latest", "az", "start"); + } + + @Test + void ociTreeDefaults() { + assertDefaults("http://localhost:4599", "floci-oci", "oci", "status"); + assertStartDefaults("4599", "floci/floci-oci:latest", "oci", "start"); + } + + @Test + void endpointFlagOverridesDefaultInEveryTree() { + for (String tree : new String[]{"aws", "gcp", "az", "oci"}) { + CommandSpec spec = leafSpec(tree, "status", "--endpoint", "http://x:1"); + assertEquals("http://x:1", spec.findOption("--endpoint").getValue(), tree); + } + } + + @Test + void everyProductGroupExposesTheFullCommandSet() { + CommandLine root = new CommandLine(new FlociCli()); + for (String tree : new String[]{"aws", "gcp", "az", "oci"}) { + Set names = root.getSubcommands().get(tree).getSubcommands().keySet(); + assertTrue(names.containsAll(LIFECYCLE_COMMANDS), + tree + " is missing " + LIFECYCLE_COMMANDS.stream().filter(c -> !names.contains(c)).toList()); + } + // oci additionally has setup; bare root additionally has update + the product groups + assertTrue(root.getSubcommands().get("oci").getSubcommands().containsKey("setup")); + assertTrue(root.getSubcommands().keySet().containsAll(Set.of("update", "aws", "gcp", "az", "oci"))); + } + + @Test + void productConfigGroupsHaveNoDefaultProduct() { + CommandLine root = new CommandLine(new FlociCli()); + assertTrue(root.getSubcommands().get("config").getSubcommands().containsKey("default-product")); + for (String tree : new String[]{"gcp", "az", "oci"}) { + Set configSubs = root.getSubcommands().get(tree) + .getSubcommands().get("config").getSubcommands().keySet(); + assertTrue(configSubs.containsAll(Set.of("show", "validate", "profile")), tree); + assertFalse(configSubs.contains("default-product"), tree); + } + } + + @Test + void snapshotGroupsExposeAllLeaves() { + CommandLine root = new CommandLine(new FlociCli()); + Set expected = Set.of("save", "load", "list", "delete", "export", "import"); + assertTrue(root.getSubcommands().get("snapshot").getSubcommands().keySet().containsAll(expected)); + for (String tree : new String[]{"gcp", "az", "oci"}) { + assertTrue(root.getSubcommands().get(tree) + .getSubcommands().get("snapshot").getSubcommands().keySet().containsAll(expected), tree); + } + } + + @Test + void sharedFlagsParseInEveryTree() { + for (String tree : new String[]{"aws", "gcp", "az", "oci"}) { + CommandSpec spec = leafSpec(tree, "status", "-o", "json", "-q", "--no-color", "-v"); + assertEquals("json", spec.findOption("--output").getValue().toString()); + assertEquals(Boolean.TRUE, spec.findOption("--quiet").getValue()); + assertEquals(Boolean.TRUE, spec.findOption("--no-color").getValue()); + assertEquals(Boolean.TRUE, spec.findOption("--verbose").getValue()); + } + } +} diff --git a/src/test/java/io/floci/cli/unit/WaitCommandParseTest.java b/src/test/java/io/floci/cli/unit/WaitCommandParseTest.java index ce5e299..bce7da3 100644 --- a/src/test/java/io/floci/cli/unit/WaitCommandParseTest.java +++ b/src/test/java/io/floci/cli/unit/WaitCommandParseTest.java @@ -1,6 +1,6 @@ package io.floci.cli.unit; -import io.floci.cli.commands.WaitCommand; +import io.floci.cli.util.Durations; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; @@ -18,11 +18,11 @@ class WaitCommandParseTest { "10, 10000" }) void testDurationParsing(String input, long expectedMillis) { - assertEquals(expectedMillis, WaitCommand.parseDuration(input.trim())); + assertEquals(expectedMillis, Durations.parseDuration(input.trim())); } @Test void testNullDuration() { - assertEquals(30_000, WaitCommand.parseDuration(null)); + assertEquals(30_000, Durations.parseDuration(null)); } }