snan-cli 1.0.0 → 1.0.1

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/README.md CHANGED
@@ -1,168 +1,127 @@
1
1
  # Astro CLI
2
2
 
3
- A Node.js CLI tool that exposes backend HTTP interfaces as command-line commands. Supports modular extension without core file modifications.
3
+ Astro 数据查询命令行工具。通过统一的 `astro <module> <action>` 命令调用 Astro OpenAPI。
4
4
 
5
- ## Installation
5
+ ## 安装
6
+
7
+ 需要 Node.js 16 或更高版本。
6
8
 
7
9
  ```bash
8
10
  npm install -g snan-cli
11
+ astro --version
9
12
  ```
10
13
 
11
- ## Configuration
14
+ npm 包名是 `snan-cli`,安装后的可执行命令是 `astro`。
12
15
 
13
- Configure the CLI with one of three methods (priority: env vars > ~/.astrorc > .env):
16
+ ## 配置
14
17
 
15
- ### 1. Environment Variables (Highest Priority)
18
+ 推荐使用交互式配置:
16
19
 
17
- **macOS / Linux / Git Bash(bash 语法)**
20
+ ```bash
21
+ astro setup
22
+ ```
18
23
 
19
- `export` 仅在 bash/zsh 等 shell 中有效。
24
+ 也可以使用环境变量:
20
25
 
21
26
  ```bash
27
+ export ASTRO_BASE_URL=https://data.snanai.com
22
28
  export ASTRO_TOKEN=your-bearer-token
23
- export ASTRO_BASE_URL=http://127.0.0.1:8080
24
- export ASTRO_PREFIX=/astro-server/mcp
25
29
  export ASTRO_TIMEOUT=30000
26
30
  ```
27
31
 
28
- **Windows PowerShell**
29
-
30
- PowerShell 不支持 `export`。在当前会话中设置环境变量请使用 `$env:NAME = "value"`(关闭终端后失效):
31
-
32
- ```powershell
33
- $env:ASTRO_TOKEN = "your-bearer-token"
34
- $env:ASTRO_BASE_URL = "http://127.0.0.1:8080"
35
- $env:ASTRO_PREFIX = "/astro-server/mcp"
36
- $env:ASTRO_TIMEOUT = "30000"
37
- ```
38
-
39
- 检查是否生效:`$env:ASTRO_TOKEN`
32
+ 或写入 `~/.astrorc`:
40
33
 
41
- 若要在 **用户级永久** 生效(新开 PowerShell 仍有效),可使用:
42
-
43
- ```powershell
44
- [System.Environment]::SetEnvironmentVariable('ASTRO_TOKEN', 'your-bearer-token', 'User')
45
- ```
46
-
47
- (`ASTRO_BASE_URL` 等同理替换变量名与值。)
48
-
49
- ### 2. Configuration File (~/.astrorc)
50
34
  ```json
51
35
  {
52
- "baseUrl": "http://127.0.0.1:8080",
53
- "token": "your-bearer-token-here",
54
- "prefix": "/astro-server/mcp",
36
+ "baseUrl": "https://data.snanai.com",
37
+ "token": "your-bearer-token",
55
38
  "timeout": 30000
56
39
  }
57
40
  ```
58
41
 
59
- ### 3. .env File
60
- ```
61
- ASTRO_TOKEN=your-bearer-token
62
- ASTRO_BASE_URL=http://127.0.0.1:8080
63
- ASTRO_PREFIX=/astro-server/mcp
64
- ASTRO_TIMEOUT=30000
65
- ```
42
+ 配置优先级为环境变量、`~/.astrorc`、默认配置。默认服务地址是 `https://data.snanai.com`。
66
43
 
67
- ## Usage
44
+ ## 命令
68
45
 
69
- ### Basic Commands
46
+ 查看帮助:
70
47
 
71
48
  ```bash
72
- # View available commands
73
49
  astro --help
50
+ astro metadata --help
51
+ astro metadata tables --help
52
+ ```
74
53
 
75
- # View module commands
76
- astro data --help
54
+ ### 元数据
77
55
 
78
- # View action options
79
- astro data search --help
56
+ ```bash
57
+ # 获取数据来源;结果包含 serviceName、dsCode、sourceType 和 dbType
58
+ astro metadata sources
80
59
 
81
- # Execute action with JSON body
82
- astro data search --body '{"sql":"SELECT * FROM table LIMIT 10"}'
60
+ # serviceName 必须来自 metadata sources
61
+ astro metadata tables --serviceName=tenant-doris_prod --keyword="订单|order"
83
62
 
84
- # Output compact JSON (no pretty-printing)
85
- astro data search --body '{"sql":"..."}' --raw
63
+ # fqn 为 serviceName.database.schema.table
64
+ astro metadata tableDetail --fqn=tenant-doris_prod.default.prod_dwi.dwi_order
86
65
  ```
87
66
 
88
- ### Command Structure
67
+ ### 数据资产
89
68
 
