@stalfh233/omc-cli 0.6.32 → 0.6.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -63,6 +63,7 @@ import { ENDPOINTS } from "./endpoints.js";
63
63
  import { makeToolContext } from "./context.js";
64
64
  import { cmdRollbackList, cmdRollbackPlan, cmdRollbackApply } from "./rollback.js";
65
65
  import { SessionManager } from "./session-manager.js";
66
+ import { exportEnvironment, fieldValue, formatEnv } from "./env-export.js";
66
67
  async function ask(question) {
67
68
  const rl = createInterface({ input: stdin, output: process.stderr });
68
69
  try {
@@ -2183,10 +2184,53 @@ async function main() {
2183
2184
  nextCommand: "omc setup --list",
2184
2185
  }));
2185
2186
  });
2186
- command.command("env [action] [name]").option("--json").action(async (action, name, options) => {
2187
+ command.command("env [action] [name]").description("环境清单与联调参数包;`env export` 一键导出 token/headers,免手工从浏览器复制 token").option("--json").option("--env <environment>", "环境名;按与 call 相同的项目范围门解析").option("--format <format>", "export:输出可 source 的 bash 环境变量语句").option("--field <field>", "输出单个裸值:accessToken|baseUrl|enterpriseId|engineCode").option("--example <example>", "追加一个复制即测示例:curl|node;path 换成你的接口").addHelpText("after", "\n`omc env export` 用于二开联调参数包;`--example curl|node` 追加复制即测示例(把 `<your-api-path>` 换成你的接口),默认 JSON 不带示例。`--format env` 可 source,`--field accessToken` 输出裸 token。\n").action(async (action, name, options) => {
2187
2188
  // Read-only inventory of configured environment names. r18: the sandbox
2188
2189
  // guessed names instead of listing them; doctor's wording now points here.
2189
2190
  const store = new ConfigStore(defaultPaths());
2191
+ if (action === "export") {
2192
+ const environment = options.env ?? name;
2193
+ try {
2194
+ // Resolve before reading credentials or touching the network: this is
2195
+ // the same cross-project isolation gate used by `omc call`.
2196
+ const resolved = store.resolveEnvironment(environment);
2197
+ const example = options.example;
2198
+ if (options.example !== undefined && options.example !== "curl" && options.example !== "node")
2199
+ throw new Error("--example must be curl or node");
2200
+ if (options.format !== undefined && options.format !== "env")
2201
+ throw new Error("--format must be env");
2202
+ const validFields = ["accessToken", "baseUrl", "enterpriseId", "engineCode"];
2203
+ if (options.field !== undefined && !validFields.includes(options.field))
2204
+ throw new Error(`--field must be one of ${validFields.join("|")}`);
2205
+ if (options.format !== undefined && options.field !== undefined)
2206
+ throw new Error("--format and --field are mutually exclusive");
2207
+ const session = await new SessionManager(store).session(resolved.name);
2208
+ const result = exportEnvironment(session, example);
2209
+ if (options.format === "env")
2210
+ process.stdout.write(formatEnv(result));
2211
+ else if (options.field !== undefined) {
2212
+ const value = fieldValue(result, options.field);
2213
+ if (value === undefined)
2214
+ throw new Error(`field ${options.field} is unavailable for this environment`);
2215
+ process.stdout.write(`${value}\n`);
2216
+ }
2217
+ else
2218
+ writeEnvelope(envelope("ok", result, request));
2219
+ commandExitCode = CLI_EXIT_CODES.ok;
2220
+ }
2221
+ catch (error) {
2222
+ const message = error instanceof Error ? error.message : String(error);
2223
+ const setup = /setup|credential|凭据|credentials|no default environment|unknown environment/i.test(message);
2224
+ writeEnvelope(envelope("error", {}, request, {
2225
+ code: setup ? "auth-config" : "environment-unreachable",
2226
+ message,
2227
+ retryable: !setup,
2228
+ ...(setup ? { nextCommand: "omc setup" } : {}),
2229
+ }));
2230
+ commandExitCode = setup ? CLI_EXIT_CODES.authConfig : CLI_EXIT_CODES.internal;
2231
+ }
2232
+ return;
2233
+ }
2190
2234
  if (action === "allow") {
2191
2235
  // The only sanctioned way to widen a project's environment scope. The
2192
2236
  // list stays project-scoped until the operator names the environment —
@@ -0,0 +1,56 @@
1
+ const EXPORT_NOTE = "token 即登录态(本地缓存 8h,401 自动重登);勿提交版本库/粘贴共享文档;仅本机联调。";
2
+ /** Build the structured local-only credential bundle. */
3
+ export function exportEnvironment(session, example) {
4
+ const token = session.token();
5
+ // This is the same projection consumed by CloudPivotSession.send(). In
6
+ // particular, it includes the gateway tenant header when configured.
7
+ const headers = session.headers();
8
+ const login = session.loginInfo();
9
+ const account = login.accountIdentity ?? session.credential.username;
10
+ const result = {
11
+ environment: session.environmentName,
12
+ baseUrl: session.profile.baseUrl,
13
+ enterpriseId: session.enterpriseId(),
14
+ ...(session.profile.engineCode ? { engineCode: session.profile.engineCode } : {}),
15
+ accessToken: token,
16
+ tokenType: "Bearer",
17
+ expiresAt: new Date(Date.now() + 8 * 60 * 60 * 1000).toISOString(),
18
+ expiresAtBasis: "local-ttl-8h",
19
+ headers,
20
+ account: maskAccount(account),
21
+ administrator: session.administrator(),
22
+ note: EXPORT_NOTE,
23
+ };
24
+ return example === undefined ? result : { ...result, example: requestExample(result, example) };
25
+ }
26
+ export function fieldValue(result, field) {
27
+ return result[field];
28
+ }
29
+ /** Bash-compatible, source-able environment assignments. */
30
+ export function formatEnv(result) {
31
+ const lines = [
32
+ `export OMC_BASE_URL=${bashQuote(result.baseUrl)}`,
33
+ `export OMC_ACCESS_TOKEN=${bashQuote(result.accessToken)}`,
34
+ `export OMC_ENTERPRISE_ID=${bashQuote(result.enterpriseId)}`,
35
+ `export OMC_AUTHORIZATION=${bashQuote(result.headers.Authorization)}`,
36
+ ];
37
+ if (result.engineCode)
38
+ lines.push(`export OMC_ENGINE_CODE=${bashQuote(result.engineCode)}`);
39
+ return `${lines.join("\n")}\n`;
40
+ }
41
+ export function maskAccount(account) {
42
+ return `${account.slice(0, 2)}***${account.slice(-2)}`;
43
+ }
44
+ function requestExample(result, example) {
45
+ const url = `${result.baseUrl}/<your-api-path>`;
46
+ const headers = Object.entries(result.headers);
47
+ if (example === "curl") {
48
+ return `curl -s ${bashQuote(url)} ${headers.map(([name, value]) => `-H ${bashQuote(`${name}: ${value}`)}`).join(" ")}`;
49
+ }
50
+ const nodeHeaders = Object.fromEntries(headers);
51
+ return `const res = await fetch(${JSON.stringify(url)}, { headers: ${JSON.stringify(nodeHeaders)} });\nconsole.log(JSON.stringify(await res.json(), null, 2));`;
52
+ }
53
+ function bashQuote(value) {
54
+ return `'${value.replace(/'/gu, "'\\''")}'`;
55
+ }
56
+ //# sourceMappingURL=env-export.js.map
package/dist/session.js CHANGED
@@ -142,8 +142,18 @@ export class CloudPivotSession {
142
142
  throw new Error("session is not logged in");
143
143
  return this.loginResult.administrator;
144
144
  }
145
+ /** Read-only login result for local diagnostic/export surfaces. */
146
+ loginInfo() {
147
+ if (!this.loginResult)
148
+ throw new Error("session is not logged in");
149
+ return this.loginResult;
150
+ }
145
151
  headers() {
146
- return { Authorization: `Bearer ${this.token()}`, "X-LowCode-Corpid": this.enterpriseId() };
152
+ return {
153
+ Authorization: `Bearer ${this.token()}`,
154
+ "X-LowCode-Corpid": this.enterpriseId(),
155
+ ...(this.profile.engineCode ? { "x-lowcode-enginecode": this.profile.engineCode } : {}),
156
+ };
147
157
  }
148
158
  environmentFingerprint() {
149
159
  if (!this.loginResult)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stalfh233/omc-cli",
3
- "version": "0.6.32",
3
+ "version": "0.6.33",
4
4
  "description": "CloudPivot 8.6 configuration capability CLI — governed, fail-closed, coverage-manifest bounded.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -55,7 +55,7 @@
55
55
  },
56
56
  "dependencies": {
57
57
  "commander": "14.0.3",
58
- "omc-core": "0.6.32",
58
+ "omc-core": "0.6.33",
59
59
  "proper-lockfile": "4.1.2",
60
60
  "write-file-atomic": "6.0.0",
61
61
  "zod": "^3.23.8",
@@ -73,6 +73,6 @@
73
73
  "directory": "packages/omc-cli"
74
74
  },
75
75
  "optionalDependencies": {
76
- "@stalfh233/omc-fake-platform": "0.6.32"
76
+ "@stalfh233/omc-fake-platform": "0.6.33"
77
77
  }
78
78
  }