@srobroek/pi-chezmoi-editor 0.0.0-stage → 0.7.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.
- package/CHANGELOG.md +61 -0
- package/LICENSE +202 -0
- package/README.md +66 -2
- package/extensions/chezmoi.js +920 -0
- package/package.json +31 -4
- package/skills/chezmoi-editor/SKILL.md +64 -0
- package/skills/chezmoi-editor/references/conventions.md +31 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
## chezmoi [0.7.0](https://github.com/srobroek/harness-plugins/compare/chezmoi--v0.6.0...chezmoi--v0.7.0) (2026-11-23)
|
|
2
|
+
|
|
3
|
+
### Features
|
|
4
|
+
|
|
5
|
+
* **chezmoi:** add pi write guard ([b462296](https://github.com/srobroek/harness-plugins/commit/b4622966c11a824e069390597019f0b4bbca10b8))
|
|
6
|
+
|
|
7
|
+
## chezmoi [0.6.0](https://github.com/srobroek/harness-plugins/compare/chezmoi--v0.5.0...chezmoi--v0.6.0) (2026-11-21)
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* **chezmoi:** fix one-off host changes directly instead of adding reconcile scripts ([92e3b16](https://github.com/srobroek/harness-plugins/commit/92e3b16cbf2ee80520cb59e47176ef945157a259))
|
|
12
|
+
|
|
13
|
+
# Changelog
|
|
14
|
+
|
|
15
|
+
## [0.5.0](https://github.com/srobroek/opencode-plugins/compare/chezmoi--v0.4.0...chezmoi--v0.5.0) (2026-11-20)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Features
|
|
19
|
+
|
|
20
|
+
* plugin scan fixes, wider agent access and the plugin-audit plugin ([9c71525](https://github.com/srobroek/opencode-plugins/commit/9c7152559776aaf80cfed9626d78bafdd845fa9a))
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
### Bug Fixes
|
|
24
|
+
|
|
25
|
+
* **chezmoi:** allow writes to seed-once create_ targets ([#261](https://github.com/srobroek/opencode-plugins/issues/261)) ([bb00818](https://github.com/srobroek/opencode-plugins/commit/bb00818637ddfca7190a8105d3ee174f1268d1f4))
|
|
26
|
+
|
|
27
|
+
## [0.4.0](https://github.com/srobroek/opencode-plugins/compare/chezmoi--v0.3.0...chezmoi--v0.4.0) (2026-11-12)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
### Features
|
|
31
|
+
|
|
32
|
+
* **safety:** refuse direct 1Password reads and literal secrets in commands, pointing to fnox ([#131](https://github.com/srobroek/opencode-plugins/issues/131)) ([36cd391](https://github.com/srobroek/opencode-plugins/commit/36cd3912e954c1078b808802b069d7010d8f319f))
|
|
33
|
+
|
|
34
|
+
## [0.3.0](https://github.com/srobroek/opencode-plugins/compare/chezmoi--v0.2.0...chezmoi--v0.3.0) (2026-11-11)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
### Features
|
|
38
|
+
|
|
39
|
+
* **agent-efficiency:** add lead routing, a foreground sleep guard and subagent scratch reminders ([#122](https://github.com/srobroek/opencode-plugins/issues/122)) ([34bdb2d](https://github.com/srobroek/opencode-plugins/commit/34bdb2d512109065956f88397bd2dc8e6e99ecd3))
|
|
40
|
+
|
|
41
|
+
## [0.2.0](https://github.com/srobroek/opencode-plugins/compare/chezmoi--v0.1.0...chezmoi--v0.2.0) (2026-10-27)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
### Features
|
|
45
|
+
|
|
46
|
+
* **agent-efficiency:** run bash tool calls as the built-in shell tool ([bdca7d7](https://github.com/srobroek/opencode-plugins/commit/bdca7d779c47d9cd2cdc6db8d8a8d5f78a3ba8cc))
|
|
47
|
+
* **chezmoi:** port managed-file guard and chezmoi-editor skill ([08817a5](https://github.com/srobroek/opencode-plugins/commit/08817a50a7eff849cb26272f3feffec53b32cfba))
|
|
48
|
+
* **journeys:** port journeys tools, skills and scribe/validator subagents ([88b9fec](https://github.com/srobroek/opencode-plugins/commit/88b9fec97bde041f9894937e6d2c19f3364447b7))
|
|
49
|
+
* **lib:** honour metadata opencode/autoinvoke in shipped SKILL.md files ([881e7df](https://github.com/srobroek/opencode-plugins/commit/881e7df220095d0155b07437e733fdb795d9279b))
|
|
50
|
+
* **lib:** register shipped agent files and content-only plugins ([df7fa53](https://github.com/srobroek/opencode-plugins/commit/df7fa53ac93f0327a1735fcf1e5c6017d0216754))
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
### Bug Fixes
|
|
54
|
+
|
|
55
|
+
* **chezmoi:** guard chezmoi probes against lost exit events ([cc30a03](https://github.com/srobroek/opencode-plugins/commit/cc30a03639f5be8190b02587083b12d00b8f3a0f))
|
|
56
|
+
* **lib:** accept numeric signal codes from @types/bun 1.4.4 ([a3d2e42](https://github.com/srobroek/opencode-plugins/commit/a3d2e4225211d1a288fa3fed561e1c9d8e696284))
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
### Performance
|
|
60
|
+
|
|
61
|
+
* shorten skill descriptions and hide pure reference skills from the listing ([51841bd](https://github.com/srobroek/opencode-plugins/commit/51841bd68ae7fe809711430fe3430b75b63e81c0))
|
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,67 @@
|
|
|
1
|
-
#
|
|
1
|
+
# chezmoi
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Refuses edits and shell writes to chezmoi-managed live files under `$HOME`, because
|
|
4
|
+
the next `chezmoi apply` overwrites them from source. The refusal names the source
|
|
5
|
+
path to edit instead (`chezmoi source-path <target>`).
|
|
6
|
+
|
|
7
|
+
Ported from the omp-plugins `chezmoi` plugin.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
opencode plugin add 'github:srobroek/harness-plugins::path:plugins/chezmoi/opencode'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Claude Code install the skill from this repository's marketplace:
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
claude plugin install chezmoi@harness-plugins
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Claude Code installs the skill without a write guard.
|
|
22
|
+
|
|
23
|
+
Pi loads `plugins/chezmoi/pi/extensions/chezmoi.js` as an extension. Foreground
|
|
24
|
+
children started by pi-subagents do not inherit parent extensions. Add the extension
|
|
25
|
+
to `subagents.defaultSubagentOnlyExtensions` in Pi settings:
|
|
26
|
+
|
|
27
|
+
```json
|
|
28
|
+
{
|
|
29
|
+
"subagents": {
|
|
30
|
+
"defaultSubagentOnlyExtensions": ["/absolute/path/to/chezmoi.js"]
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
For one agent, use `subagentOnlyExtensions` in its configuration. Set an absolute
|
|
36
|
+
path to the built extension.
|
|
37
|
+
|
|
38
|
+
## Guard
|
|
39
|
+
|
|
40
|
+
OpenCode's `tool.hook("execute.before")` and pi's `tool_call` handler block writes
|
|
41
|
+
to a managed target. They read write targets statically:
|
|
42
|
+
|
|
43
|
+
- `edit` and `write`: the named `path`.
|
|
44
|
+
- `patch` (OpenCode): each file header (`Add File`, `Update File`, `Delete File`,
|
|
45
|
+
`Move to`).
|
|
46
|
+
- `shell` (OpenCode) and `bash` (pi): redirects (`>`, `>>`, `>|`), `tee`, and
|
|
47
|
+
`cp`/`mv` destinations. The parser handles `-t DIR`, existing destination
|
|
48
|
+
directories, `sed -i`/`gsed -i`, and `perl -i`.
|
|
49
|
+
- Wrapper prefixes: `sudo`, `doas`, `env`, `nice`, `timeout`, `stdbuf`, `command`,
|
|
50
|
+
`exec`, `nohup`, `time`, and `VAR=value`.
|
|
51
|
+
|
|
52
|
+
Relative paths resolve against the call's working directory. A literal `cd DIR`
|
|
53
|
+
sets the working directory for following shell commands.
|
|
54
|
+
|
|
55
|
+
The guard checks paths under `$HOME` and resolves symlink aliases. It allows writes
|
|
56
|
+
inside the chezmoi source directory.
|
|
57
|
+
|
|
58
|
+
The guard reads `chezmoi managed --path-style=absolute` and caches the result for five
|
|
59
|
+
seconds. It reloads after a source-directory write or a shell command that runs
|
|
60
|
+
chezmoi. Seed-once `create_` targets remain writable. The guard blocks other managed
|
|
61
|
+
targets and names their source paths.
|
|
62
|
+
|
|
63
|
+
The guard ignores variable writes beyond `$HOME`, shell strings, scripts, and unlisted tools.
|
|
64
|
+
|
|
65
|
+
The plugin registers the `chezmoi-editor` skill from
|
|
66
|
+
`skills/chezmoi-editor/SKILL.md` unless a skill with that id already exists. The
|
|
67
|
+
skill links to `references/conventions.md` for source-file naming prefixes.
|
|
@@ -0,0 +1,920 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// Generated by scripts/build.ts. Do not edit.
|
|
3
|
+
// plugins/chezmoi/pi-src/chezmoi.ts
|
|
4
|
+
import process from "process";
|
|
5
|
+
|
|
6
|
+
// plugins/chezmoi/shared/guard.ts
|
|
7
|
+
import { spawn as spawnProcess } from "child_process";
|
|
8
|
+
import { realpathSync } from "fs";
|
|
9
|
+
import { homedir as homedir2 } from "os";
|
|
10
|
+
import { basename as basename2 } from "path";
|
|
11
|
+
|
|
12
|
+
// lib/src/shell-write-targets.ts
|
|
13
|
+
import { statSync } from "fs";
|
|
14
|
+
import { homedir } from "os";
|
|
15
|
+
import { basename, isAbsolute, join, relative, resolve } from "path";
|
|
16
|
+
|
|
17
|
+
// lib/src/shell-tokenizer.ts
|
|
18
|
+
var SEPARATORS = new Set([";", "&", "|", "(", ")", `
|
|
19
|
+
`]);
|
|
20
|
+
function token(value, startsQuoted = false, sawQuote = false) {
|
|
21
|
+
return { value, startsQuoted, sawQuote };
|
|
22
|
+
}
|
|
23
|
+
function tokenizeShell(command, options = {}) {
|
|
24
|
+
if (typeof command !== "string")
|
|
25
|
+
return [];
|
|
26
|
+
const out = [];
|
|
27
|
+
let current = "";
|
|
28
|
+
let started = false;
|
|
29
|
+
let startsQuoted = false;
|
|
30
|
+
let sawQuote = false;
|
|
31
|
+
let quote = null;
|
|
32
|
+
const pending = [];
|
|
33
|
+
const flush = () => {
|
|
34
|
+
if (!started)
|
|
35
|
+
return;
|
|
36
|
+
out.push(token(current, startsQuoted, sawQuote));
|
|
37
|
+
current = "";
|
|
38
|
+
started = false;
|
|
39
|
+
startsQuoted = false;
|
|
40
|
+
sawQuote = false;
|
|
41
|
+
};
|
|
42
|
+
for (let i = 0;i < command.length; i++) {
|
|
43
|
+
const ch = command[i];
|
|
44
|
+
if (quote !== null) {
|
|
45
|
+
if (ch === quote) {
|
|
46
|
+
quote = null;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
current += ch;
|
|
50
|
+
started = true;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (ch === '"' || ch === "'") {
|
|
54
|
+
if (!started)
|
|
55
|
+
startsQuoted = true;
|
|
56
|
+
started = true;
|
|
57
|
+
sawQuote = true;
|
|
58
|
+
quote = ch;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (ch === "\\" && i + 1 < command.length) {
|
|
62
|
+
if (options.preserveBackslashes)
|
|
63
|
+
current += ch;
|
|
64
|
+
current += command[++i];
|
|
65
|
+
started = true;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (ch === "$" && command[i + 1] === "(") {
|
|
69
|
+
flush();
|
|
70
|
+
out.push(token("$("));
|
|
71
|
+
i++;
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (ch === "<" && command[i + 1] === "<" && command[i + 2] === "<") {
|
|
75
|
+
flush();
|
|
76
|
+
out.push(token("<<<"));
|
|
77
|
+
i += 2;
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (ch === "<" && command[i + 1] === "<") {
|
|
81
|
+
const operator = hereDocumentOperator(command, i);
|
|
82
|
+
if (operator !== null) {
|
|
83
|
+
flush();
|
|
84
|
+
pending.push(operator);
|
|
85
|
+
i = operator.end - 1;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (ch === `
|
|
90
|
+
`) {
|
|
91
|
+
flush();
|
|
92
|
+
out.push(token(ch));
|
|
93
|
+
let bodyStart = i + 1;
|
|
94
|
+
while (pending.length > 0) {
|
|
95
|
+
const document = pending.shift();
|
|
96
|
+
if (document === undefined)
|
|
97
|
+
break;
|
|
98
|
+
const body = hereDocumentBody(command, bodyStart, document);
|
|
99
|
+
if (!document.quoted) {
|
|
100
|
+
const source = command.slice(bodyStart, body.bodyEnd);
|
|
101
|
+
if (options.hereDocumentSubstitutionsOnly)
|
|
102
|
+
out.push(...substitutions(source, options));
|
|
103
|
+
else
|
|
104
|
+
out.push(...tokenizeShell(source, options));
|
|
105
|
+
}
|
|
106
|
+
i = body.terminatorEnd;
|
|
107
|
+
bodyStart = i + 1;
|
|
108
|
+
}
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (/\s/.test(ch)) {
|
|
112
|
+
flush();
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
if (SEPARATORS.has(ch)) {
|
|
116
|
+
flush();
|
|
117
|
+
out.push(token(ch));
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
current += ch;
|
|
121
|
+
started = true;
|
|
122
|
+
}
|
|
123
|
+
flush();
|
|
124
|
+
return out;
|
|
125
|
+
}
|
|
126
|
+
function hereDocumentOperator(command, start) {
|
|
127
|
+
let i = start + 2;
|
|
128
|
+
const stripTabs = command[i] === "-";
|
|
129
|
+
if (stripTabs)
|
|
130
|
+
i++;
|
|
131
|
+
while (command[i] === " " || command[i] === "\t")
|
|
132
|
+
i++;
|
|
133
|
+
let delimiter = "";
|
|
134
|
+
let quoted = false;
|
|
135
|
+
let quote = null;
|
|
136
|
+
while (i < command.length) {
|
|
137
|
+
const ch = command[i];
|
|
138
|
+
if (quote !== null) {
|
|
139
|
+
if (ch === quote) {
|
|
140
|
+
quote = null;
|
|
141
|
+
i++;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
delimiter += ch;
|
|
145
|
+
i++;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (ch === '"' || ch === "'") {
|
|
149
|
+
quoted = true;
|
|
150
|
+
quote = ch;
|
|
151
|
+
i++;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (ch === "\\" && i + 1 < command.length) {
|
|
155
|
+
quoted = true;
|
|
156
|
+
delimiter += command[i + 1];
|
|
157
|
+
i += 2;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (/\s|[;&|<>()]/.test(ch))
|
|
161
|
+
break;
|
|
162
|
+
delimiter += ch;
|
|
163
|
+
i++;
|
|
164
|
+
}
|
|
165
|
+
if (quote !== null || delimiter.length === 0)
|
|
166
|
+
return null;
|
|
167
|
+
return { delimiter, stripTabs, quoted, end: i };
|
|
168
|
+
}
|
|
169
|
+
function hereDocumentBody(command, from, document) {
|
|
170
|
+
let cursor = from;
|
|
171
|
+
while (cursor < command.length) {
|
|
172
|
+
let next = command.indexOf(`
|
|
173
|
+
`, cursor);
|
|
174
|
+
if (next === -1)
|
|
175
|
+
next = command.length;
|
|
176
|
+
let line = command.slice(cursor, next);
|
|
177
|
+
if (document.stripTabs)
|
|
178
|
+
line = line.replace(/^\t+/, "");
|
|
179
|
+
if (line === document.delimiter)
|
|
180
|
+
return { bodyEnd: cursor, terminatorEnd: next };
|
|
181
|
+
if (next === command.length)
|
|
182
|
+
break;
|
|
183
|
+
cursor = next + 1;
|
|
184
|
+
}
|
|
185
|
+
return { bodyEnd: command.length, terminatorEnd: command.length };
|
|
186
|
+
}
|
|
187
|
+
function substitutions(body, options) {
|
|
188
|
+
const out = [];
|
|
189
|
+
for (let i = 0;i < body.length; i++) {
|
|
190
|
+
const ch = body[i];
|
|
191
|
+
if (ch === "\\") {
|
|
192
|
+
i++;
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
let inner;
|
|
196
|
+
if (ch === "$" && body[i + 1] === "(") {
|
|
197
|
+
const end = substitutionEnd(body, i + 2);
|
|
198
|
+
inner = body.slice(i + 2, end);
|
|
199
|
+
i = end;
|
|
200
|
+
if (inner.startsWith("("))
|
|
201
|
+
continue;
|
|
202
|
+
} else if (ch === "`") {
|
|
203
|
+
let end = i + 1;
|
|
204
|
+
while (end < body.length && body[end] !== "`")
|
|
205
|
+
end += body[end] === "\\" ? 2 : 1;
|
|
206
|
+
inner = body.slice(i + 1, end);
|
|
207
|
+
i = end;
|
|
208
|
+
} else {
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
out.push(token("$("), ...tokenizeShell(inner, options), token(")"));
|
|
212
|
+
}
|
|
213
|
+
return out;
|
|
214
|
+
}
|
|
215
|
+
function substitutionEnd(body, from) {
|
|
216
|
+
let depth = 1;
|
|
217
|
+
let quote = null;
|
|
218
|
+
for (let i = from;i < body.length; i++) {
|
|
219
|
+
const ch = body[i];
|
|
220
|
+
if (quote !== null) {
|
|
221
|
+
if (ch === quote)
|
|
222
|
+
quote = null;
|
|
223
|
+
else if (ch === "\\" && quote === '"')
|
|
224
|
+
i++;
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
if (ch === "\\")
|
|
228
|
+
i++;
|
|
229
|
+
else if (ch === '"' || ch === "'")
|
|
230
|
+
quote = ch;
|
|
231
|
+
else if (ch === "(")
|
|
232
|
+
depth++;
|
|
233
|
+
else if (ch === ")" && --depth === 0)
|
|
234
|
+
return i;
|
|
235
|
+
}
|
|
236
|
+
return body.length;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// lib/src/command-words.ts
|
|
240
|
+
var NONE = new Set;
|
|
241
|
+
var WRAPPERS = new Map([
|
|
242
|
+
[
|
|
243
|
+
"sudo",
|
|
244
|
+
{
|
|
245
|
+
valued: new Set([
|
|
246
|
+
"-C",
|
|
247
|
+
"-D",
|
|
248
|
+
"-g",
|
|
249
|
+
"-h",
|
|
250
|
+
"-p",
|
|
251
|
+
"-R",
|
|
252
|
+
"-r",
|
|
253
|
+
"-T",
|
|
254
|
+
"-t",
|
|
255
|
+
"-U",
|
|
256
|
+
"-u",
|
|
257
|
+
"--chdir",
|
|
258
|
+
"--chroot",
|
|
259
|
+
"--close-from",
|
|
260
|
+
"--command-timeout",
|
|
261
|
+
"--group",
|
|
262
|
+
"--host",
|
|
263
|
+
"--other-user",
|
|
264
|
+
"--prompt",
|
|
265
|
+
"--role",
|
|
266
|
+
"--type",
|
|
267
|
+
"--user"
|
|
268
|
+
]),
|
|
269
|
+
chdir: new Set(["-D", "--chdir"])
|
|
270
|
+
}
|
|
271
|
+
],
|
|
272
|
+
["doas", { valued: new Set(["-C", "-u"]) }],
|
|
273
|
+
[
|
|
274
|
+
"env",
|
|
275
|
+
{
|
|
276
|
+
valued: new Set(["-C", "-L", "-P", "-S", "-U", "-u", "--chdir", "--split-string", "--unset"]),
|
|
277
|
+
chdir: new Set(["-C", "--chdir"]),
|
|
278
|
+
split: new Set(["-S", "--split-string"]),
|
|
279
|
+
assignments: true
|
|
280
|
+
}
|
|
281
|
+
],
|
|
282
|
+
["nice", { valued: new Set(["-n", "--adjustment"]) }],
|
|
283
|
+
["timeout", { valued: new Set(["-k", "-s", "--kill-after", "--signal"]), operands: 1 }],
|
|
284
|
+
["stdbuf", { valued: new Set(["-e", "-i", "-o", "--error", "--input", "--output"]) }],
|
|
285
|
+
["exec", { valued: new Set(["-a"]) }],
|
|
286
|
+
["command", { valued: NONE, lookup: new Set(["-V", "-v"]) }],
|
|
287
|
+
["time", { valued: new Set(["-f", "-o", "--format", "--output"]) }],
|
|
288
|
+
["nohup", { valued: NONE }]
|
|
289
|
+
]);
|
|
290
|
+
var ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*=/;
|
|
291
|
+
function readOption(words, at, wrapper) {
|
|
292
|
+
const word = words[at];
|
|
293
|
+
if (word.startsWith("--")) {
|
|
294
|
+
const eq = word.indexOf("=");
|
|
295
|
+
const name = eq === -1 ? word : word.slice(0, eq);
|
|
296
|
+
if (!wrapper.valued.has(name))
|
|
297
|
+
return null;
|
|
298
|
+
return eq === -1 ? { name, value: words[at + 1], span: 2 } : { name, value: word.slice(eq + 1), span: 1 };
|
|
299
|
+
}
|
|
300
|
+
for (let j = 1;j < word.length; j++) {
|
|
301
|
+
const name = `-${word[j]}`;
|
|
302
|
+
if (wrapper.lookup?.has(name))
|
|
303
|
+
return "lookup";
|
|
304
|
+
if (!wrapper.valued.has(name))
|
|
305
|
+
continue;
|
|
306
|
+
const attached = word.slice(j + 1);
|
|
307
|
+
return attached ? { name, value: attached, span: 1 } : { name, value: words[at + 1], span: 2 };
|
|
308
|
+
}
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
function commandWords(input) {
|
|
312
|
+
const { argv, directories } = stripWrappers(input);
|
|
313
|
+
return { argv, directories };
|
|
314
|
+
}
|
|
315
|
+
function stripWrappers(input) {
|
|
316
|
+
const words = [...input];
|
|
317
|
+
const directories = [];
|
|
318
|
+
const wrappers = [];
|
|
319
|
+
let i = 0;
|
|
320
|
+
while (i < words.length) {
|
|
321
|
+
const word = words[i];
|
|
322
|
+
if (ASSIGNMENT.test(word) || word === "--") {
|
|
323
|
+
i++;
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
const wrapper = WRAPPERS.get(word.slice(word.lastIndexOf("/") + 1));
|
|
327
|
+
if (wrapper === undefined)
|
|
328
|
+
break;
|
|
329
|
+
wrappers.push(word.slice(word.lastIndexOf("/") + 1));
|
|
330
|
+
i++;
|
|
331
|
+
while (i < words.length) {
|
|
332
|
+
const option = words[i];
|
|
333
|
+
if (option === "--") {
|
|
334
|
+
i++;
|
|
335
|
+
break;
|
|
336
|
+
}
|
|
337
|
+
if (wrapper.assignments && ASSIGNMENT.test(option)) {
|
|
338
|
+
i++;
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
if (!option.startsWith("-"))
|
|
342
|
+
break;
|
|
343
|
+
const read = readOption(words, i, wrapper);
|
|
344
|
+
if (read === "lookup")
|
|
345
|
+
return { argv: [], directories, wrappers };
|
|
346
|
+
if (read === null) {
|
|
347
|
+
i++;
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
if (read.value === undefined)
|
|
351
|
+
return { argv: [], directories, wrappers };
|
|
352
|
+
if (wrapper.split?.has(read.name)) {
|
|
353
|
+
words.splice(i, read.span, ...tokenizeShell(read.value).map((token) => token.value));
|
|
354
|
+
continue;
|
|
355
|
+
}
|
|
356
|
+
if (wrapper.chdir?.has(read.name))
|
|
357
|
+
directories.push(read.value);
|
|
358
|
+
i += read.span;
|
|
359
|
+
}
|
|
360
|
+
i += Math.min(wrapper.operands ?? 0, words.length - i);
|
|
361
|
+
}
|
|
362
|
+
return { argv: words.slice(i), directories, wrappers };
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// lib/src/shell-write-targets.ts
|
|
366
|
+
var SEPARATORS2 = {
|
|
367
|
+
";": true,
|
|
368
|
+
"&": true,
|
|
369
|
+
"|": true,
|
|
370
|
+
"\n": true,
|
|
371
|
+
"(": true,
|
|
372
|
+
")": true,
|
|
373
|
+
"$(": true
|
|
374
|
+
};
|
|
375
|
+
function lexicalAbs(target, cwd) {
|
|
376
|
+
let expanded = target.startsWith("~") ? homedir() + target.slice(1) : target;
|
|
377
|
+
if (!isAbsolute(expanded))
|
|
378
|
+
expanded = resolve(cwd, expanded);
|
|
379
|
+
const parts = [];
|
|
380
|
+
for (const segment of expanded.split(/[/\\]/)) {
|
|
381
|
+
if (segment === "" || segment === ".")
|
|
382
|
+
continue;
|
|
383
|
+
if (segment === "..") {
|
|
384
|
+
parts.pop();
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
parts.push(segment);
|
|
388
|
+
}
|
|
389
|
+
return `/${parts.join("/")}`;
|
|
390
|
+
}
|
|
391
|
+
function under(child, parent) {
|
|
392
|
+
if (!parent)
|
|
393
|
+
return false;
|
|
394
|
+
const rel = relative(parent, child);
|
|
395
|
+
return rel === "" || !rel.startsWith("..") && !isAbsolute(rel);
|
|
396
|
+
}
|
|
397
|
+
function teePaths(args) {
|
|
398
|
+
const at = args.indexOf("--");
|
|
399
|
+
const options = at === -1 ? args : args.slice(0, at);
|
|
400
|
+
const rest = at === -1 ? [] : args.slice(at + 1);
|
|
401
|
+
return [...options.filter((arg) => !arg.startsWith("-") || arg === "-"), ...rest].filter((arg) => arg !== "-");
|
|
402
|
+
}
|
|
403
|
+
function copyTargets(args) {
|
|
404
|
+
const positional = [];
|
|
405
|
+
let targetDir;
|
|
406
|
+
let noTargetDir = false;
|
|
407
|
+
for (let i = 0;i < args.length; i++) {
|
|
408
|
+
const arg = args[i];
|
|
409
|
+
if (arg === "--") {
|
|
410
|
+
positional.push(...args.slice(i + 1));
|
|
411
|
+
break;
|
|
412
|
+
}
|
|
413
|
+
if (arg === "-t" || arg === "--target-directory")
|
|
414
|
+
targetDir = args[++i];
|
|
415
|
+
else if (arg.startsWith("--target-directory="))
|
|
416
|
+
targetDir = arg.slice("--target-directory=".length);
|
|
417
|
+
else if (/^-t./.test(arg))
|
|
418
|
+
targetDir = arg.slice(2);
|
|
419
|
+
else if (arg === "-T" || arg === "--no-target-directory")
|
|
420
|
+
noTargetDir = true;
|
|
421
|
+
else if (arg === "-S")
|
|
422
|
+
i++;
|
|
423
|
+
else if (!arg.startsWith("-") || arg === "-")
|
|
424
|
+
positional.push(arg);
|
|
425
|
+
}
|
|
426
|
+
if (targetDir)
|
|
427
|
+
return [{ path: targetDir, sources: positional, intoDir: true }];
|
|
428
|
+
if (positional.length < 2)
|
|
429
|
+
return [];
|
|
430
|
+
const dest = positional[positional.length - 1];
|
|
431
|
+
return [noTargetDir ? { path: dest } : { path: dest, sources: positional.slice(0, -1) }];
|
|
432
|
+
}
|
|
433
|
+
function sedPaths(args) {
|
|
434
|
+
let inplace = false;
|
|
435
|
+
let script = false;
|
|
436
|
+
const paths = [];
|
|
437
|
+
for (let i = 0;i < args.length; i++) {
|
|
438
|
+
const token = args[i];
|
|
439
|
+
if (token === "--") {
|
|
440
|
+
paths.push(...args.slice(i + 1 + (script ? 0 : 1)));
|
|
441
|
+
break;
|
|
442
|
+
}
|
|
443
|
+
if (token === "-e" || token === "-f" || token === "--expression" || token === "--file") {
|
|
444
|
+
script = true;
|
|
445
|
+
i++;
|
|
446
|
+
} else if (/^--(?:expression|file)=/.test(token) || /^-[ef].+/.test(token)) {
|
|
447
|
+
script = true;
|
|
448
|
+
} else if (/^--in-place(?:=|$)/.test(token) || /^-[a-zA-Z]*i/.test(token)) {
|
|
449
|
+
inplace = true;
|
|
450
|
+
if (token === "-i" && args[i + 1] === "")
|
|
451
|
+
i++;
|
|
452
|
+
} else if (!token.startsWith("-")) {
|
|
453
|
+
if (!script)
|
|
454
|
+
script = true;
|
|
455
|
+
else
|
|
456
|
+
paths.push(token);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
return inplace ? paths : [];
|
|
460
|
+
}
|
|
461
|
+
function perlPaths(args) {
|
|
462
|
+
let inplace = false;
|
|
463
|
+
let script = false;
|
|
464
|
+
let positional = false;
|
|
465
|
+
const files = [];
|
|
466
|
+
for (let i = 0;i < args.length; i++) {
|
|
467
|
+
const arg = args[i];
|
|
468
|
+
if (!positional && arg === "--") {
|
|
469
|
+
positional = true;
|
|
470
|
+
continue;
|
|
471
|
+
}
|
|
472
|
+
if (!positional && arg.startsWith("-") && arg.length > 1) {
|
|
473
|
+
for (let j = 1;j < arg.length; j++) {
|
|
474
|
+
const flag = arg[j];
|
|
475
|
+
if (flag === "e" || flag === "E") {
|
|
476
|
+
script = true;
|
|
477
|
+
if (j === arg.length - 1)
|
|
478
|
+
i++;
|
|
479
|
+
break;
|
|
480
|
+
}
|
|
481
|
+
if (flag === "i") {
|
|
482
|
+
inplace = true;
|
|
483
|
+
break;
|
|
484
|
+
}
|
|
485
|
+
if ("MmIdDxlo0C".includes(flag))
|
|
486
|
+
break;
|
|
487
|
+
}
|
|
488
|
+
continue;
|
|
489
|
+
}
|
|
490
|
+
positional = true;
|
|
491
|
+
if (!script)
|
|
492
|
+
script = true;
|
|
493
|
+
else
|
|
494
|
+
files.push(arg);
|
|
495
|
+
}
|
|
496
|
+
return inplace ? files : [];
|
|
497
|
+
}
|
|
498
|
+
function simpleCommandTargets(words, normalizeWords) {
|
|
499
|
+
const redirects = [];
|
|
500
|
+
const args = [];
|
|
501
|
+
for (let i = 0;i < words.length; i++) {
|
|
502
|
+
const word = words[i];
|
|
503
|
+
const value = word.value;
|
|
504
|
+
if (word.startsQuoted) {
|
|
505
|
+
args.push(value);
|
|
506
|
+
continue;
|
|
507
|
+
}
|
|
508
|
+
if (value.startsWith("<")) {
|
|
509
|
+
if (value === "<" || value === "<<<")
|
|
510
|
+
i++;
|
|
511
|
+
continue;
|
|
512
|
+
}
|
|
513
|
+
const at = value.indexOf(">");
|
|
514
|
+
if (at === -1) {
|
|
515
|
+
args.push(value);
|
|
516
|
+
continue;
|
|
517
|
+
}
|
|
518
|
+
const before = value.slice(0, at);
|
|
519
|
+
if (before && !/^\d+$/.test(before))
|
|
520
|
+
args.push(before);
|
|
521
|
+
let target = value.slice(at + 1);
|
|
522
|
+
if (target.startsWith(">") || target.startsWith("|"))
|
|
523
|
+
target = target.slice(1);
|
|
524
|
+
if (!target)
|
|
525
|
+
target = words[++i]?.value ?? "";
|
|
526
|
+
if (target)
|
|
527
|
+
redirects.push({ path: target });
|
|
528
|
+
}
|
|
529
|
+
const { argv, directories } = commandWords(normalizeWords?.(args) ?? args);
|
|
530
|
+
const name = basename(argv[0] ?? "");
|
|
531
|
+
const rest = argv.slice(1);
|
|
532
|
+
const writes = [];
|
|
533
|
+
if (name === "tee")
|
|
534
|
+
writes.push(...teePaths(rest).map((path) => ({ path })));
|
|
535
|
+
else if (name === "cp" || name === "mv")
|
|
536
|
+
writes.push(...copyTargets(rest));
|
|
537
|
+
else if (name === "sed" || name === "gsed")
|
|
538
|
+
writes.push(...sedPaths(rest).map((path) => ({ path })));
|
|
539
|
+
else if (name === "perl")
|
|
540
|
+
writes.push(...perlPaths(rest).map((path) => ({ path })));
|
|
541
|
+
return { argv, directories, redirects, writes };
|
|
542
|
+
}
|
|
543
|
+
function literalPath(raw, cwd) {
|
|
544
|
+
const path = raw.replace(/^\$\{?HOME\}?(?=\/|$)/, homedir());
|
|
545
|
+
if (/[$`]/.test(path))
|
|
546
|
+
return;
|
|
547
|
+
if (path.startsWith("~") || isAbsolute(path))
|
|
548
|
+
return lexicalAbs(path, "/");
|
|
549
|
+
return cwd === null ? undefined : lexicalAbs(path, cwd);
|
|
550
|
+
}
|
|
551
|
+
function isDirectory(path) {
|
|
552
|
+
try {
|
|
553
|
+
return statSync(path).isDirectory();
|
|
554
|
+
} catch {
|
|
555
|
+
return false;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
function cdTarget(args, cwd) {
|
|
559
|
+
const dirs = args.filter((arg) => !/^-[LPe@]+$/.test(arg));
|
|
560
|
+
if (dirs.length === 0)
|
|
561
|
+
return homedir();
|
|
562
|
+
const dir = dirs[0];
|
|
563
|
+
if (dir === "-")
|
|
564
|
+
return null;
|
|
565
|
+
return literalPath(dir, cwd) ?? null;
|
|
566
|
+
}
|
|
567
|
+
function shellWriteTargets(command, cwd, additionalWrites, normalizeWords) {
|
|
568
|
+
const out = [];
|
|
569
|
+
let dir = cwd;
|
|
570
|
+
const saved = [];
|
|
571
|
+
let words = [];
|
|
572
|
+
const resolveInto = (targets, from) => {
|
|
573
|
+
for (const target of targets) {
|
|
574
|
+
const abs = literalPath(target.path, from);
|
|
575
|
+
if (abs === undefined)
|
|
576
|
+
continue;
|
|
577
|
+
const intoDir = target.intoDir || target.sources && (target.path.endsWith("/") || isDirectory(abs));
|
|
578
|
+
if (intoDir && target.sources) {
|
|
579
|
+
for (const source of target.sources)
|
|
580
|
+
out.push(join(abs, basename(source)));
|
|
581
|
+
} else {
|
|
582
|
+
out.push(abs);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
};
|
|
586
|
+
const finish = () => {
|
|
587
|
+
const segment = words;
|
|
588
|
+
words = [];
|
|
589
|
+
if (segment.length === 0)
|
|
590
|
+
return;
|
|
591
|
+
const { argv, directories, redirects, writes } = simpleCommandTargets(segment, normalizeWords);
|
|
592
|
+
resolveInto(redirects, dir);
|
|
593
|
+
if (argv[0] === "cd") {
|
|
594
|
+
dir = cdTarget(argv.slice(1), dir);
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
let commandDir = dir;
|
|
598
|
+
for (const directory of directories)
|
|
599
|
+
commandDir = literalPath(directory, commandDir) ?? null;
|
|
600
|
+
writes.push(...(additionalWrites?.(argv, commandDir) ?? []).map((path) => ({ path })));
|
|
601
|
+
resolveInto(writes, commandDir);
|
|
602
|
+
};
|
|
603
|
+
for (const token of tokenizeShell(command, { hereDocumentSubstitutionsOnly: true })) {
|
|
604
|
+
if (token.sawQuote || !Object.hasOwn(SEPARATORS2, token.value)) {
|
|
605
|
+
words.push(token);
|
|
606
|
+
continue;
|
|
607
|
+
}
|
|
608
|
+
finish();
|
|
609
|
+
if (token.value === "(" || token.value === "$(")
|
|
610
|
+
saved.push(dir);
|
|
611
|
+
else if (token.value === ")" && saved.length > 0)
|
|
612
|
+
dir = saved.pop() ?? null;
|
|
613
|
+
}
|
|
614
|
+
finish();
|
|
615
|
+
return [...new Set(out)];
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
// lib/src/tool-targets.ts
|
|
619
|
+
var WRITE_TOOLS = new Set(["edit", "write", "patch"]);
|
|
620
|
+
var READ_TOOLS = new Set(["read"]);
|
|
621
|
+
var SHELL_TOOL = "shell";
|
|
622
|
+
var SHELL_ALIASES = new Set(["bash", "Bash"]);
|
|
623
|
+
function isShellTool(tool) {
|
|
624
|
+
return tool === SHELL_TOOL || SHELL_ALIASES.has(tool);
|
|
625
|
+
}
|
|
626
|
+
var SHELL_EXECUTABLES = new Set(["bash", "sh", "zsh", "dash", "ksh"]);
|
|
627
|
+
var SAFE_WORD = /^[A-Za-z0-9_@%+=:,./-]+$/;
|
|
628
|
+
function quoteWord(word) {
|
|
629
|
+
return SAFE_WORD.test(word) ? word : `'${word.replaceAll("'", `'\\''`)}'`;
|
|
630
|
+
}
|
|
631
|
+
function commandText(value) {
|
|
632
|
+
if (typeof value === "string")
|
|
633
|
+
return value;
|
|
634
|
+
if (!Array.isArray(value) || value.length === 0 || !value.every((word) => typeof word === "string"))
|
|
635
|
+
return;
|
|
636
|
+
const argv = value;
|
|
637
|
+
const executable = argv[0]?.split("/").pop() ?? "";
|
|
638
|
+
if (argv.length === 3 && SHELL_EXECUTABLES.has(executable) && /^-l?c$/.test(argv[1] ?? ""))
|
|
639
|
+
return argv[2];
|
|
640
|
+
return argv.map(quoteWord).join(" ");
|
|
641
|
+
}
|
|
642
|
+
function flag(value) {
|
|
643
|
+
if (typeof value === "boolean")
|
|
644
|
+
return value;
|
|
645
|
+
if (value === "true")
|
|
646
|
+
return true;
|
|
647
|
+
if (value === "false")
|
|
648
|
+
return false;
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
function milliseconds(value) {
|
|
652
|
+
const parsed = typeof value === "string" && value.trim() !== "" ? Number(value) : value;
|
|
653
|
+
return typeof parsed === "number" && Number.isSafeInteger(parsed) && parsed >= 0 ? parsed : undefined;
|
|
654
|
+
}
|
|
655
|
+
function shellAliasInput(input) {
|
|
656
|
+
let raw = input;
|
|
657
|
+
if (typeof raw === "string") {
|
|
658
|
+
try {
|
|
659
|
+
raw = JSON.parse(raw);
|
|
660
|
+
} catch {
|
|
661
|
+
return input;
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
const value = fields(raw);
|
|
665
|
+
const command = commandText(value.command ?? value.cmd);
|
|
666
|
+
if (command === undefined)
|
|
667
|
+
return input;
|
|
668
|
+
const out = { command };
|
|
669
|
+
const workdir = [value.workdir, value.cwd, value.working_directory].find((dir) => typeof dir === "string" && dir !== "");
|
|
670
|
+
if (workdir !== undefined)
|
|
671
|
+
out.workdir = workdir;
|
|
672
|
+
const timeout = milliseconds(value.timeout ?? value.timeout_ms);
|
|
673
|
+
if (timeout !== undefined)
|
|
674
|
+
out.timeout = timeout;
|
|
675
|
+
const background = flag(value.background ?? value.run_in_background);
|
|
676
|
+
if (background !== undefined)
|
|
677
|
+
out.background = background;
|
|
678
|
+
return out;
|
|
679
|
+
}
|
|
680
|
+
var PATCH_HEADER = /^\*\*\* (?:(?<verb>Add|Update|Delete) File|(?<move>Move to)):\s*(?<path>.+?)\s*$/;
|
|
681
|
+
function unquote(path) {
|
|
682
|
+
const first = path[0];
|
|
683
|
+
if (path.length > 1 && (first === '"' || first === "'") && path.endsWith(first))
|
|
684
|
+
return path.slice(1, -1);
|
|
685
|
+
return path;
|
|
686
|
+
}
|
|
687
|
+
function fields(input) {
|
|
688
|
+
return input !== null && typeof input === "object" ? input : {};
|
|
689
|
+
}
|
|
690
|
+
function patchTargets(patchText) {
|
|
691
|
+
const out = [];
|
|
692
|
+
for (const raw of patchText.split(`
|
|
693
|
+
`)) {
|
|
694
|
+
const match = PATCH_HEADER.exec(raw.replace(/\r$/, ""));
|
|
695
|
+
const path = match?.groups?.path;
|
|
696
|
+
if (match === null || path === undefined)
|
|
697
|
+
continue;
|
|
698
|
+
const operation = match.groups?.move ? "move" : match.groups?.verb?.toLowerCase();
|
|
699
|
+
const target = unquote(path);
|
|
700
|
+
if (target.length > 0)
|
|
701
|
+
out.push({ operation, path: target });
|
|
702
|
+
}
|
|
703
|
+
return out;
|
|
704
|
+
}
|
|
705
|
+
function patchPaths(patchText) {
|
|
706
|
+
return [...new Set(patchTargets(patchText).map((target) => target.path))];
|
|
707
|
+
}
|
|
708
|
+
function writeTargets(tool, input) {
|
|
709
|
+
const value = fields(input);
|
|
710
|
+
if (tool === "edit" || tool === "write")
|
|
711
|
+
return typeof value.path === "string" && value.path ? [value.path] : [];
|
|
712
|
+
if (tool === "patch")
|
|
713
|
+
return typeof value.patchText === "string" ? patchPaths(value.patchText) : [];
|
|
714
|
+
return [];
|
|
715
|
+
}
|
|
716
|
+
function shellCall(tool, input) {
|
|
717
|
+
if (!isShellTool(tool))
|
|
718
|
+
return;
|
|
719
|
+
const value = fields(tool === SHELL_TOOL ? input : shellAliasInput(input));
|
|
720
|
+
if (typeof value.command !== "string")
|
|
721
|
+
return;
|
|
722
|
+
const call = { command: value.command };
|
|
723
|
+
if (typeof value.workdir === "string" && value.workdir)
|
|
724
|
+
call.workdir = value.workdir;
|
|
725
|
+
if (typeof value.timeout === "number")
|
|
726
|
+
call.timeout = value.timeout;
|
|
727
|
+
if (typeof value.background === "boolean")
|
|
728
|
+
call.background = value.background;
|
|
729
|
+
return call;
|
|
730
|
+
}
|
|
731
|
+
function writtenPaths(tool, input, cwd) {
|
|
732
|
+
const shell = shellCall(tool, input);
|
|
733
|
+
if (shell !== undefined)
|
|
734
|
+
return shellWriteTargets(shell.command, shell.workdir ? lexicalAbs(shell.workdir, cwd) : cwd);
|
|
735
|
+
return [...new Set(writeTargets(tool, input).map((path) => lexicalAbs(path, cwd)))];
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
// plugins/chezmoi/shared/guard.ts
|
|
739
|
+
var SUBPROCESS_TIMEOUT_MS = 2000;
|
|
740
|
+
var MANAGED_TTL_MS = 5000;
|
|
741
|
+
var ATTRIBUTE_PREFIXES = new Set([
|
|
742
|
+
"encrypted",
|
|
743
|
+
"private",
|
|
744
|
+
"readonly",
|
|
745
|
+
"empty",
|
|
746
|
+
"executable",
|
|
747
|
+
"exact",
|
|
748
|
+
"external",
|
|
749
|
+
"create",
|
|
750
|
+
"modify",
|
|
751
|
+
"remove",
|
|
752
|
+
"symlink",
|
|
753
|
+
"run",
|
|
754
|
+
"once",
|
|
755
|
+
"onchange",
|
|
756
|
+
"before",
|
|
757
|
+
"after"
|
|
758
|
+
]);
|
|
759
|
+
function isSeedOnceSource(source) {
|
|
760
|
+
let name = basename2(source);
|
|
761
|
+
for (;; ) {
|
|
762
|
+
const cut = name.indexOf("_");
|
|
763
|
+
if (cut <= 0 || !ATTRIBUTE_PREFIXES.has(name.slice(0, cut)))
|
|
764
|
+
return false;
|
|
765
|
+
if (name.slice(0, cut) === "create")
|
|
766
|
+
return true;
|
|
767
|
+
name = name.slice(cut + 1);
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
var CHEZMOI_RUN = /(?:^|[\s;&|(/])chezmoi(?:\s|$)/;
|
|
771
|
+
var spawnChezmoi = (args, globalArgs = []) => new Promise((resolve) => {
|
|
772
|
+
let stdout = "";
|
|
773
|
+
let settled = false;
|
|
774
|
+
const child = spawnProcess("chezmoi", [...globalArgs, ...args], { stdio: ["ignore", "pipe", "ignore"] });
|
|
775
|
+
const timer = setTimeout(() => child.kill("SIGKILL"), SUBPROCESS_TIMEOUT_MS);
|
|
776
|
+
child.stdout.setEncoding("utf8");
|
|
777
|
+
child.stdout.on("data", (chunk) => {
|
|
778
|
+
stdout += chunk;
|
|
779
|
+
});
|
|
780
|
+
child.on("error", () => {
|
|
781
|
+
if (settled)
|
|
782
|
+
return;
|
|
783
|
+
settled = true;
|
|
784
|
+
clearTimeout(timer);
|
|
785
|
+
resolve(null);
|
|
786
|
+
});
|
|
787
|
+
child.on("close", (code) => {
|
|
788
|
+
if (settled)
|
|
789
|
+
return;
|
|
790
|
+
settled = true;
|
|
791
|
+
clearTimeout(timer);
|
|
792
|
+
resolve(code === 0 ? stdout : null);
|
|
793
|
+
});
|
|
794
|
+
});
|
|
795
|
+
function normalized(path) {
|
|
796
|
+
try {
|
|
797
|
+
return realpathSync(path);
|
|
798
|
+
} catch {
|
|
799
|
+
return path;
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
function refusal(target, source) {
|
|
803
|
+
const hint = source ? `'${source}'` : `the path \`chezmoi source-path ${target}\` prints`;
|
|
804
|
+
return `'${target}' is a chezmoi-managed target, not the source; the next chezmoi apply overwrites it. ` + `Edit the source at ${hint}, then run chezmoi apply. Do not write the live home-directory copy.`;
|
|
805
|
+
}
|
|
806
|
+
function createGuard(options = {}) {
|
|
807
|
+
const spawn = options.spawn ?? spawnChezmoi;
|
|
808
|
+
const now = options.now ?? Date.now;
|
|
809
|
+
const home = options.home ?? homedir2();
|
|
810
|
+
const ttl = options.ttlMs ?? MANAGED_TTL_MS;
|
|
811
|
+
let managed = null;
|
|
812
|
+
let aliases = null;
|
|
813
|
+
let managedAt = 0;
|
|
814
|
+
let sourceDir;
|
|
815
|
+
let unavailableNotified = false;
|
|
816
|
+
const notifyUnavailable = () => {
|
|
817
|
+
if (unavailableNotified)
|
|
818
|
+
return;
|
|
819
|
+
unavailableNotified = true;
|
|
820
|
+
options.onUnavailable?.();
|
|
821
|
+
};
|
|
822
|
+
const invalidate = () => {
|
|
823
|
+
managed = null;
|
|
824
|
+
aliases = null;
|
|
825
|
+
};
|
|
826
|
+
const loadSourceDir = async () => {
|
|
827
|
+
if (sourceDir !== undefined)
|
|
828
|
+
return sourceDir;
|
|
829
|
+
const out = await spawn(["source-path"], options.globalArgs);
|
|
830
|
+
if (out === null)
|
|
831
|
+
notifyUnavailable();
|
|
832
|
+
sourceDir = out?.trim() || null;
|
|
833
|
+
return sourceDir;
|
|
834
|
+
};
|
|
835
|
+
const loadManaged = async () => {
|
|
836
|
+
if (managed && now() - managedAt < ttl)
|
|
837
|
+
return managed;
|
|
838
|
+
const out = await spawn(["managed", "--path-style=absolute"], options.globalArgs);
|
|
839
|
+
if (out === null) {
|
|
840
|
+
notifyUnavailable();
|
|
841
|
+
return null;
|
|
842
|
+
}
|
|
843
|
+
managed = new Set(out.split(`
|
|
844
|
+
`).map((line) => line.trim()).filter(Boolean));
|
|
845
|
+
aliases = null;
|
|
846
|
+
managedAt = now();
|
|
847
|
+
return managed;
|
|
848
|
+
};
|
|
849
|
+
const managedTarget = (set, abs) => {
|
|
850
|
+
if (set.has(abs))
|
|
851
|
+
return abs;
|
|
852
|
+
aliases ??= new Map([...set].map((path) => [normalized(path), path]));
|
|
853
|
+
return aliases.get(normalized(abs));
|
|
854
|
+
};
|
|
855
|
+
const considerPath = async (abs) => {
|
|
856
|
+
if (!under(abs, home) && !under(normalized(abs), normalized(home)))
|
|
857
|
+
return;
|
|
858
|
+
const source = await loadSourceDir();
|
|
859
|
+
if (source && (under(abs, source) || under(normalized(abs), normalized(source)))) {
|
|
860
|
+
invalidate();
|
|
861
|
+
return;
|
|
862
|
+
}
|
|
863
|
+
const set = await loadManaged();
|
|
864
|
+
if (!set)
|
|
865
|
+
return;
|
|
866
|
+
const target = managedTarget(set, abs);
|
|
867
|
+
if (!target)
|
|
868
|
+
return;
|
|
869
|
+
const targetSource = (await spawn(["source-path", target], options.globalArgs))?.trim() || undefined;
|
|
870
|
+
if (targetSource && isSeedOnceSource(targetSource))
|
|
871
|
+
return;
|
|
872
|
+
return refusal(abs, targetSource);
|
|
873
|
+
};
|
|
874
|
+
return {
|
|
875
|
+
invalidate,
|
|
876
|
+
decide: async (tool, input, cwd) => {
|
|
877
|
+
const shell = shellCall(tool, input);
|
|
878
|
+
if (shell && CHEZMOI_RUN.test(shell.command))
|
|
879
|
+
invalidate();
|
|
880
|
+
for (const abs of writtenPaths(tool, input, cwd)) {
|
|
881
|
+
const reason = await considerPath(abs);
|
|
882
|
+
if (reason)
|
|
883
|
+
return reason;
|
|
884
|
+
}
|
|
885
|
+
return;
|
|
886
|
+
}
|
|
887
|
+
};
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
// plugins/chezmoi/pi-src/chezmoi.ts
|
|
891
|
+
var UNAVAILABLE_NOTICE = "chezmoi: unable to read the managed file list; writes are allowed until chezmoi is available.";
|
|
892
|
+
function createExtension(pi, options = {}) {
|
|
893
|
+
let context;
|
|
894
|
+
const guard = createGuard({
|
|
895
|
+
...options,
|
|
896
|
+
onUnavailable: () => {
|
|
897
|
+
options.onUnavailable?.();
|
|
898
|
+
if (context?.hasUI)
|
|
899
|
+
context.ui.notify(UNAVAILABLE_NOTICE, "warning");
|
|
900
|
+
else
|
|
901
|
+
process.stderr.write(`${UNAVAILABLE_NOTICE}
|
|
902
|
+
`);
|
|
903
|
+
}
|
|
904
|
+
});
|
|
905
|
+
pi.on("tool_call", async (event, ctx) => {
|
|
906
|
+
if (event.toolName !== "write" && event.toolName !== "edit" && event.toolName !== "bash")
|
|
907
|
+
return;
|
|
908
|
+
context = ctx;
|
|
909
|
+
const reason = await guard.decide(event.toolName, event.input, ctx.cwd);
|
|
910
|
+
if (reason)
|
|
911
|
+
return { block: true, reason };
|
|
912
|
+
});
|
|
913
|
+
}
|
|
914
|
+
function chezmoi_default(pi) {
|
|
915
|
+
createExtension(pi);
|
|
916
|
+
}
|
|
917
|
+
export {
|
|
918
|
+
createExtension,
|
|
919
|
+
chezmoi_default as default
|
|
920
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@srobroek/pi-chezmoi-editor",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"description": "Refuse edits and shell writes to chezmoi-managed live files, naming the source to edit, plus the chezmoi-editor skill.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"pi-package"
|
|
7
|
+
],
|
|
8
|
+
"license": "Apache-2.0",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/srobroek/harness-plugins.git",
|
|
12
|
+
"directory": "plugins/chezmoi/pi"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"CHANGELOG.md",
|
|
16
|
+
"LICENSE",
|
|
17
|
+
"README.md",
|
|
18
|
+
"extensions",
|
|
19
|
+
"skills"
|
|
20
|
+
],
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public",
|
|
23
|
+
"provenance": true
|
|
24
|
+
},
|
|
25
|
+
"pi": {
|
|
26
|
+
"extensions": [
|
|
27
|
+
"./extensions/chezmoi.js"
|
|
28
|
+
],
|
|
29
|
+
"skills": [
|
|
30
|
+
"./skills"
|
|
31
|
+
]
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: chezmoi-editor
|
|
3
|
+
description: Edits chezmoi-managed dotfiles at their source; use when changing dotfiles, templates, or global agent and tool config.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Chezmoi Editor
|
|
7
|
+
|
|
8
|
+
Use this skill when a task changes files managed by chezmoi. Resolve the managed
|
|
9
|
+
source first; do not edit the rendered live target of a continuously managed file as
|
|
10
|
+
the durable fix (live-owned targets: see Rules).
|
|
11
|
+
|
|
12
|
+
In OpenCode, the chezmoi plugin refuses `edit`, `write` and `patch` calls that
|
|
13
|
+
target a managed file, and common literal shell writes (`>`/`>>` redirects, `tee`,
|
|
14
|
+
`cp`/`mv`, `sed -i`, `perl -i`). The refusal names the source path to edit. It reads
|
|
15
|
+
literal words only: writes through `sh -c`, the shell eval builtin, scripts, variables other than
|
|
16
|
+
`$HOME`, or other tools pass unchecked, so resolve the source yourself.
|
|
17
|
+
|
|
18
|
+
## Workflow
|
|
19
|
+
|
|
20
|
+
1. Determine whether the target is managed:
|
|
21
|
+
- `chezmoi managed`
|
|
22
|
+
- `chezmoi source-path <target>` when a specific target is known
|
|
23
|
+
- existing symlink/source layout when chezmoi cannot resolve it directly
|
|
24
|
+
2. Edit the source under the chezmoi source tree, not `$HOME` runtime output, unless
|
|
25
|
+
the target is live-owned (see Rules).
|
|
26
|
+
3. Use native chezmoi names for dotfiles, executables, private files, readonly
|
|
27
|
+
files, symlinks, scripts, and templates.
|
|
28
|
+
4. Never write a credential in plaintext. Read it at apply time with
|
|
29
|
+
`{{ onepasswordRead "op://<vault>/<item>/<field>" }}` in a `.tmpl` file, or
|
|
30
|
+
store the file with `chezmoi add --encrypt <target>`. `private_` only sets
|
|
31
|
+
`0600` permissions; the file stays plaintext in git.
|
|
32
|
+
5. Preview with `chezmoi diff`. Apply only when the source diff is correct and
|
|
33
|
+
the user wants the live target updated now.
|
|
34
|
+
|
|
35
|
+
## Rules
|
|
36
|
+
|
|
37
|
+
- Treat the chezmoi source directory as the source of truth.
|
|
38
|
+
- Prefer native chezmoi patterns over ad hoc symlink or copy schemes.
|
|
39
|
+
- If a live target changed outside chezmoi, reconcile it back into source
|
|
40
|
+
instead of patching around the generated copy.
|
|
41
|
+
- A one-off change to hosts that already exist, such as adding or removing one entry
|
|
42
|
+
or fixing one live key, gets no reconcile machinery: no `run_once_` or
|
|
43
|
+
`run_onchange_` script, retirement list entry, or declared key that only exists so a
|
|
44
|
+
reconcile script carries it. This applies only to live-owned targets: seed-once
|
|
45
|
+
`create_` files, and files that a reconcile script or the user owns on the host after
|
|
46
|
+
seeding. Edit those live files on each affected host directly, and change the source
|
|
47
|
+
only where newly seeded hosts should differ. Reconcile code is for state that must
|
|
48
|
+
keep converging on every apply.
|
|
49
|
+
- Continuously managed files and templates are not live-owned. A host-specific change to
|
|
50
|
+
one still goes in the source (use the template's host conditionals); a live edit is
|
|
51
|
+
refused by the OpenCode guard and overwritten by the next `chezmoi apply`.
|
|
52
|
+
- Temporary runtime experiments are allowed only when the user explicitly asks
|
|
53
|
+
for them; record how to promote the result into source.
|
|
54
|
+
- Resolve locations only through `chezmoi source-path` and `chezmoi managed`.
|
|
55
|
+
Do not assume a source-tree path, home-directory layout, or username.
|
|
56
|
+
|
|
57
|
+
## Scripts
|
|
58
|
+
|
|
59
|
+
- Status and diff: `chezmoi status`, `chezmoi diff`.
|
|
60
|
+
|
|
61
|
+
## References
|
|
62
|
+
|
|
63
|
+
- Read `references/conventions.md` in this skill's directory when choosing file
|
|
64
|
+
naming prefixes.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Chezmoi Conventions
|
|
2
|
+
|
|
3
|
+
## Source-file name prefixes
|
|
4
|
+
|
|
5
|
+
Attributes stack in a fixed order (for example `private_executable_dot_foo` or
|
|
6
|
+
`run_once_before_install.sh.tmpl`); `chezmoi add` writes them for you.
|
|
7
|
+
|
|
8
|
+
| Prefix / suffix | Meaning |
|
|
9
|
+
|---|---|
|
|
10
|
+
| `dot_` | target name starts with `.` |
|
|
11
|
+
| `private_` | `0600` file or `0700` directory; permissions only, not encryption |
|
|
12
|
+
| `readonly_` | write permission removed |
|
|
13
|
+
| `executable_` | executable file |
|
|
14
|
+
| `empty_` | keep the file even when it is empty |
|
|
15
|
+
| `encrypted_` | encrypted in source; written by `chezmoi add --encrypt` |
|
|
16
|
+
| `create_` | write the target only when it does not exist yet |
|
|
17
|
+
| `modify_` | script that receives the current target on stdin and prints the new contents |
|
|
18
|
+
| `remove_` | remove the target |
|
|
19
|
+
| `symlink_` | target is a symlink; file contents are the link destination |
|
|
20
|
+
| `exact_` | directory: remove target entries not present in source |
|
|
21
|
+
| `external_` | directory: ignore attributes in child entries |
|
|
22
|
+
| `literal_` | stop attribute parsing; the rest of the name is literal |
|
|
23
|
+
| `run_` | script run by `chezmoi apply` |
|
|
24
|
+
| `once_` | with `run_`: run once per content hash |
|
|
25
|
+
| `onchange_` | with `run_`: run when contents change |
|
|
26
|
+
| `before_` / `after_` | with `run_`: run before or after updating targets |
|
|
27
|
+
| `.tmpl` | Go template rendered at apply time |
|
|
28
|
+
| `.literal` | suffix: stop suffix parsing |
|
|
29
|
+
|
|
30
|
+
Resolve the source tree with `chezmoi source-path` (or `chezmoi source-path
|
|
31
|
+
<target>` for one target); it is not at a fixed location.
|