90
- Commands follow the pattern: `astro <module> <action> [options]`
69
+ ```bash
70
+ astro asset dataModels --keyword="销售|订单"
71
+ astro asset dataModelDetail --nameEn=SalesOrder
72
+ astro asset indicators --keyword="GMV|成交额"
73
+ ```
91
74
 
92
- - **Module**: Data query/manipulation, standard operations, etc.
93
- - **Action**: search, insert, update, delete, etc.
94
- - **Options**: `--body <json>` for request payload, `--raw` for compact output
75
+ ### 数仓数据
95
76
 
96
- ## Error Handling
77
+ ```bash
78
+ astro data queryDw --body="{\"sql\":\"SELECT order_date, COUNT(*) AS order_count FROM prod_dwi.dwi_order GROUP BY order_date LIMIT 100\",\"env\":\"prod\"}"
79
+ ```
97
80
 
98
- The CLI provides clear error messages for:
99
- - Missing token configuration
100
- - Invalid JSON in `--body`
101
- - HTTP errors (with status code and response details)
102
- - Network errors
81
+ 请求体字段:
103
82
 
104
- ## Adding New Modules
83
+ - `sql`:必填,只读 SQL。
84
+ - `env`:可选,支持 `dev` 或 `prod`,默认 `prod`。
105
85
 
106
- Create a new module file in `src/modules/`:
86
+ ### 源数据库数据
107
87
 
108
- **src/modules/standard.js**
109
- ```js
110
- module.exports = {
111
- name: 'standard',
112
- description: 'Standard data operations',
113
- actions: {
114
- query: {
115
- description: 'Query data with filters',
116
- },
117
- transform: {
118
- description: 'Transform data format',
119
- },
120
- },
121
- };
88
+ ```bash
89
+ astro data queryOrigin --body="{\"dsCode\":\"erp_prod\",\"sql\":\"SELECT order_no, amount FROM sale_order LIMIT 50\"}"
122
90
  ```
123
91
 
124
- The module is automatically discovered and registered. No core files need modification.
92
+ `dsCode` 必须来自 `metadata sources`,并且对应 `sourceType=DATASOURCE` 的来源。
125
93
 
126
- ### Usage
94
+ ### 数据标准
127
95
 
128
96
  ```bash
129
- astro standard query --body '{"filter":"..."}'
130
- astro standard transform --body '{"format":"json"}'
97
+ astro standard dataStandard --body='{"nameCn":"客户编码"}'
98
+ astro standard nameRoot --body='{"nameCn":"客户"}'
131
99
  ```
132
100
 
133
- ## Project Structure
101
+ 具体请求字段以服务端 `DataStandardRequestDTO` 和 `StandardDictRequestDTO` 为准。
134
102
 
135
- ```
136
- .
137
- ├── bin/
138
- │ └── astro # Executable entry point
139
- ├── src/
140
- │ ├── index.js # Main CLI orchestrator
141
- │ ├── config/
142
- │ │ └── loader.js # Configuration reader
143
- │ ├── http/
144
- │ │ └── client.js # HTTP client with axios
145
- │ ├── modules/
146
- │ │ ├── index.js # Module auto-loader
147
- │ │ └── data.js # Example data module
148
- │ └── output/
149
- │ └── formatter.js # Output formatting & error handling
150
- ├── .astrorc.example # Configuration template
151
- └── package.json
103
+ ## 输出
104
+
105
+ 默认输出格式化 JSON。添加 `--raw` 输出紧凑 JSON:
106
+
107
+ ```bash
108
+ astro metadata sources --raw
152
109
  ```
153
110
 
154
- ## API Endpoint Format
111
+ ## Shell 补全
155
112
 
156
- Requests are sent to: `{baseUrl}{prefix}/{module}/{action}`
113
+ ```bash
114
+ astro --setup-completion
115
+ ```
157
116
 
158
- Example: `http://127.0.0.1:8080/astro-server/mcp/data/search`
117
+ 执行后重新打开终端,或重新加载对应的 shell 配置文件。
159
118
 
160
- ## Dependencies
119
+ ## 请求地址
161
120
 
162
- - **axios**: HTTP client
163
- - **commander**: CLI argument parsing
164
- - **dotenv**: Environment variable loading
121
+ 各模块请求当前版本的 Astro OpenAPI:
165
122
 
166
- ## License
123
+ ```text
124
+ {baseUrl}/astro-server/openapi/v1/{module}/{action}
125
+ ```
167
126
 
168
- MIT
127
+ 所有请求使用 `Authorization: Bearer <token>` 认证。
package/bin/astro CHANGED
@@ -1,3 +1,2 @@
1
1
  #!/usr/bin/env node
