@isvbytes.com/cli 1.0.0

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.
Files changed (2) hide show
  1. package/dist/index.js +142 -0
  2. package/package.json +29 -0
package/dist/index.js ADDED
@@ -0,0 +1,142 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/index.ts
4
+ import { Command } from "commander";
5
+
6
+ // src/config.ts
7
+ var config = {
8
+ dev: {
9
+ token: "eyJhbGciOiJIUzI1NiJ9.eyJzZXJ2aWNlTmFtZSI6IjM3MCIsInNlcnZpY2VLZXkiOiI1OGM2ZmQwNDBkMmU0MzZhODA5NDMwYzc1ODE0YmE1MCIsInNlcnZpY2VUeXBlIjoiMSIsInNlcnZpY2VBcHAiOiIxNjciLCJzZXJ2aWNlQ2x1c3RlciI6IjIiLCJqdGkiOiJiYjA5MzA2MjU0MzA0ZGUyYWU1Yjc5Y2I2MTdiZjE1MSIsImlhdCI6MTU1NDcxMzQ1Mn0.RQOE0_yNIr-cfVozxNmk1JXKQ5dQSDBtyxARLziVeJY",
10
+ api: "http://cmp-governance-backend-dev.earth.xpaas.lenovo.com/internal/governance/api/v1/auth/generate-token"
11
+ },
12
+ tst: {
13
+ token: "eyJhbGciOiJIUzI1NiJ9.eyJzZXJ2aWNlTmFtZSI6IjM3MCIsInNlcnZpY2VLZXkiOiIwOTRiZjRkYmExZGE0NTRkYWJlZGZiYzhhMGU3OWZmOSIsInNlcnZpY2VUeXBlIjoiMSIsInNlcnZpY2VBcHAiOiIxNjciLCJzZXJ2aWNlQ2x1c3RlciI6IjMiLCJqdGkiOiIwMjQ3NDdiZWUyYzI0N2I2OWMxZmEwNWQ3NjJhYTE0YiIsImlhdCI6MTU1NDcxMzYwMn0.LyewINi9m98_3fai0gDLK3LGnG1fTIZZPZVs1S0-xvo",
14
+ api: "http://cmp-governance-backend-tst.earth.xpaas.lenovo.com/internal/governance/api/v1/auth/generate-token"
15
+ },
16
+ pre: {
17
+ token: "eyJhbGciOiJIUzI1NiJ9.eyJzZXJ2aWNlTmFtZSI6IjM3MCIsInNlcnZpY2VLZXkiOiI5ODlkNjdiMWM3NWE0MjcwOGM5ODlhOGRlN2NmYTc3NCIsInNlcnZpY2VUeXBlIjoiMSIsInNlcnZpY2VBcHAiOiIxNjciLCJzZXJ2aWNlQ2x1c3RlciI6IjQiLCJqdGkiOiIxMjVjNWIyODg2NGE0NDRkOGYwZTA2NDhiYWVhYWI5MCIsImlhdCI6MTU1ODMyMjU1NX0.lG-RTxBzmfwtkrgZ9ju4oxf6oukXqSh9rb56lya1p3U",
18
+ api: "http://cmp-governance-backend-pre.earth.xpaas.lenovo.com/internal/governance/api/v1/auth/generate-token"
19
+ },
20
+ prod: {
21
+ token: "eyJhbGciOiJIUzI1NiJ9.eyJzZXJ2aWNlTmFtZSI6IjM3MCIsInNlcnZpY2VLZXkiOiIzMGM5NGQxZDU5NGE0NTRjYjkwNDFjOGIxODU4MDU5OCIsInNlcnZpY2VUeXBlIjoiMSIsInNlcnZpY2VBcHAiOiIxNjciLCJzZXJ2aWNlQ2x1c3RlciI6IjUiLCJqdGkiOiJjM2RmNjVmOWZmNDg0OWUzODUwY2YyZTg0ZDkxMTgwMyIsImlhdCI6MTU1ODMyMjU1NX0.nwfIhC3yHdByuNCZ5kHvrdQ-54CuBMOOLkTmNRbjT0",
22
+ api: "http://cmp-governance-api.earth.xpaas.lenovo.com/api/v1/auth/generate-token"
23
+ }
24
+ };
25
+ var SUPPORTED_ENVS = Object.keys(config);
26
+
27
+ // src/token.ts
28
+ async function generateToken(options) {
29
+ const { env, code, orgId = 88 } = options;
30
+ const envConfig = config[env];
31
+ if (!envConfig) {
32
+ return {
33
+ success: false,
34
+ env,
35
+ code,
36
+ error: `\u4E0D\u652F\u6301\u7684\u73AF\u5883: ${env}\uFF0C\u53EF\u9009: ${Object.keys(config).join(", ")}`
37
+ };
38
+ }
39
+ try {
40
+ const response = await fetch(envConfig.api, {
41
+ method: "POST",
42
+ headers: {
43
+ "content-type": "application/json",
44
+ "CMP-Product-Key": "cmp",
45
+ "SERVICE-AUTHENTICATION": envConfig.token
46
+ },
47
+ body: JSON.stringify({ code, orgId })
48
+ });
49
+ if (!response.ok) {
50
+ return {
51
+ success: false,
52
+ env,
53
+ code,
54
+ error: `HTTP ${response.status}: ${response.statusText}`
55
+ };
56
+ }
57
+ const res = await response.json();
58
+ if (res?.data?.authToken) {
59
+ return {
60
+ success: true,
61
+ authToken: res.data.authToken,
62
+ env,
63
+ code,
64
+ expiresAt: parseJwtExpiry(res.data.authToken)
65
+ };
66
+ }
67
+ return {
68
+ success: false,
69
+ env,
70
+ code,
71
+ error: res?.message || "\u63A5\u53E3\u8FD4\u56DE\u6570\u636E\u4E2D\u65E0 authToken \u5B57\u6BB5"
72
+ };
73
+ } catch (err) {
74
+ return {
75
+ success: false,
76
+ env,
77
+ code,
78
+ error: err.message || String(err)
79
+ };
80
+ }
81
+ }
82
+ function parseJwtExpiry(token) {
83
+ try {
84
+ const payload = JSON.parse(
85
+ Buffer.from(token.split(".")[1], "base64url").toString("utf-8")
86
+ );
87
+ if (payload.exp) {
88
+ return new Date(payload.exp * 1e3).toISOString();
89
+ }
90
+ if (payload.iat) {
91
+ return new Date((payload.iat + 86400) * 1e3).toISOString();
92
+ }
93
+ } catch {
94
+ }
95
+ return void 0;
96
+ }
97
+
98
+ // src/index.ts
99
+ var program = new Command();
100
+ program.name("xcloud-token").description("\u751F\u6210 xCloud authToken\uFF0C\u4F9B AI agent \u548C CLI \u5DE5\u5177\u4F7F\u7528").version("1.0.0");
101
+ program.command("generate").description("\u751F\u6210\u6307\u5B9A\u7528\u6237\u7684 authToken").requiredOption("-e, --env <env>", `\u76EE\u6807\u73AF\u5883 (${SUPPORTED_ENVS.join("/")})`, "prod").requiredOption("-c, --code <code>", "\u7528\u6237 IT code (\u5982 zhangys36)").option("-o, --org-id <orgId>", "\u7EC4\u7EC7 ID", "88").option("--json", "\u4EE5 JSON \u683C\u5F0F\u8F93\u51FA\uFF08\u9002\u5408\u7A0B\u5E8F\u6D88\u8D39\uFF09", false).option("--quiet", "\u53EA\u8F93\u51FA token \u503C\uFF0C\u9002\u5408\u7BA1\u9053\u4F20\u9012", false).action(async (opts) => {
102
+ const result = await generateToken({
103
+ env: opts.env,
104
+ code: opts.code,
105
+ orgId: Number(opts.orgId)
106
+ });
107
+ if (opts.quiet) {
108
+ if (result.success) {
109
+ process.stdout.write(result.authToken);
110
+ } else {
111
+ process.stderr.write(`Error: ${result.error}
112
+ `);
113
+ process.exit(1);
114
+ }
115
+ return;
116
+ }
117
+ if (opts.json) {
118
+ console.log(JSON.stringify(result, null, 2));
119
+ if (!result.success) process.exit(1);
120
+ return;
121
+ }
122
+ if (result.success) {
123
+ console.log(`\u2705 authToken \u751F\u6210\u6210\u529F`);
124
+ console.log(` \u73AF\u5883: ${result.env}`);
125
+ console.log(` \u7528\u6237: ${result.code}`);
126
+ if (result.expiresAt) {
127
+ console.log(` \u8FC7\u671F: ${result.expiresAt}`);
128
+ }
129
+ console.log(`
130
+ ${result.authToken}`);
131
+ } else {
132
+ console.error(`\u274C \u751F\u6210\u5931\u8D25: ${result.error}`);
133
+ process.exit(1);
134
+ }
135
+ });
136
+ program.command("envs").description("\u5217\u51FA\u6240\u6709\u652F\u6301\u7684\u73AF\u5883").action(() => {
137
+ console.log("\u652F\u6301\u7684\u73AF\u5883:\n");
138
+ for (const env of SUPPORTED_ENVS) {
139
+ console.log(` - ${env}`);
140
+ }
141
+ });
142
+ program.parse();
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@isvbytes.com/cli",
3
+ "version": "1.0.0",
4
+ "description": "CLI tool to generate xCloud authToken for AI agents",
5
+ "type": "module",
6
+ "bin": {
7
+ "oliverz": "./dist/index.js"
8
+ },
9
+ "files": [
10
+ "dist"
11
+ ],
12
+ "scripts": {
13
+ "build": "tsup",
14
+ "dev": "tsx src/index.ts",
15
+ "prepublishOnly": "npm run build"
16
+ },
17
+ "publishConfig": {
18
+ "access": "public"
19
+ },
20
+ "dependencies": {
21
+ "commander": "^12.1.0"
22
+ },
23
+ "devDependencies": {
24
+ "@types/node": "^22.0.0",
25
+ "tsup": "^8.3.0",
26
+ "tsx": "^4.19.0",
27
+ "typescript": "^5.5.0"
28
+ }
29
+ }