@lingda-ai/dsh-plugin-vault 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/README.md +134 -2
- package/bin/dsh-vault.mjs +189 -0
- package/cordis.patch.yml +7 -0
- package/icon.svg +7 -0
- package/index.js +84 -0
- package/lib/client.js +369 -0
- package/lib/route.js +70 -0
- package/lib/scope.js +125 -0
- package/lib/tool.js +60 -0
- package/lib/vault.js +268 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +83 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,135 @@
|
|
|
1
|
-
#
|
|
1
|
+
# 机库 · Hangar
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> 空港套件第 5 件:装插件、改 profile 前的「照相」,改坏了之后的一键退回。
|
|
4
|
+
|
|
5
|
+
`@lingda-ai/dsh-plugin-vault` 给 `~/.dsh` 里**那些改坏了会让你开不了工、但又不含密钥**的配置拍快照,并在需要时把它写回去。
|
|
6
|
+
|
|
7
|
+
它解决的问题很具体:你在试一个新插件,改 `profiles/<name>/package.json`、加一层 `cordis.patch.yml`,重启,宿主起不来了。报错是一串你看不懂的 bean 名。这时候你需要的不是"重装一遍",而是"回到三十秒前"。
|
|
8
|
+
|
|
9
|
+
## 它备什么
|
|
10
|
+
|
|
11
|
+
| 会备份 | 说明 |
|
|
12
|
+
| --- | --- |
|
|
13
|
+
| `profiles/*/package.json` | 装了哪些插件、bundle 顺序 |
|
|
14
|
+
| `profiles/*/*.yml` `*.yaml` | 补丁层(含 `cordis.patch.yml`) |
|
|
15
|
+
| `settings.json` | 全局设置 |
|
|
16
|
+
| `app-patches` | 应用级补丁 |
|
|
17
|
+
|
|
18
|
+
四类之外一律不碰。展开讲:**不备** `.credentials.yaml`、`*.key/.pem/.p12`、任何名字里带 token / secret / password / credential 的文件、`.env`、`sessions/`、`storages/`、`node_modules/`、`.git/`。
|
|
19
|
+
|
|
20
|
+
这条边界不是"默认值",是**硬规则**:即使你写 `include: ['**']`,密钥类文件仍然进不了备份。理由很直接——备份目录是普通文件,把凭据复制进去,等于凭空多造一份泄漏面,而且这份还没人记得删。
|
|
21
|
+
|
|
22
|
+
## 它不做什么
|
|
23
|
+
|
|
24
|
+
- **不做增量、不做去重**:一份备份就是一次完整拷贝,几十 KB。可解释 > 省空间。
|
|
25
|
+
- **不做云端同步**:备份只在本机 `~/.dsh/ops/vault/`。要异地容灾,自己 `rsync` 那个目录。
|
|
26
|
+
- **不从网页触发写操作**:见下面「为什么回滚只在终端」。
|
|
27
|
+
|
|
28
|
+
## 安装
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
# 作为插件装进 profile(重启宿主即生效,不用手改 bundles —— 插件的 cordis.patch.yml 会自动组合)
|
|
32
|
+
dsh plugin add @lingda-ai/dsh-plugin-vault --profile <你的 profile>
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
只要 CLI、不要面板也行:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npx @lingda-ai/dsh-plugin-vault create
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
想直接用短命令 `dsh-vault`,全局装一份:`npm i -g @lingda-ai/dsh-plugin-vault`。
|
|
42
|
+
|
|
43
|
+
> 前提:profile 里已有宿主壳(`@deepseek-ai/dsh-base` + `dsh-acp-app` 或 `dsh-web-app`)。空白 profile 先装壳,否则插件没有可挂载的位置。
|
|
44
|
+
|
|
45
|
+
## 四道使用动作
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
# 装插件前拍一张(之后每次装前跑一次就够)
|
|
49
|
+
npx @lingda-ai/dsh-plugin-vault create --note "装 X 之前"
|
|
50
|
+
|
|
51
|
+
# 出事了:先看有哪些
|
|
52
|
+
npx @lingda-ai/dsh-plugin-vault list
|
|
53
|
+
|
|
54
|
+
# 校验某份是否完整、当前配置有没有被改过
|
|
55
|
+
npx @lingda-ai/dsh-plugin-vault verify 20261129-2230
|
|
56
|
+
|
|
57
|
+
# 到底差在哪:已改 / 缺失 / 新增
|
|
58
|
+
npx @lingda-ai/dsh-plugin-vault diff 20261129-2230
|
|
59
|
+
|
|
60
|
+
# 退回
|
|
61
|
+
npx @lingda-ai/dsh-plugin-vault restore 20261129-2230
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
`restore` 支持 `--dry-run`(只列出将恢复哪些文件,不动盘),也支持 `--no-pre-backup`(默认关闭:回滚前**自动**把当前状态再备一份,所以点错了还能退回来)。
|
|
65
|
+
|
|
66
|
+
## 三个设计取舍
|
|
67
|
+
|
|
68
|
+
**1. 为什么回滚只在终端,不做「网页一键」**
|
|
69
|
+
|
|
70
|
+
回滚是这套插件里唯一会覆盖用户文件的动作。终端里执行一次,`shell` 历史里留下时间与命令;网页上的一个按钮什么都不留,而它造成的结果是一样的。把唯一的破坏性动作放在需要主动敲命令的地方,是对"好用"和"安全"之间最省事的取舍——真需要面板一键,那个场景应该是「云上多租户托管」,那时才该引入确认令牌与同源校验。
|
|
71
|
+
|
|
72
|
+
**2. 为什么回滚前要自动备份**
|
|
73
|
+
|
|
74
|
+
因为用户会点错。人会点错,所以"退回"本身必须是可逆的。`restore` 先给当前状态拍一张(`pre: true`),再写回目标——最坏情况是多一次回滚。
|
|
75
|
+
|
|
76
|
+
**3. 为什么校验是逐文件 sha256,而不是"文件数对不对"**
|
|
77
|
+
|
|
78
|
+
"文件数对不对"在配置场景里几乎必然通过(改的是内容,不是增删)。真实故障长这样:某个 `package.json` 被编辑器写成了半截 JSON,或者你把一份旧文件 `cp` 回来盖掉了新的。这两种都不改文件数,只改哈希。
|
|
79
|
+
|
|
80
|
+
## 与空港其它四件的关系
|
|
81
|
+
|
|
82
|
+
- **塔台**管指挥,**账本**管结算,**黑匣子**管记录,**航油**管续航,机库管**检修**。
|
|
83
|
+
- 机库不读会话、不读金额、不读模型配置的内容——它只按字节搬运文件。这意味着它**不能**告诉你"这次回滚会让成本变多少",那是航油的活。
|
|
84
|
+
- 与黑匣子的唯一交集是落盘风格:都是 `~/.dsh/ops/<模块>/`、都是 `tmp + rename` 原子写。这不是巧合,是同一套规矩。
|
|
85
|
+
|
|
86
|
+
## 面板
|
|
87
|
+
|
|
88
|
+
右侧栏出现「机库」tab,内容只读:
|
|
89
|
+
|
|
90
|
+
- 最近一份备份的 id、时间、文件数、体积、完整性校验结果;
|
|
91
|
+
- 当前配置与这份备份的差异(已改 / 缺失 / 新增,各列前几项);
|
|
92
|
+
- 最近 6 份备份,点一下切换对比对象;
|
|
93
|
+
- 要回滚时该敲的那一行命令,直接给你复制。
|
|
94
|
+
|
|
95
|
+
面板**不会**替你执行回滚。它只负责让你看清"现在处于什么状态、退回去会发生什么"。
|
|
96
|
+
|
|
97
|
+
## 权限与兼容
|
|
98
|
+
|
|
99
|
+
- 需要 Node ≥ 22.6(用了 `node:test` 与 ESM 原生能力)。
|
|
100
|
+
- 零运行时依赖;不发起任何外部网络请求。
|
|
101
|
+
- 读权限:`DSH_HOME`(默认 `~`)下的配置类文件;写权限:`~/.dsh/ops/vault/` 与 `restore` 时的目标配置文件。
|
|
102
|
+
- 仅在用户显式执行 `dsh-vault restore`(或 `npx @lingda-ai/dsh-plugin-vault restore`) 时写配置;插件加载本身**从不写任何文件**。
|
|
103
|
+
- Web 端只暴露 `GET /dsh-vault/api`(`list` / `verify` / `diff` / `manifest`),列表返回里不含文件内容,只有路径、字节数与哈希。
|
|
104
|
+
|
|
105
|
+
## 落盘结构
|
|
106
|
+
|
|
107
|
+
```
|
|
108
|
+
~/.dsh/ops/vault/
|
|
109
|
+
20261129-2230/
|
|
110
|
+
manifest.json # { v, id, createdAt, home, items[{path,bytes,sha256,mtimeMs}], skipped, totals }
|
|
111
|
+
files/
|
|
112
|
+
settings.json
|
|
113
|
+
profiles/acp/package.json
|
|
114
|
+
profiles/acp/cordis.yml
|
|
115
|
+
20261129-2245/ # restore 前自动生成的那一份
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
默认保留最近 10 份,超出按时间删最旧的(`--keep` 可改)。`manifest.json` 里记的是**相对路径**;`restore` 会拒绝任何含 `..`、绝对路径、反斜杠的条目——防止 manifest 被改坏之后越界写文件。
|
|
119
|
+
|
|
120
|
+
## 测试
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
npm test # 31 项:备份范围、密钥排除、备份/校验/对比/回滚、HTTP 只读、CLI 全链路、客户端门禁
|
|
124
|
+
npm run check # 收录前自检(市场条款逐条断言)
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
其中三组断言值得单独说:
|
|
128
|
+
|
|
129
|
+
- **密钥永不入选**:即使 `include: ['**']`,`.credentials.yaml` / `*token*` / `*.key` / `.env` 仍然被挡住。
|
|
130
|
+
- **篡改拒回滚**:把备份里的一个文件改掉,`restore` 必须拒绝、且**一个文件都不写**。
|
|
131
|
+
- **回滚可逆**:`restore` 之后,必须能用它自动生成的 pre-backup 再退回去。
|
|
132
|
+
|
|
133
|
+
## 许可
|
|
134
|
+
|
|
135
|
+
Apache-2.0 © LingDaAi
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* dsh-vault —— 机库命令行。
|
|
4
|
+
*
|
|
5
|
+
* 设计取舍:备份与回滚只走命令行,不进 HTTP。原因很直接——
|
|
6
|
+
* 回滚会覆盖用户的配置文件。终端里的一次执行会在 shell 历史里留痕;
|
|
7
|
+
* 网页上的一个按钮不会,而它一样能毁掉一个能用的环境。
|
|
8
|
+
*
|
|
9
|
+
* 用法:
|
|
10
|
+
* dsh-vault create [--note "..."]
|
|
11
|
+
* dsh-vault list
|
|
12
|
+
* dsh-vault verify <id>
|
|
13
|
+
* dsh-vault diff <id>
|
|
14
|
+
* dsh-vault restore <id> [--dry-run] [--no-pre-backup]
|
|
15
|
+
* dsh-vault remove <id>
|
|
16
|
+
* dsh-vault doctor
|
|
17
|
+
*
|
|
18
|
+
* 全局:--json 机读;--home <dir>(默认 DSH_HOME 或 ~);--vault <dir>;--keep <n>
|
|
19
|
+
*/
|
|
20
|
+
import { join } from 'node:path'
|
|
21
|
+
import { createVault } from '../lib/vault.js'
|
|
22
|
+
import { selectFiles, DEFAULT_INCLUDE, dshHomeOf } from '../lib/scope.js'
|
|
23
|
+
|
|
24
|
+
const HELP = `dsh-vault —— 备份与回滚 ~/.dsh 里的配置(不含任何密钥)
|
|
25
|
+
|
|
26
|
+
dsh-vault create [--note "装 X 之前"] 给当前状态拍一份
|
|
27
|
+
dsh-vault list 看有哪些备份
|
|
28
|
+
dsh-vault verify <id> 校验备份完整性 + 当前文件是否被改过
|
|
29
|
+
dsh-vault diff <id> 当前配置与这一份差在哪
|
|
30
|
+
dsh-vault restore <id> 回滚(会先自动备份当前状态)
|
|
31
|
+
dsh-vault remove <id> 删掉一份
|
|
32
|
+
dsh-vault doctor 看备份范围与不备份清单
|
|
33
|
+
|
|
34
|
+
全局:--json 机读输出;--home/--vault/--keep 覆盖默认位置与份数
|
|
35
|
+
`
|
|
36
|
+
|
|
37
|
+
function parse(argv) {
|
|
38
|
+
const args = { _: [], json: false, dryRun: false, preBackup: true, note: null }
|
|
39
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
40
|
+
const token = argv[index]
|
|
41
|
+
if (token === '--json') args.json = true
|
|
42
|
+
else if (token === '--dry-run') args.dryRun = true
|
|
43
|
+
else if (token === '--no-pre-backup') args.preBackup = false
|
|
44
|
+
else if (token === '--note') args.note = argv[++index] ?? null
|
|
45
|
+
else if (token === '--home') args.home = argv[++index]
|
|
46
|
+
else if (token === '--vault') args.vault = argv[++index]
|
|
47
|
+
else if (token === '--keep') args.keep = Number(argv[++index])
|
|
48
|
+
else if (token === '--help' || token === '-h') args.help = true
|
|
49
|
+
else args._.push(token)
|
|
50
|
+
}
|
|
51
|
+
return args
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function human(bytes) {
|
|
55
|
+
if (!Number.isFinite(bytes)) return '—'
|
|
56
|
+
if (bytes < 1024) return bytes + 'B'
|
|
57
|
+
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + 'KB'
|
|
58
|
+
return (bytes / 1024 / 1024).toFixed(1) + 'MB'
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function timeOf(at) {
|
|
62
|
+
if (!Number.isFinite(at)) return '—'
|
|
63
|
+
const d = new Date(at)
|
|
64
|
+
const pad = (n) => String(n).padStart(2, '0')
|
|
65
|
+
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}`
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function main(argv) {
|
|
69
|
+
const args = parse(argv)
|
|
70
|
+
if (args.help || args._.length === 0) {
|
|
71
|
+
process.stdout.write(HELP)
|
|
72
|
+
return 0
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const home = args.home ?? dshHomeOf()
|
|
76
|
+
const vaultDir = args.vault ?? join(home, 'ops', 'vault')
|
|
77
|
+
const vault = createVault({ home, vaultDir, keep: args.keep })
|
|
78
|
+
const command = args._[0]
|
|
79
|
+
const id = args._[1]
|
|
80
|
+
|
|
81
|
+
const emit = (payload, humanText) => {
|
|
82
|
+
if (args.json) {
|
|
83
|
+
process.stdout.write(JSON.stringify(payload, null, 2) + '\n')
|
|
84
|
+
} else if (humanText !== undefined) {
|
|
85
|
+
process.stdout.write(humanText)
|
|
86
|
+
}
|
|
87
|
+
return payload.ok === false ? 1 : 0
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const fail = (message) => {
|
|
91
|
+
if (args.json) process.stdout.write(JSON.stringify({ ok: false, error: message }) + '\n')
|
|
92
|
+
else process.stderr.write(`✗ ${message}\n`)
|
|
93
|
+
return 1
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (command === 'create') {
|
|
97
|
+
const result = vault.create({ note: args.note })
|
|
98
|
+
if (!result.ok) return fail(result.error)
|
|
99
|
+
return emit(
|
|
100
|
+
result,
|
|
101
|
+
`✓ 已备份 ${result.files} 个文件(${human(result.bytes)})→ ${result.dir}\n`
|
|
102
|
+
+ (result.skipped.length > 0 ? ` 跳过 ${result.skipped.length} 个(消失或命中排除规则)\n` : ''),
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (command === 'list') {
|
|
107
|
+
const items = vault.list()
|
|
108
|
+
if (items.length === 0) {
|
|
109
|
+
return emit({ ok: true, vaultDir, backups: items }, `机库是空的(${vaultDir})。先跑一次 dsh-vault create。\n`)
|
|
110
|
+
}
|
|
111
|
+
const lines = [`${items.length} 份备份 · ${vaultDir}`, '']
|
|
112
|
+
for (const item of items) {
|
|
113
|
+
lines.push(item.broken
|
|
114
|
+
? ` ${item.id} manifest 读取失败`
|
|
115
|
+
: ` ${item.id} ${timeOf(item.createdAt)} ${item.files} 文件 ${human(item.bytes)}${item.note ? ' · ' + item.note : ''}`)
|
|
116
|
+
}
|
|
117
|
+
return emit({ ok: true, vaultDir, backups: items }, lines.join('\n') + '\n')
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (command === 'verify') {
|
|
121
|
+
if (!id) return fail('用法:dsh-vault verify <id>')
|
|
122
|
+
const result = vault.verify(id)
|
|
123
|
+
if (!result.ok) return fail(result.error)
|
|
124
|
+
const s = result.summary
|
|
125
|
+
const lines = [
|
|
126
|
+
`备份 ${id}(${timeOf(result.createdAt)})`,
|
|
127
|
+
` 完整 ${s.backupIntact}/${s.total}${s.backupIntact === s.total ? ' ✓' : ' ✗'}`,
|
|
128
|
+
` 当前:相同 ${s.currentSame} · 已改 ${s.currentChanged} · 缺失 ${s.currentMissing}`,
|
|
129
|
+
]
|
|
130
|
+
for (const item of result.items) if (!item.backupOk) lines.push(` ! 备份文件不一致:${item.path}`)
|
|
131
|
+
return emit(result, lines.join('\n') + '\n')
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (command === 'diff') {
|
|
135
|
+
if (!id) return fail('用法:dsh-vault diff <id>')
|
|
136
|
+
const result = vault.diff(id)
|
|
137
|
+
if (!result.ok) return fail(result.error)
|
|
138
|
+
const section = (label, list) => (list.length === 0 ? ` ${label} 0` : ` ${label} ${list.length}:\n ${list.join('\n ')}`)
|
|
139
|
+
return emit(result, [
|
|
140
|
+
`当前配置 vs 备份 ${id}(共 ${result.total} 个文件)`,
|
|
141
|
+
section('已改', result.changed),
|
|
142
|
+
section('缺失', result.missing),
|
|
143
|
+
section('新增', result.added),
|
|
144
|
+
].join('\n') + '\n')
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (command === 'restore') {
|
|
148
|
+
if (!id) return fail('用法:dsh-vault restore <id>')
|
|
149
|
+
const result = vault.restore(id, { dryRun: args.dryRun, preBackup: args.preBackup })
|
|
150
|
+
if (!result.ok) return fail(result.error)
|
|
151
|
+
if (result.dryRun) {
|
|
152
|
+
return emit(result, `将恢复 ${result.willRestore.length} 个文件:\n ${result.willRestore.join('\n ')}\n`)
|
|
153
|
+
}
|
|
154
|
+
const lines = [`✓ 已恢复到 ${id}:${result.restored.length} 个文件`]
|
|
155
|
+
if (result.preBackupId) lines.push(` 回滚前状态已存为 ${result.preBackupId}(点错了用 dsh-vault restore ${result.preBackupId} 退回)`)
|
|
156
|
+
if (result.failed.length > 0) {
|
|
157
|
+
lines.push(`✗ ${result.failed.length} 个文件写入失败:`)
|
|
158
|
+
for (const item of result.failed) lines.push(` ${item.path}: ${item.error}`)
|
|
159
|
+
}
|
|
160
|
+
return emit(result, lines.join('\n') + '\n')
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (command === 'remove') {
|
|
164
|
+
if (!id) return fail('用法:dsh-vault remove <id>')
|
|
165
|
+
const result = vault.remove(id)
|
|
166
|
+
if (!result.ok) return fail(result.error)
|
|
167
|
+
return emit(result, `✓ 已删除备份 ${id}\n`)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (command === 'doctor') {
|
|
171
|
+
const paths = selectFiles({ home })
|
|
172
|
+
return emit({ ok: true, home, vaultDir, include: DEFAULT_INCLUDE, selected: paths }, [
|
|
173
|
+
`home: ${home}`,
|
|
174
|
+
`vault: ${vaultDir}`,
|
|
175
|
+
`会备份 ${paths.length} 个文件:`,
|
|
176
|
+
` ${paths.join('\n ') || '(无)'}`,
|
|
177
|
+
'不会备份:.credentials.yaml / *token* / *secret* / *.key|pem|p12 / .env / sessions / storages / node_modules / .git',
|
|
178
|
+
].join('\n') + '\n')
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return fail(`未知命令:${command}(dsh-vault --help)`)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
try {
|
|
185
|
+
process.exitCode = main(process.argv.slice(2))
|
|
186
|
+
} catch (error) {
|
|
187
|
+
process.stderr.write(`✗ ${error instanceof Error ? error.message : String(error)}\n`)
|
|
188
|
+
process.exitCode = 1
|
|
189
|
+
}
|
package/cordis.patch.yml
ADDED
package/icon.svg
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round">
|
|
2
|
+
<path d="M3 20V11.2c0-.5.2-.9.6-1.2l7.6-5.4c.5-.35 1.1-.35 1.6 0l7.6 5.4c.4.3.6.7.6 1.2V20" />
|
|
3
|
+
<path d="M3 20h18" />
|
|
4
|
+
<path d="M8 20v-5.2c0-2.2 1.8-4 4-4s4 1.8 4 4V20" />
|
|
5
|
+
<path d="M12 10.8V7.6" />
|
|
6
|
+
<circle cx="12" cy="13.4" r="1.4" />
|
|
7
|
+
</svg>
|
package/index.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @lingda-ai/dsh-plugin-vault —— 机库:备份与回滚。
|
|
3
|
+
*
|
|
4
|
+
* 定位:`~/.dsh` 是你的机库。装插件、改 profile、换模型,一旦改出问题,
|
|
5
|
+
* 你需要一个"回到上一次能用的状态"的按钮——而且它不该要求你记得当初改了什么。
|
|
6
|
+
*
|
|
7
|
+
* 边界(刻意收窄):
|
|
8
|
+
* - 只备份**配置类**文件(profile/package.json、补丁层、settings、app-patches);
|
|
9
|
+
* - 不备份凭据、会话、存储、node_modules;
|
|
10
|
+
* - HTTP 只读,写操作(备份 / 回滚)走 CLI,留得下审计痕迹。
|
|
11
|
+
*
|
|
12
|
+
* 装配经验(真机踩过):
|
|
13
|
+
* cordis 的服务必须"声明了依赖才能用"。`ctx.tools` 在没 inject 的情况下直接取会抛
|
|
14
|
+
* `cannot get property "tools" without inject`——如果外面套 try 吞掉,表现就是
|
|
15
|
+
* "插件装上了、工具却不存在",而且不报错。所以这里一律走 `ctx.inject([...])`。
|
|
16
|
+
*/
|
|
17
|
+
import { join } from 'node:path'
|
|
18
|
+
import { createVault } from './lib/vault.js'
|
|
19
|
+
import { dshHomeOf, DEFAULT_INCLUDE } from './lib/scope.js'
|
|
20
|
+
import { createApi, handleRequest, API_PREFIX } from './lib/route.js'
|
|
21
|
+
import { createVaultTool } from './lib/tool.js'
|
|
22
|
+
|
|
23
|
+
export const name = '@lingda-ai/dsh-plugin-vault'
|
|
24
|
+
|
|
25
|
+
/** 不强制依赖任何服务:没有 webServer / tools 也能备份、回滚(走 CLI)。 */
|
|
26
|
+
export const inject = []
|
|
27
|
+
|
|
28
|
+
const SERVICE_KEY = 'vault'
|
|
29
|
+
|
|
30
|
+
export function apply(ctx, config = {}) {
|
|
31
|
+
const home = config.home ?? dshHomeOf()
|
|
32
|
+
const vaultDir = config.vaultDir ?? join(home, 'ops', 'vault')
|
|
33
|
+
|
|
34
|
+
const vault = createVault({ home, vaultDir, keep: config.keep, now: config.now })
|
|
35
|
+
const api = createApi({ vault })
|
|
36
|
+
const route = {
|
|
37
|
+
kind: 'prefix',
|
|
38
|
+
path: API_PREFIX,
|
|
39
|
+
handler: (req, res) => handleRequest(api, req, res),
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const mount = (services, callback) => {
|
|
43
|
+
if (typeof ctx.inject === 'function') {
|
|
44
|
+
ctx.inject(services, callback)
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
callback(ctx)
|
|
49
|
+
} catch {
|
|
50
|
+
/* 老宿主没有 inject:能力降级,不阻断加载 */
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
mount(['webServer'], (inner) => {
|
|
55
|
+
const webServer = inner.webServer ?? (typeof inner.get === 'function' ? inner.get('webServer') : null)
|
|
56
|
+
if (!webServer || typeof webServer.register !== 'function') return
|
|
57
|
+
if (typeof inner.effect === 'function') inner.effect(() => webServer.register(route))
|
|
58
|
+
else webServer.register(route)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
mount(['tools'], (inner) => {
|
|
62
|
+
if (inner.tools && typeof inner.tools.register === 'function') {
|
|
63
|
+
inner.tools.register(createVaultTool({ vault }))
|
|
64
|
+
}
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
try {
|
|
68
|
+
if (typeof ctx.provide === 'function' && !(typeof ctx.get === 'function' && ctx.get(SERVICE_KEY))) {
|
|
69
|
+
ctx.provide(SERVICE_KEY, {
|
|
70
|
+
list: () => vault.list(),
|
|
71
|
+
verify: (id) => vault.verify(id),
|
|
72
|
+
diff: (id) => vault.diff(id),
|
|
73
|
+
diagnostics: () => ({ home, vaultDir, include: DEFAULT_INCLUDE }),
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
} catch {
|
|
77
|
+
/* 重复装配(HMR)会抛,忽略 */
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
list: () => vault.list(),
|
|
82
|
+
diagnostics: () => ({ home, vaultDir }),
|
|
83
|
+
}
|
|
84
|
+
}
|