2
-
3
- require('../src');
2
+ require('../src/index.js');
package/package.json CHANGED
@@ -1,15 +1,21 @@
1
1
  {
2
2
  "name": "snan-cli",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "CLI tool to expose backend HTTP interfaces as command-line commands",
5
5
  "type": "commonjs",
6
6
  "bin": {
7
- "astro": "bin/astro"
7
+ "astro": "./bin/astro"
8
8
  },
9
9
  "main": "src/index.js",
10
+ "files": [
11
+ "bin",
12
+ "src",
13
+ ".astrorc.example",
14
+ "README.md"
15
+ ],
10
16
  "scripts": {
11
17
  "start": "node bin/astro",
12
- "test": "echo \"Error: no test specified\" && exit 1"
18
+ "test": "node test/cli.test.js"
13
19
  },
14
20
  "keywords": [
15
21
  "cli",
@@ -18,6 +24,12 @@
18
24
  ],
19
25
  "author": "",
20
26
  "license": "MIT",
27
+ "engines": {
28
+ "node": ">=16"
29
+ },
30
+ "publishConfig": {
31
+ "registry": "https://registry.npmjs.org/"
32
+ },
21
33
  "dependencies": {
22
34
  "axios": "^1.6.0",
23
35
  "commander": "^11.0.0",
package/src/index.js CHANGED
@@ -5,6 +5,7 @@ const { createHttpClient } = require('./http/client');
5
5
  const { loadModules } = require('./modules');
6
6
  const { print, error: printError } = require('./output/formatter');
7
7
  const { runSetup } = require('./commands/setup');
8
+ const { version } = require('../package.json');
8
9
 
9
10
  function setupCompletion(modules) {
10
11
  const completionTree = {};
@@ -42,7 +43,7 @@ async function main() {
42
43
  program
43
44
  .name('astro')
44
45
  .description('CLI tool to expose backend HTTP interfaces')
45
- .version('1.0.0');
46
+ .version(version);
46
47
 
47
48
  program
48
49
  .command('setup')
@@ -91,7 +92,13 @@ async function main() {
91
92
  cmd.option(`--${p.name} <value>`, p.description);
92
93
  });
93
94
  } else {
94
- cmd.option('--body <json>', 'JSON body for the request');
95
+ const bodyParam = paramDefs.find(p => p.name === 'body');
96
+ const bodyDescription = bodyParam?.description || 'JSON body for the request';
97
+ if (bodyParam?.required) {
98
+ cmd.requiredOption('--body <json>', bodyDescription);
99
+ } else {
100
+ cmd.option('--body <json>', bodyDescription);
101
+ }
95
102
  }
96
103
 
97
104
  cmd.option('--raw', 'Output compact JSON');
@@ -12,11 +12,11 @@ module.exports = {
12
12
  ],
13
13
  },
14
14
  queryDw: {
15
- description: '查询治理好的数仓资产数据',
15
+ description: '查询治理好的数仓资产数据(env 支持 dev/prod,不传默认为 prod)',
16
16
  method: 'POST',
17
17
  path: '/queryDw',
18
18
  params: [
19
- { name: 'body', description: 'JSON body: { "sql": "SQL查询语句", "rule": "数据统计口径(可选)" }', required: true },
19
+ { name: 'body', description: 'JSON body: { "sql": "SQL查询语句", "env": "dev或prod(可选,默认prod)" }', required: true },
20
20
  ],
21
21
  },
22
22
  },
@@ -4,24 +4,23 @@ module.exports = {
4
4
  urlPrefix: '/astro-server/openapi/v1/metadata',
5
5
  actions: {
6
6
  sources: {
7
- description: '获取当前租户下所有元数据来源列表(查表前先调此接口获取 systemAbbr)',
7
+ description: '获取当前租户下所有元数据来源列表(查表前先获取 serviceName,查源数据时使用 dsCode)',
8
8
  method: 'GET',
9
9
  params: [],
10
10
  },
11
11
  tables: {
12
- description: '根据元数据来源简称和关键字查询表列表(最多 200 条)',
12
+ description: '根据元数据来源 serviceName 和关键字查询表列表(最多 200 条)',
13
13
  method: 'GET',
14
14
  params: [
15
- { name: 'systemAbbr', description: '元数据来源简称,传 doris_prod 查数仓表,传数据源简称查数据源表', required: true },
15
+ { name: 'serviceName', description: '元数据来源唯一标识,通过 metadata sources 获取', required: true },
16
16
  { name: 'keyword', description: '查询关键字,匹配表名或描述,多关键字用空格或|分隔,为空返回全部', required: false },
17
17
  ],
18
18
  },
19
19
  tableDetail: {
20
- description: '获取表详情,包含字段列表和字段类型',
20
+ description: '根据四段式 FQN 获取表详情,包含字段列表和字段类型',
21
21
  method: 'GET',
22
22
  params: [
23
- { name: 'systemAbbr', description: '元数据来源简称,传 doris_prod 查数仓表,传数据源简称查数据源表', required: true },
24
- { name: 'schemaTable', description: '表引用,schema.tablename 形式,如 ods.ods_order', required: true },
23
+ { name: 'fqn', description: '完全限定名,格式 serviceName.database.schema.table,通过 metadata tables 结果构造', required: true },
25
24
  ],
26
25
  },
27
26
  },