zt-apollo-cli 0.3.0__tar.gz → 0.5.0__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: zt-apollo-cli
3
- Version: 0.3.0
3
+ Version: 0.5.0
4
4
  Summary: Apollo configuration management CLI
5
5
  Requires-Python: >=3.10
6
6
  Description-Content-Type: text/markdown
@@ -46,6 +46,20 @@ uv publish --token <your_token>
46
46
  zt-apollo-cli login
47
47
  ```
48
48
 
49
+ 有界面登录:
50
+
51
+ ```bash
52
+ zt-apollo-cli login --headed
53
+ ```
54
+
55
+ 也可以导入已经验证过的浏览器 SESSION。`cookie-file` 支持
56
+ `{"SESSION": "..."}` 对象或 Playwright cookie 数组;导入后 CLI 会先调用
57
+ Apollo 业务接口验证,成功后才覆盖本地缓存。
58
+
59
+ ```bash
60
+ zt-apollo-cli login --cookie-file ./cookies.json
61
+ ```
62
+
49
63
  ### 2) 查看部门列表
50
64
 
51
65
  ```bash
@@ -104,6 +118,56 @@ zt-apollo-cli app del-namespace-release-publisher -a your_app_id -n application
104
118
  zt-apollo-cli app update -a your_app_id --file ./app-update.json
105
119
  ```
106
120
 
121
+ ### 11) 修改 Namespace 配置
122
+
123
+ 适用于 yml/json 等文本类 Namespace。`namespaceId` 与 `format` 默认从 `list-namespace` 自动解析。
124
+
125
+ ```bash
126
+ # 从文件更新
127
+ zt-apollo-cli app update-namespace -a your_app_id -n application.yml --file ./application.yml --env DEV --cluster default
128
+
129
+ # 直接传入配置文本
130
+ zt-apollo-cli app update-namespace -a your_app_id -n application.yml --text "# config"
131
+ ```
132
+
133
+ ### 12) 发布 Namespace 配置
134
+
135
+ 将未发布的 Namespace 变更发布到指定环境。`--title` 未指定时自动生成(如 `20260613144414-release`)。
136
+
137
+ ```bash
138
+ zt-apollo-cli app release-namespace -a your_app_id -n application.yml --env DEV --cluster default --comment "发布说明"
139
+
140
+ # 指定发布标题
141
+ zt-apollo-cli app release-namespace -a your_app_id -n application.yml --title "20260613144414-release" --comment "s"
142
+ ```
143
+
144
+ ### 13) 创建 Namespace
145
+
146
+ 在应用下新建 Namespace。默认追加 appId 前缀(`appendNamespacePrefix=true`)。
147
+
148
+ ```bash
149
+ zt-apollo-cli app create-namespace -a your_app_id -n test --format properties --comment ""
150
+
151
+ # 不追加前缀
152
+ zt-apollo-cli app create-namespace -a your_app_id -n application.yml --format yml --no-append-prefix
153
+ ```
154
+
155
+ ### 14) 删除 Namespace(指定环境)
156
+
157
+ 删除指定 env/cluster 下的 Namespace。
158
+
159
+ ```bash
160
+ zt-apollo-cli app delete-namespace -a your_app_id -n test --env DEV --cluster default
161
+ ```
162
+
163
+ ### 15) 删除应用 Namespace(所有环境)
164
+
165
+ 删除应用下的 Namespace 定义及所有环境的配置。
166
+
167
+ ```bash
168
+ zt-apollo-cli app delete-app-namespace -a your_app_id -n test
169
+ ```
170
+
107
171
  ## 全局参数
108
172
  - `-o, --output`:输出格式,支持:
109
173
  - `json`(默认)
@@ -132,7 +196,9 @@ browser_channel: chromium
132
196
 
133
197
  支持通过环境变量自动填充登录页账号密码(如 SSO 页面字段可用):
134
198
 
135
- - `ZT_SSO_USERNAME`
136
- - `ZT_SSO_PASSWORD`
199
+ - `ZT_APOLLO_USERNAME`
200
+ - `ZT_APOLLO_PASSWORD`
201
+
202
+ 旧变量名 `ZT_SSO_USERNAME` / `ZT_SSO_PASSWORD` 仍然兼容。
137
203
 
138
204
  未设置时可在浏览器中手动完成登录。
@@ -33,6 +33,20 @@ uv publish --token <your_token>
33
33
  zt-apollo-cli login
34
34
  ```
