Skip to content

Commit e5b0ec1

Browse files
committed
feat: Support file:// URLs in externals
1 parent efc8ddc commit e5b0ec1

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

assets/chezmoi.io/docs/reference/special-files/chezmoiexternal-format.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ archives to be included as if they were in the source state.
1212
externals to be included on different machines.
1313

1414
If a `.chezmoiexternal.$FORMAT` file is located in an ignored directory (one
15-
listed in [`.chezmoiignore`](chezmoiignore.md)), all entries within the file are also ignored.
15+
listed in [`.chezmoiignore`](chezmoiignore.md)), all entries within the file are
16+
also ignored.
1617

1718
Entries are indexed by target name relative to the directory of the
1819
`.chezmoiexternal.$FORMAT` file, and must have a `type` and a `url` field.
@@ -48,6 +49,8 @@ Entries may have the following fields:
4849
| `pull.args` | []string | *none* | Extra args to `git pull` |
4950
| `archive.extractAppleDouble` | bool | `false` | If `true`, AppleDouble files are extracted |
5051

52+
`url` must be an `https://`, `http://`, or `file://` URL.
53+
5154
If any of the optional `checksum.sha256`, `checksum.sha384`, or
5255
`checksum.sha512` fields are set, chezmoi will verify that the downloaded data
5356
has the given checksum.

internal/chezmoi/sourcestate.go

+12
Original file line numberDiff line numberDiff line change
@@ -1561,6 +1561,18 @@ func (s *SourceState) getExternalDataRaw(
15611561
external *External,
15621562
options *ReadOptions,
15631563
) ([]byte, error) {
1564+
// Handle file:// URLs by always reading from disk.
1565+
switch urlStruct, err := url.Parse(external.URL); {
1566+
case err != nil:
1567+
return nil, err
1568+
case urlStruct.Scheme == "file":
1569+
data, err := s.system.ReadFile(NewAbsPath(urlStruct.Path))
1570+
if err != nil {
1571+
return nil, err
1572+
}
1573+
return data, nil
1574+
}
1575+
15641576
var now time.Time
15651577
if options != nil && options.TimeNow != nil {
15661578
now = options.TimeNow()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
exec chezmoi apply
2+
cmp $HOME/.file golden/.file
3+
4+
-- golden/.file --
5+
# contents of .file
6+
-- home/user/.local/share/chezmoi/.chezmoiexternal.toml --
7+
[".file"]
8+
type = "file"
9+
url = "file://{{ .chezmoi.homeDir }}/.local/share/file.txt"
10+
-- home/user/.local/share/file.txt --
11+
# contents of .file

0 commit comments

Comments
 (0)