feat: add CLI env registry and release verification
This commit is contained in:
@@ -23,10 +23,18 @@ public final class Eip4Cli {
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
private final Eip4HttpClient httpClient;
|
||||
private final Eip4EnvStore envStore;
|
||||
|
||||
public Eip4Cli(Eip4HttpClient httpClient) {
|
||||
this.objectMapper = new ObjectMapper();
|
||||
this.httpClient = httpClient;
|
||||
this.envStore = Eip4EnvStore.defaultStore(objectMapper);
|
||||
}
|
||||
|
||||
Eip4Cli(Eip4HttpClient httpClient, Eip4EnvStore envStore) {
|
||||
this.objectMapper = new ObjectMapper();
|
||||
this.httpClient = httpClient;
|
||||
this.envStore = envStore;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
@@ -46,6 +54,7 @@ public final class Eip4Cli {
|
||||
return switch (args[0]) {
|
||||
case "env" -> runEnv(args, out, err);
|
||||
case "ai" -> runAi(args, out, err);
|
||||
case "release" -> runRelease(args, out, err);
|
||||
default -> fail(err, "Unknown command: " + args[0]);
|
||||
};
|
||||
} catch (IllegalArgumentException ex) {
|
||||
@@ -63,20 +72,186 @@ public final class Eip4Cli {
|
||||
printEnvHelp(out);
|
||||
return 0;
|
||||
}
|
||||
if (!"status".equals(args[1])) {
|
||||
return fail(err, "Unknown env command: " + args[1]);
|
||||
return switch (args[1]) {
|
||||
case "add" -> runEnvAdd(args, out);
|
||||
case "list" -> runEnvList(args, out);
|
||||
case "use" -> runEnvUse(args, out);
|
||||
case "current" -> runEnvCurrent(out);
|
||||
case "status" -> runEnvStatus(args, out, err);
|
||||
default -> fail(err, "Unknown env command: " + args[1]);
|
||||
};
|
||||
}
|
||||
|
||||
private int runRelease(String[] args, PrintStream out, PrintStream err) throws IOException {
|
||||
if (args.length < 2 || isHelp(args[1])) {
|
||||
printReleaseHelp(out);
|
||||
return 0;
|
||||
}
|
||||
return switch (args[1]) {
|
||||
case "verify" -> runReleaseVerify(args, out, err);
|
||||
default -> fail(err, "Unknown release command: " + args[1]);
|
||||
};
|
||||
}
|
||||
|
||||
private int runReleaseVerify(String[] args, PrintStream out, PrintStream err) throws IOException {
|
||||
Options options = Options.parse(args, 2);
|
||||
String baseUrl = baseUrl(options);
|
||||
Path packageFile = Path.of(requiredPart(options.single("package-file"), "package-file"));
|
||||
JsonNode root = objectMapper.readTree(Files.readString(packageFile, StandardCharsets.UTF_8));
|
||||
JsonNode deploymentPackage = root.has("deploymentPackage") ? root.path("deploymentPackage") : root;
|
||||
List<String> blockers = new ArrayList<>();
|
||||
List<String> warnings = new ArrayList<>();
|
||||
|
||||
if (!"READY_FOR_HUMAN_REVIEW".equals(text(deploymentPackage, "status"))) {
|
||||
blockers.add("deploymentPackage.status 必须是 READY_FOR_HUMAN_REVIEW");
|
||||
}
|
||||
if (!deploymentPackage.path("requiresHumanApproval").asBoolean(false)) {
|
||||
blockers.add("deploymentPackage.requiresHumanApproval 必须是 true");
|
||||
}
|
||||
if (!hasTextArrayItems(deploymentPackage.path("sqlStatements"))) {
|
||||
blockers.add("sqlStatements 不能为空");
|
||||
}
|
||||
if (!hasTextArrayItems(deploymentPackage.path("rollbackSqlStatements"))) {
|
||||
blockers.add("rollbackSqlStatements 不能为空");
|
||||
}
|
||||
if (!hasRunEvidence(deploymentPackage.path("runEvidence"), "LOCAL_APPLY", "PASSED")) {
|
||||
blockers.add("缺少通过的 LOCAL_APPLY evidence");
|
||||
}
|
||||
if (!hasRunEvidence(deploymentPackage.path("runEvidence"), "SMOKE", "PASSED")) {
|
||||
blockers.add("缺少通过的 SMOKE evidence");
|
||||
}
|
||||
if (containsPlaceholder(deploymentPackage.path("sqlStatements"))) {
|
||||
blockers.add("sqlStatements 仍包含 ${...} 占位符,必须人工替换后再发布");
|
||||
}
|
||||
deploymentPackage.path("warnings").forEach(warning -> warnings.add(warning.asText()));
|
||||
|
||||
ObjectNode report = objectMapper.createObjectNode();
|
||||
report.put("status", blockers.isEmpty() ? "PASSED" : "FAILED");
|
||||
report.put("packageId", packageId(root, deploymentPackage));
|
||||
report.put("taskId", text(deploymentPackage, "taskId"));
|
||||
report.put("changeSetId", text(deploymentPackage, "changeSetId"));
|
||||
ArrayNode blockerNodes = report.putArray("blockers");
|
||||
blockers.forEach(blockerNodes::add);
|
||||
ArrayNode warningNodes = report.putArray("warnings");
|
||||
warnings.forEach(warningNodes::add);
|
||||
|
||||
if (options.has("json-output")) {
|
||||
out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(report));
|
||||
} else if (blockers.isEmpty()) {
|
||||
out.println("Release package PASSED");
|
||||
out.println("packageId: " + packageId(root, deploymentPackage));
|
||||
if (!warnings.isEmpty()) {
|
||||
out.println("warnings:");
|
||||
warnings.forEach(warning -> out.println("- " + warning));
|
||||
}
|
||||
} else {
|
||||
err.println("Release package FAILED");
|
||||
blockers.forEach(blocker -> err.println("- " + blocker));
|
||||
}
|
||||
return blockers.isEmpty() ? 0 : 2;
|
||||
}
|
||||
|
||||
private int runEnvAdd(String[] args, PrintStream out) throws IOException {
|
||||
Options options = Options.parse(args, 2);
|
||||
String name = requiredPart(options.single("name"), "name");
|
||||
String baseUrl = Eip4EnvStore.normalizeBaseUrl(requiredPart(options.single("base-url"), "base-url"));
|
||||
String kind = options.single("kind") == null ? "local" : options.single("kind");
|
||||
Eip4EnvStore.EnvConfig env = Eip4EnvStore.normalize(new Eip4EnvStore.EnvConfig(name, kind, baseUrl));
|
||||
envStore.saveEnv(env, options.has("use"));
|
||||
out.println("Saved env: " + env.name());
|
||||
out.println("baseUrl: " + env.baseUrl());
|
||||
if (options.has("use")) {
|
||||
out.println("currentEnv: " + env.name());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private int runEnvList(String[] args, PrintStream out) throws IOException {
|
||||
Options options = Options.parse(args, 2);
|
||||
List<Eip4EnvStore.EnvConfig> envs = envStore.listEnvs();
|
||||
String current = envStore.currentEnvName();
|
||||
if (options.has("json-output")) {
|
||||
ObjectNode root = objectMapper.createObjectNode();
|
||||
root.put("currentEnv", current);
|
||||
ArrayNode array = root.putArray("envs");
|
||||
for (Eip4EnvStore.EnvConfig env : envs) {
|
||||
ObjectNode row = array.addObject();
|
||||
row.put("name", env.name());
|
||||
row.put("kind", env.kind());
|
||||
row.put("baseUrl", env.baseUrl());
|
||||
row.put("current", env.name().equals(current));
|
||||
}
|
||||
out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(root));
|
||||
return 0;
|
||||
}
|
||||
if (envs.isEmpty()) {
|
||||
out.println("No EIP4 envs configured. Add one with `eip4 env add --name local --base-url http://localhost:8084 --use`.");
|
||||
return 0;
|
||||
}
|
||||
out.println("CURRENT NAME KIND BASE_URL");
|
||||
for (Eip4EnvStore.EnvConfig env : envs) {
|
||||
out.println((env.name().equals(current) ? "*" : "-") + " "
|
||||
+ env.name() + " " + env.kind() + " " + env.baseUrl());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private int runEnvUse(String[] args, PrintStream out) throws IOException {
|
||||
Options options = Options.parse(args, 2);
|
||||
String name = requiredPart(options.single("name"), "name");
|
||||
Eip4EnvStore.EnvConfig env = envStore.useEnv(name);
|
||||
out.println("currentEnv: " + env.name());
|
||||
out.println("baseUrl: " + env.baseUrl());
|
||||
return 0;
|
||||
}
|
||||
|
||||
private int runEnvCurrent(PrintStream out) throws IOException {
|
||||
Eip4EnvStore.EnvConfig env = envStore.currentEnv().orElse(null);
|
||||
if (env == null) {
|
||||
out.println("No current EIP4 env configured.");
|
||||
return 0;
|
||||
}
|
||||
out.println("currentEnv: " + env.name());
|
||||
out.println("kind: " + env.kind());
|
||||
out.println("baseUrl: " + env.baseUrl());
|
||||
return 0;
|
||||
}
|
||||
|
||||
private int runEnvStatus(String[] args, PrintStream out, PrintStream err) throws IOException, InterruptedException {
|
||||
Options options = Options.parse(args, 2);
|
||||
if (options.has("all")) {
|
||||
int exit = 0;
|
||||
for (Eip4EnvStore.EnvConfig env : envStore.listEnvs()) {
|
||||
int code = checkEnvStatus(env.name(), env.baseUrl(), out, err);
|
||||
if (code != 0) {
|
||||
exit = code;
|
||||
}
|
||||
}
|
||||
if (envStore.listEnvs().isEmpty()) {
|
||||
out.println("No EIP4 envs configured.");
|
||||
}
|
||||
return exit;
|
||||
}
|
||||
CliTarget target = target(options);
|
||||
return checkEnvStatus(target.envName(), target.baseUrl(), out, err);
|
||||
}
|
||||
|
||||
private int checkEnvStatus(String envName, String baseUrl, PrintStream out, PrintStream err)
|
||||
throws IOException, InterruptedException {
|
||||
URI uri = endpoint(baseUrl, "/v3/api-docs");
|
||||
Eip4HttpResponse response = httpClient.send("GET", uri, null);
|
||||
if (!response.successful()) {
|
||||
err.println("EIP4 env NOT_READY");
|
||||
if (!envName.isBlank()) {
|
||||
err.println("env: " + envName);
|
||||
}
|
||||
err.println("baseUrl: " + baseUrl);
|
||||
err.println("status: " + response.statusCode());
|
||||
return 2;
|
||||
}
|
||||
out.println("EIP4 env READY");
|
||||
if (!envName.isBlank()) {
|
||||
out.println("env: " + envName);
|
||||
}
|
||||
out.println("baseUrl: " + baseUrl);
|
||||
out.println("apiDocs: " + uri);
|
||||
return 0;
|
||||
@@ -98,6 +273,7 @@ public final class Eip4Cli {
|
||||
private int runAiAutoDevelop(String[] args, PrintStream out, PrintStream err)
|
||||
throws IOException, InterruptedException {
|
||||
Options options = Options.parse(args, 2);
|
||||
ensureCrossEnvConfirmed(options);
|
||||
ObjectNode request = buildAutoDevelopRequest(options);
|
||||
String baseUrl = baseUrl(options);
|
||||
Eip4HttpResponse response = httpClient.send(
|
||||
@@ -110,6 +286,7 @@ public final class Eip4Cli {
|
||||
private int runAiFeaturePackage(String[] args, PrintStream out, PrintStream err)
|
||||
throws IOException, InterruptedException {
|
||||
Options options = Options.parse(args, 2);
|
||||
ensureCrossEnvConfirmed(options);
|
||||
String taskId = requiredPart(options.single("task-id"), "task-id");
|
||||
String changeSetId = requiredPart(options.single("change-set-id"), "change-set-id");
|
||||
Eip4HttpResponse response = httpClient.send(
|
||||
@@ -123,6 +300,7 @@ public final class Eip4Cli {
|
||||
private int runAiRevision(String[] args, PrintStream out, PrintStream err)
|
||||
throws IOException, InterruptedException {
|
||||
Options options = Options.parse(args, 2);
|
||||
ensureCrossEnvConfirmed(options);
|
||||
String taskId = requiredPart(options.single("task-id"), "task-id");
|
||||
String packageId = requiredPart(options.single("package-id"), "package-id");
|
||||
ObjectNode request = objectMapper.createObjectNode();
|
||||
@@ -167,7 +345,7 @@ public final class Eip4Cli {
|
||||
return 0;
|
||||
}
|
||||
|
||||
private ObjectNode buildAutoDevelopRequest(Options options) {
|
||||
private ObjectNode buildAutoDevelopRequest(Options options) throws IOException {
|
||||
ObjectNode root = objectMapper.createObjectNode();
|
||||
String baseUrl = baseUrl(options);
|
||||
root.put("baseUrl", baseUrl);
|
||||
@@ -220,18 +398,42 @@ public final class Eip4Cli {
|
||||
node.put(jsonName, requiredPart(options.single(optionName), optionName));
|
||||
}
|
||||
|
||||
private String baseUrl(Options options) {
|
||||
String value = options.single("base-url");
|
||||
if (value == null || value.isBlank()) {
|
||||
value = System.getenv("EIP4_BASE_URL");
|
||||
private String baseUrl(Options options) throws IOException {
|
||||
return target(options).baseUrl();
|
||||
}
|
||||
|
||||
private CliTarget target(Options options) throws IOException {
|
||||
String explicitBaseUrl = options.single("base-url");
|
||||
if (explicitBaseUrl != null && !explicitBaseUrl.isBlank()) {
|
||||
return new CliTarget("", Eip4EnvStore.normalizeBaseUrl(explicitBaseUrl));
|
||||
}
|
||||
String envName = options.single("env");
|
||||
if (envName != null && !envName.isBlank()) {
|
||||
Eip4EnvStore.EnvConfig env = envStore.findEnv(envName)
|
||||
.orElseThrow(() -> new IllegalArgumentException("EIP4 env not found: " + envName));
|
||||
return new CliTarget(env.name(), env.baseUrl());
|
||||
}
|
||||
Eip4EnvStore.EnvConfig current = envStore.currentEnv().orElse(null);
|
||||
if (current != null) {
|
||||
return new CliTarget(current.name(), current.baseUrl());
|
||||
}
|
||||
String value = System.getenv("EIP4_BASE_URL");
|
||||
if (value == null || value.isBlank()) {
|
||||
value = DEFAULT_BASE_URL;
|
||||
}
|
||||
while (value.endsWith("/")) {
|
||||
value = value.substring(0, value.length() - 1);
|
||||
return new CliTarget("", Eip4EnvStore.normalizeBaseUrl(value));
|
||||
}
|
||||
|
||||
private void ensureCrossEnvConfirmed(Options options) throws IOException {
|
||||
String envName = options.single("env");
|
||||
if (envName == null || envName.isBlank() || options.has("yes")) {
|
||||
return;
|
||||
}
|
||||
String current = envStore.currentEnvName();
|
||||
if (!current.isBlank() && !current.equals(envName.trim())) {
|
||||
throw new IllegalArgumentException("Refusing to target env `" + envName
|
||||
+ "` while current env is `" + current + "`. Re-run with --yes to confirm.");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private URI endpoint(String baseUrl, String path) {
|
||||
@@ -265,6 +467,43 @@ public final class Eip4Cli {
|
||||
return value.isMissingNode() || value.isNull() ? "" : value.asText();
|
||||
}
|
||||
|
||||
private boolean hasTextArrayItems(JsonNode node) {
|
||||
return node.isArray() && node.size() > 0 && node.elements().hasNext();
|
||||
}
|
||||
|
||||
private boolean containsPlaceholder(JsonNode node) {
|
||||
if (!node.isArray()) {
|
||||
return false;
|
||||
}
|
||||
for (JsonNode item : node) {
|
||||
if (item.asText("").contains("${")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean hasRunEvidence(JsonNode runEvidence, String runType, String status) {
|
||||
if (!runEvidence.isArray()) {
|
||||
return false;
|
||||
}
|
||||
for (JsonNode run : runEvidence) {
|
||||
if (runType.equals(run.path("runType").asText()) && status.equals(run.path("status").asText())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private String packageId(JsonNode root, JsonNode deploymentPackage) {
|
||||
String id = text(root, "id");
|
||||
if (!id.isBlank()) {
|
||||
return id;
|
||||
}
|
||||
String manifestId = deploymentPackage.path("manifest").path("packageId").asText("");
|
||||
return manifestId == null ? "" : manifestId;
|
||||
}
|
||||
|
||||
private int parseInt(String value, String name) {
|
||||
try {
|
||||
return Integer.parseInt(value);
|
||||
@@ -295,18 +534,35 @@ public final class Eip4Cli {
|
||||
EIP4 CLI
|
||||
|
||||
Usage:
|
||||
eip4 env status [--base-url URL]
|
||||
eip4 env add --name NAME --base-url URL [--kind local|http] [--use]
|
||||
eip4 env list [--json-output]
|
||||
eip4 env use --name NAME
|
||||
eip4 env current
|
||||
eip4 env status [--env NAME|--base-url URL|--all]
|
||||
eip4 ai auto-develop --objective TEXT --feature-name NAME --table-name TABLE --entity-name NAME --template-id ID --menu-name NAME --field column:label:type[:length][:required]
|
||||
eip4 release verify --package-file FILE
|
||||
|
||||
Environment:
|
||||
EIP4_BASE_URL defaults the target service URL when --base-url is omitted.
|
||||
Configured envs are stored in ~/.eip4/eip4-cli.json by default.
|
||||
EIP4_CLI_HOME overrides the CLI home directory.
|
||||
""");
|
||||
}
|
||||
|
||||
private void printReleaseHelp(PrintStream out) {
|
||||
out.println("""
|
||||
Usage:
|
||||
eip4 release verify --package-file FILE [--json-output]
|
||||
""");
|
||||
}
|
||||
|
||||
private void printEnvHelp(PrintStream out) {
|
||||
out.println("""
|
||||
Usage:
|
||||
eip4 env status [--base-url URL]
|
||||
eip4 env add --name NAME --base-url URL [--kind local|http] [--use]
|
||||
eip4 env list [--json-output]
|
||||
eip4 env use --name NAME
|
||||
eip4 env current
|
||||
eip4 env status [--env NAME|--base-url URL|--all]
|
||||
""");
|
||||
}
|
||||
|
||||
@@ -335,15 +591,22 @@ public final class Eip4Cli {
|
||||
--package-id ID
|
||||
|
||||
Optional:
|
||||
--env NAME
|
||||
--base-url URL
|
||||
--max-repair-attempts N
|
||||
--no-deployment-package
|
||||
--yes
|
||||
--output FILE
|
||||
""");
|
||||
}
|
||||
|
||||
private record CliTarget(String envName, String baseUrl) {
|
||||
}
|
||||
|
||||
private static final class Options {
|
||||
private final Map<String, List<String>> values = new LinkedHashMap<>();
|
||||
private static final List<String> BOOLEAN_OPTIONS = List.of(
|
||||
"all", "json-output", "no-deployment-package", "use", "yes");
|
||||
|
||||
static Options parse(String[] args, int from) {
|
||||
Options options = new Options();
|
||||
@@ -356,7 +619,7 @@ public final class Eip4Cli {
|
||||
if (name.isBlank()) {
|
||||
throw new IllegalArgumentException("Invalid option: " + arg);
|
||||
}
|
||||
if ("no-deployment-package".equals(name)) {
|
||||
if (BOOLEAN_OPTIONS.contains(name)) {
|
||||
options.add(name, "true");
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
package com.eip4.platform.cli;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
final class Eip4EnvStore {
|
||||
|
||||
private static final int SCHEMA_VERSION = 1;
|
||||
|
||||
private final Path configPath;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
Eip4EnvStore(Path configPath, ObjectMapper objectMapper) {
|
||||
this.configPath = configPath;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
static Eip4EnvStore defaultStore(ObjectMapper objectMapper) {
|
||||
String cliHome = System.getenv("EIP4_CLI_HOME");
|
||||
Path home = cliHome == null || cliHome.isBlank()
|
||||
? Path.of(System.getProperty("user.home"), ".eip4")
|
||||
: Path.of(cliHome);
|
||||
return new Eip4EnvStore(home.resolve("eip4-cli.json"), objectMapper);
|
||||
}
|
||||
|
||||
Config load() throws IOException {
|
||||
if (!Files.exists(configPath)) {
|
||||
return new Config(SCHEMA_VERSION, "", new LinkedHashMap<>());
|
||||
}
|
||||
String json = Files.readString(configPath, StandardCharsets.UTF_8);
|
||||
if (json.isBlank()) {
|
||||
return new Config(SCHEMA_VERSION, "", new LinkedHashMap<>());
|
||||
}
|
||||
Config config = objectMapper.readValue(json, Config.class);
|
||||
return normalize(config);
|
||||
}
|
||||
|
||||
void saveEnv(EnvConfig env, boolean use) throws IOException {
|
||||
Config config = load();
|
||||
Map<String, EnvConfig> envs = new LinkedHashMap<>(config.envs());
|
||||
envs.put(env.name(), env);
|
||||
String current = use || config.currentEnv().isBlank() ? env.name() : config.currentEnv();
|
||||
save(new Config(SCHEMA_VERSION, current, envs));
|
||||
}
|
||||
|
||||
List<EnvConfig> listEnvs() throws IOException {
|
||||
return List.copyOf(load().envs().values());
|
||||
}
|
||||
|
||||
Optional<EnvConfig> findEnv(String name) throws IOException {
|
||||
if (name == null || name.isBlank()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.ofNullable(load().envs().get(name.trim()));
|
||||
}
|
||||
|
||||
Optional<EnvConfig> currentEnv() throws IOException {
|
||||
Config config = load();
|
||||
if (config.currentEnv().isBlank()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.ofNullable(config.envs().get(config.currentEnv()));
|
||||
}
|
||||
|
||||
String currentEnvName() throws IOException {
|
||||
return load().currentEnv();
|
||||
}
|
||||
|
||||
EnvConfig useEnv(String name) throws IOException {
|
||||
String requested = name == null ? "" : name.trim();
|
||||
Config config = load();
|
||||
EnvConfig env = config.envs().get(requested);
|
||||
if (env == null) {
|
||||
throw new IllegalArgumentException("EIP4 env not found: " + requested);
|
||||
}
|
||||
save(new Config(SCHEMA_VERSION, requested, new LinkedHashMap<>(config.envs())));
|
||||
return env;
|
||||
}
|
||||
|
||||
private void save(Config config) throws IOException {
|
||||
if (configPath.getParent() != null) {
|
||||
Files.createDirectories(configPath.getParent());
|
||||
}
|
||||
String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(normalize(config));
|
||||
Files.writeString(configPath, json + System.lineSeparator(), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private static Config normalize(Config config) {
|
||||
Map<String, EnvConfig> envs = new LinkedHashMap<>();
|
||||
if (config != null && config.envs() != null) {
|
||||
for (EnvConfig env : config.envs().values()) {
|
||||
if (env == null || env.name() == null || env.name().isBlank()) {
|
||||
continue;
|
||||
}
|
||||
envs.put(env.name(), normalize(env));
|
||||
}
|
||||
}
|
||||
String current = config == null || config.currentEnv() == null ? "" : config.currentEnv().trim();
|
||||
if (!current.isBlank() && !envs.containsKey(current)) {
|
||||
current = "";
|
||||
}
|
||||
return new Config(SCHEMA_VERSION, current, envs);
|
||||
}
|
||||
|
||||
static EnvConfig normalize(EnvConfig env) {
|
||||
String name = required(env.name(), "name");
|
||||
String kind = env.kind() == null || env.kind().isBlank() ? "local" : env.kind().trim();
|
||||
String baseUrl = normalizeBaseUrl(required(env.baseUrl(), "baseUrl"));
|
||||
return new EnvConfig(name, kind, baseUrl);
|
||||
}
|
||||
|
||||
static String normalizeBaseUrl(String value) {
|
||||
String out = value == null ? "" : value.trim();
|
||||
while (out.endsWith("/")) {
|
||||
out = out.substring(0, out.length() - 1);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private static String required(String value, String name) {
|
||||
if (value == null || value.isBlank()) {
|
||||
throw new IllegalArgumentException("Missing required env " + name);
|
||||
}
|
||||
return value.trim();
|
||||
}
|
||||
|
||||
record Config(int schemaVersion, String currentEnv, Map<String, EnvConfig> envs) {
|
||||
}
|
||||
|
||||
record EnvConfig(String name, String kind, String baseUrl) {
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,30 @@ class Eip4CliTest {
|
||||
assertTrue(result.out().contains("EIP4 env READY"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void envCommandsPersistAndUseCurrentEnv() {
|
||||
CapturingClient client = new CapturingClient(new Eip4HttpResponse(200, "{}"));
|
||||
Eip4EnvStore store = tempStore();
|
||||
|
||||
CliResult add = run(store, client, "env", "add",
|
||||
"--name", "local",
|
||||
"--base-url", "http://localhost:8084/",
|
||||
"--kind", "local",
|
||||
"--use");
|
||||
CliResult current = run(store, client, "env", "current");
|
||||
CliResult list = run(store, client, "env", "list");
|
||||
CliResult status = run(store, client, "env", "status");
|
||||
|
||||
assertEquals(0, add.code());
|
||||
assertEquals(0, current.code());
|
||||
assertEquals(0, list.code());
|
||||
assertEquals(0, status.code());
|
||||
assertTrue(current.out().contains("currentEnv: local"));
|
||||
assertTrue(list.out().contains("* local local http://localhost:8084"));
|
||||
assertEquals(URI.create("http://localhost:8084/v3/api-docs"), client.uri);
|
||||
assertTrue(status.out().contains("env: local"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void autoDevelopBuildsRequestAndWritesResponse() throws Exception {
|
||||
String response = """
|
||||
@@ -79,6 +103,47 @@ class Eip4CliTest {
|
||||
assertTrue(result.out().contains("applyStatus: PASSED"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void autoDevelopCanUseConfiguredEnv() {
|
||||
CapturingClient client = new CapturingClient(new Eip4HttpResponse(200, """
|
||||
{"status":"READY_FOR_HUMAN_REVIEW","taskId":"task-1","changeSetId":"cs-1"}
|
||||
"""));
|
||||
Eip4EnvStore store = tempStore();
|
||||
run(store, client, "env", "add", "--name", "dev", "--base-url", "http://localhost:8093", "--use");
|
||||
|
||||
CliResult result = run(store, client,
|
||||
"ai", "auto-develop",
|
||||
"--env", "dev",
|
||||
"--objective", "新增客户等级维护",
|
||||
"--feature-name", "客户等级维护",
|
||||
"--table-name", "EIP4AI_CUSTOMER_LEVEL",
|
||||
"--entity-name", "客户等级",
|
||||
"--template-id", "eip4ai-customer-level",
|
||||
"--menu-name", "客户等级维护",
|
||||
"--field", "NAME:名称:string:100:true");
|
||||
|
||||
assertEquals(0, result.code());
|
||||
assertEquals(URI.create("http://localhost:8093/api/ai/features/auto-develop"), client.uri);
|
||||
assertEquals("http://localhost:8093", client.body.path("baseUrl").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void explicitCrossEnvRequiresYes() {
|
||||
CapturingClient client = new CapturingClient(new Eip4HttpResponse(200, "{}"));
|
||||
Eip4EnvStore store = tempStore();
|
||||
run(store, client, "env", "add", "--name", "local", "--base-url", "http://localhost:8084", "--use");
|
||||
run(store, client, "env", "add", "--name", "dev", "--base-url", "http://localhost:8093");
|
||||
|
||||
CliResult result = run(store, client,
|
||||
"ai", "feature-package",
|
||||
"--env", "dev",
|
||||
"--task-id", "ait-1",
|
||||
"--change-set-id", "aics-1");
|
||||
|
||||
assertEquals(1, result.code());
|
||||
assertTrue(result.err().contains("Refusing to target env `dev`"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void autoDevelopRequiresAtLeastOneField() {
|
||||
CapturingClient client = new CapturingClient(new Eip4HttpResponse(200, "{}"));
|
||||
@@ -143,10 +208,74 @@ class Eip4CliTest {
|
||||
assertTrue(result.out().contains("packageId: aipkg-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void releaseVerifyPassesReadyFeaturePackage() throws Exception {
|
||||
Path file = tempDir.resolve("package.json");
|
||||
Files.writeString(file, """
|
||||
{
|
||||
"id": "aipkg-1",
|
||||
"deploymentPackage": {
|
||||
"taskId": "ait-1",
|
||||
"changeSetId": "aics-1",
|
||||
"status": "READY_FOR_HUMAN_REVIEW",
|
||||
"requiresHumanApproval": true,
|
||||
"sqlStatements": ["insert into sys_page_schema values ('x')"],
|
||||
"rollbackSqlStatements": ["delete from sys_page_schema where id = 'x'"],
|
||||
"warnings": [],
|
||||
"runEvidence": [
|
||||
{"runType": "LOCAL_APPLY", "status": "PASSED"},
|
||||
{"runType": "SMOKE", "status": "PASSED"}
|
||||
]
|
||||
}
|
||||
}
|
||||
""", StandardCharsets.UTF_8);
|
||||
|
||||
CliResult result = run(new CapturingClient(new Eip4HttpResponse(200, "{}")),
|
||||
"release", "verify", "--package-file", file.toString());
|
||||
|
||||
assertEquals(0, result.code());
|
||||
assertTrue(result.out().contains("Release package PASSED"));
|
||||
assertTrue(result.out().contains("packageId: aipkg-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void releaseVerifyBlocksSqlPlaceholders() throws Exception {
|
||||
Path file = tempDir.resolve("package-with-placeholder.json");
|
||||
Files.writeString(file, """
|
||||
{
|
||||
"deploymentPackage": {
|
||||
"taskId": "ait-1",
|
||||
"changeSetId": "aics-1",
|
||||
"status": "READY_FOR_HUMAN_REVIEW",
|
||||
"requiresHumanApproval": true,
|
||||
"manifest": {"packageId": "aipkg-2"},
|
||||
"sqlStatements": ["insert into sys_menu(parent_id) values ('${parentMenuId}')"],
|
||||
"rollbackSqlStatements": ["delete from sys_menu where id = 'x'"],
|
||||
"warnings": ["菜单 parentId 待替换"],
|
||||
"runEvidence": [
|
||||
{"runType": "LOCAL_APPLY", "status": "PASSED"},
|
||||
{"runType": "SMOKE", "status": "PASSED"}
|
||||
]
|
||||
}
|
||||
}
|
||||
""", StandardCharsets.UTF_8);
|
||||
|
||||
CliResult result = run(new CapturingClient(new Eip4HttpResponse(200, "{}")),
|
||||
"release", "verify", "--package-file", file.toString());
|
||||
|
||||
assertEquals(2, result.code());
|
||||
assertTrue(result.err().contains("Release package FAILED"));
|
||||
assertTrue(result.err().contains("占位符"));
|
||||
}
|
||||
|
||||
private CliResult run(CapturingClient client, String... args) {
|
||||
return run(tempStore(), client, args);
|
||||
}
|
||||
|
||||
private CliResult run(Eip4EnvStore store, CapturingClient client, String... args) {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ByteArrayOutputStream err = new ByteArrayOutputStream();
|
||||
int code = new Eip4Cli(client).run(
|
||||
int code = new Eip4Cli(client, store).run(
|
||||
args,
|
||||
new PrintStream(out, true, StandardCharsets.UTF_8),
|
||||
new PrintStream(err, true, StandardCharsets.UTF_8));
|
||||
@@ -156,6 +285,10 @@ class Eip4CliTest {
|
||||
err.toString(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private Eip4EnvStore tempStore() {
|
||||
return new Eip4EnvStore(tempDir.resolve("eip4-cli.json"), objectMapper);
|
||||
}
|
||||
|
||||
private final class CapturingClient implements Eip4HttpClient {
|
||||
private final Eip4HttpResponse response;
|
||||
private int calls;
|
||||
|
||||
Reference in New Issue
Block a user