35
35
 
36
+ 有界面登录:
37
+
38
+ ```bash
39
+ zt-apollo-cli login --headed
40
+ ```
41
+
42
+ 也可以导入已经验证过的浏览器 SESSION。`cookie-file` 支持
43
+ `{"SESSION": "..."}` 对象或 Playwright cookie 数组;导入后 CLI 会先调用
44
+ Apollo 业务接口验证,成功后才覆盖本地缓存。
45
+
46
+ ```bash
47
+ zt-apollo-cli login --cookie-file ./cookies.json
48
+ ```
49
+
36
50
  ### 2) 查看部门列表
37
51
 
38
52
  ```bash
@@ -91,6 +105,56 @@ zt-apollo-cli app del-namespace-release-publisher -a your_app_id -n application
91
105
  zt-apollo-cli app update -a your_app_id --file ./app-update.json
92
106
  ```
93
107
 
108
+ ### 11) 修改 Namespace 配置
109
+
110
+ 适用于 yml/json 等文本类 Namespace。`namespaceId` 与 `format` 默认从 `list-namespace` 自动解析。
111
+
112
+ ```bash
113
+ # 从文件更新
114
+ zt-apollo-cli app update-namespace -a your_app_id -n application.yml --file ./application.yml --env DEV --cluster default
115
+
116
+ # 直接传入配置文本
117
+ zt-apollo-cli app update-namespace -a your_app_id -n application.yml --text "# config"
118
+ ```
119
+
120
+ ### 12) 发布 Namespace 配置
121
+
122
+ 将未发布的 Namespace 变更发布到指定环境。`--title` 未指定时自动生成(如 `20260613144414-release`)。
123
+
124
+ ```bash
125
+ zt-apollo-cli app release-namespace -a your_app_id -n application.yml --env DEV --cluster default --comment "发布说明"
126
+
127
+ # 指定发布标题
128
+ zt-apollo-cli app release-namespace -a your_app_id -n application.yml --title "20260613144414-release" --comment "s"
129
+ ```
130
+
131
+ ### 13) 创建 Namespace
132
+
133
+ 在应用下新建 Namespace。默认追加 appId 前缀(`appendNamespacePrefix=true`)。
134
+
135
+ ```bash
136
+ zt-apollo-cli app create-namespace -a your_app_id -n test --format properties --comment ""
137
+
138
+ # 不追加前缀
139
+ zt-apollo-cli app create-namespace -a your_app_id -n application.yml --format yml --no-append-prefix
140
+ ```
141
+
142
+ ### 14) 删除 Namespace(指定环境)
143
+
144
+ 删除指定 env/cluster 下的 Namespace。
145
+
146
+ ```bash
147
+ zt-apollo-cli app delete-namespace -a your_app_id -n test --env DEV --cluster default
148
+ ```
149
+
150
+ ### 15) 删除应用 Namespace(所有环境)
151
+
152
+ 删除应用下的 Namespace 定义及所有环境的配置。
153
+
154
+ ```bash
155
+ zt-apollo-cli app delete-app-namespace -a your_app_id -n test
156
+ ```
157
+
94
158
  ## 全局参数
95
159
  - `-o, --output`:输出格式,支持:
96
160
  - `json`(默认)
@@ -119,7 +183,9 @@ browser_channel: chromium
119
183
 
120
184
  支持通过环境变量自动填充登录页账号密码(如 SSO 页面字段可用):
121
185
 
122
- - `ZT_SSO_USERNAME`
123
- - `ZT_SSO_PASSWORD`
186
+ - `ZT_APOLLO_USERNAME`
187
+ - `ZT_APOLLO_PASSWORD`
188
+
189
+ 旧变量名 `ZT_SSO_USERNAME` / `ZT_SSO_PASSWORD` 仍然兼容。
124
190
 
125
191
  未设置时可在浏览器中手动完成登录。
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "zt-apollo-cli"
7
- version = "0.3.0"
7
+ version = "0.5.0"
8
8
  description = "Apollo configuration management CLI"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -15,7 +15,8 @@ class ApolloApi:
15
15
  """发送 API 请求;认证失效时自动重新登录并重试一次"""
16
16
  cookies = get_auth_cookies()
17
17
  headers = get_auth_headers(cookies)
18
- extra_headers = kwargs.pop("headers", None)
18
+ request_kwargs = dict(kwargs)
19
+ extra_headers = request_kwargs.pop("headers", None)
19
20
  if extra_headers:
20
21
  headers.update(extra_headers)
21
22
  url = f"{self.base_url}/{path.lstrip('/')}"
@@ -25,7 +26,7 @@ class ApolloApi:
25
26
  url=url,
26
27
  headers=headers,
27
28
  cookies=cookies,
28
- **kwargs
29
+ **request_kwargs
29
30
  )
30
31
 
31
32
  if is_auth_error(response):
@@ -35,7 +36,11 @@ class ApolloApi:
35
36
  f"{response.status_code} {response.text[:500]}"
36
37
  )
37
38
  print("检测到认证失效,正在自动重新登录...")
38
- login()
39
+ refreshed_cookies = login()
40
+ if not refreshed_cookies or not refreshed_cookies.get("SESSION"):
41
+ raise RuntimeError(
42
+ "自动重新登录未返回有效 Apollo SESSION,已停止重试。"
43
+ )
39
44
  return self._request(method, path, _retried=True, **kwargs)
40
45
 
41
46
  if response.status_code >= 400:
@@ -77,6 +82,64 @@ class ApolloApi:
77
82
  f"/apps/{app_id}/envs/{env}/clusters/{cluster}/namespaces",
78
83
  )
79
84
 
85
+ def create_app_namespace(
86
+ self,
87
+ app_id: str,
88
+ name: str,
89
+ comment: str = "",
90
+ is_public: bool = False,
91
+ config_format: str = "properties",
92
+ append_namespace_prefix: bool = True,
93
+ ):
94
+ """创建应用 Namespace"""
95
+ payload = {
96
+ "appId": app_id,
97
+ "name": name,
98
+ "comment": comment,
99
+ "isPublic": is_public,
100
+ "format": config_format,
101
+ }
102
+ headers = {
103
+ "Content-Type": "application/json;charset=UTF-8"
104
+ }
105
+ return self._request(
106
+ "POST",
107
+ f"/apps/{app_id}/appnamespaces",
108
+ params={"appendNamespacePrefix": str(append_namespace_prefix).lower()},
109
+ json=payload,
110
+ headers=headers,
111
+ )
112
+
113
+ def clean_namespace(
114
+ self,
115
+ app_id: str,
116
+ namespace: str
117
+ ):
118
+ """删除应用 Namespace(该应用下所有环境)"""
119
+ headers = {
120
+ }
121
+ return self._request(
122
+ "DELETE",
123
+ f"/apps/{app_id}/appnamespaces/{namespace}",
124
+ headers=headers,
125
+ )
126
+
127
+ def delete_namespace(
128
+ self,
129
+ app_id: str,
130
+ namespace: str,
131
+ env: str = "DEV",
132
+ cluster: str = "default"
133
+ ):
134
+ """删除指定环境下的 Namespace"""
135
+ headers = {
136
+ }
137
+ return self._request(
138
+ "DELETE",
139
+ f"/apps/{app_id}/envs/{env}/clusters/{cluster}/namespaces/{namespace}",
140
+ headers=headers,
141
+ )
142
+
80
143
  def get_namespace_active_release(
81
144
  self,
82
145
  app_id: str,
@@ -151,3 +214,96 @@ class ApolloApi:
151
214
  data=user_code,
152
215
  headers={"Content-Type": "text/plain;charset=UTF-8"},
153
216
  )
217
+
218
+ def find_namespace(
219
+ self,
220
+ app_id: str,
221
+ namespace: str,
222
+ env: str = "DEV",
223
+ cluster: str = "default",
224
+ ) -> dict:
225
+ """按名称查找 Namespace,返回 namespaceId 与 format"""
226
+ namespaces = self.list_namespaces(app_id=app_id, env=env, cluster=cluster)
227
+ if not isinstance(namespaces, list):
228
+ raise Exception(f"无法解析 Namespace 列表: {namespaces}")
229
+
230
+ for item in namespaces:
231
+ base_info = item.get("baseInfo") or {}
232
+ if base_info.get("namespaceName") == namespace:
233
+ namespace_id = base_info.get("id")
234
+ if namespace_id is None:
235
+ raise Exception(f"Namespace {namespace!r} 缺少 id")
236
+ return {
237
+ "namespaceId": namespace_id,
238
+ "format": item.get("format"),
239
+ }
240
+
241
+ raise Exception(
242
+ f"未找到 Namespace: {namespace!r} "
243
+ f"(appId={app_id}, env={env}, cluster={cluster})"
244
+ )
245
+
246
+ def update_namespace_config(
247
+ self,
248
+ app_id: str,
249
+ namespace: str,
250
+ config_text: str,
251
+ env: str = "DEV",
252
+ cluster: str = "default",
253
+ namespace_id: int | None = None,
254
+ config_format: str | None = None
255
+ ):
256
+ """更新 Namespace 配置(文本类 format,如 yml/json)"""
257
+ if namespace_id is None or config_format is None:
258
+ info = self.find_namespace(
259
+ app_id=app_id,
260
+ namespace=namespace,
261
+ env=env,
262
+ cluster=cluster,
263
+ )
264
+ namespace_id = namespace_id or info["namespaceId"]
265
+ config_format = config_format or info["format"]
266
+
267
+ if not config_format:
268
+ raise Exception(f"无法确定 Namespace {namespace!r} 的 format")
269
+
270
+ payload = {
271
+ "configText": config_text,
272
+ "namespaceId": namespace_id,
273
+ "format": config_format,
274
+ }
275
+ headers = {
276
+ "Content-Type": "application/json;charset=UTF-8",
277
+ }
278
+ return self._request(
279
+ "PUT",
280
+ f"/apps/{app_id}/envs/{env}/clusters/{cluster}/namespaces/{namespace}/items",
281
+ json=payload,
282
+ headers=headers,
283
+ )
284
+
285
+ def release_namespace(
286
+ self,
287
+ app_id: str,
288
+ namespace: str,
289
+ release_title: str,
290
+ release_comment: str = "",
291
+ is_emergency_publish: bool = False,
292
+ env: str = "DEV",
293
+ cluster: str = "default"
294
+ ):
295
+ """发布 Namespace 配置"""
296
+ payload = {
297
+ "releaseTitle": release_title,
298
+ "releaseComment": release_comment,
299
+ "isEmergencyPublish": is_emergency_publish,
300
+ }
301
+ headers = {
302
+ "Content-Type": "application/json;charset=UTF-8",
303
+ }
304
+ return self._request(
305
+ "POST",
306
+ f"/apps/{app_id}/envs/{env}/clusters/{cluster}/namespaces/{namespace}/releases",
307
+ json=payload,
308
+ headers=headers,
309
+ )