-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add decompression of file externals
- Loading branch information
Showing
4 changed files
with
138 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package chezmoi | ||
|
||
import ( | ||
"bytes" | ||
"compress/bzip2" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/klauspost/compress/gzip" | ||
"github.com/klauspost/compress/zstd" | ||
"github.com/ulikunitz/xz" | ||
) | ||
|
||
// A compressionFormat is a compression format. | ||
type compressionFormat string | ||
|
||
// Compression formats. | ||
const ( | ||
compressionFormatNone compressionFormat = "" | ||
compressionFormatBzip2 compressionFormat = "bzip2" | ||
compressionFormatGzip compressionFormat = "gzip" | ||
compressionFormatXz compressionFormat = "xz" | ||
compressionFormatZstd compressionFormat = "zstd" | ||
) | ||
|
||
func decompress(compressionFormat compressionFormat, data []byte) ([]byte, error) { | ||
switch compressionFormat { | ||
case compressionFormatNone: | ||
return data, nil | ||
case compressionFormatBzip2: | ||
return io.ReadAll(bzip2.NewReader(bytes.NewReader(data))) | ||
case compressionFormatGzip: | ||
gzipReader, err := gzip.NewReader(bytes.NewReader(data)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return io.ReadAll(gzipReader) | ||
case compressionFormatXz: | ||
xzReader, err := xz.NewReader(bytes.NewReader(data)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return io.ReadAll(xzReader) | ||
case compressionFormatZstd: | ||
zstdReader, err := zstd.NewReader(bytes.NewReader(data)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return io.ReadAll(zstdReader) | ||
default: | ||
return nil, fmt.Errorf("%s: unknown compression format", compressionFormat) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
[exec:bzip2] exec bzip2 www/file-bzip2 | ||
[exec:gzip] exec gzip www/file-gzip | ||
[exec:xz] exec xz www/file-xz | ||
[exec:zstd] exec zstd www/file-zstd | ||
|
||
httpd www | ||
|
||
# test that chezmoi apply decompresses files in multiple formats | ||
exec chezmoi apply | ||
[exec:bzip2] cmp $HOME/file-bzip2 golden/file-bzip2 | ||
[exec:gzip] cmp $HOME/file-gzip golden/file-gzip | ||
[exec:xz] cmp $HOME/file-xz golden/file-xz | ||
[exec:zstd] cmp $HOME/file-zstd golden/file-zstd | ||
|
||
-- golden/file-bzip2 -- | ||
# contents of file-bzip2 | ||
-- golden/file-gzip -- | ||
# contents of file-gzip | ||
-- golden/file-xz -- | ||
# contents of file-xz | ||
-- golden/file-zstd -- | ||
# contents of file-zstd | ||
-- home/user/.local/share/chezmoi/.chezmoiexternal.toml.tmpl -- | ||
{{ if lookPath "bzip2" }} | ||
[file-bzip2] | ||
type = "file" | ||
url = "{{ env "HTTPD_URL" }}/file-bzip2.bz2" | ||
decompress = "bzip2" | ||
{{ end }} | ||
{{ if lookPath "gzip" }} | ||
[file-gzip] | ||
type = "file" | ||
url = "{{ env "HTTPD_URL" }}/file-gzip.gz" | ||
decompress = "gzip" | ||
{{ end }} | ||
{{ if lookPath "xz" }} | ||
[file-xz] | ||
type = "file" | ||
url = "{{ env "HTTPD_URL" }}/file-xz.xz" | ||
decompress = "xz" | ||
{{ end }} | ||
{{ if lookPath "zstd" }} | ||
[file-zstd] | ||
type = "file" | ||
url = "{{ env "HTTPD_URL" }}/file-zstd.zst" | ||
decompress = "zstd" | ||
{{ end }} | ||
-- www/file-bzip2 -- | ||
# contents of file-bzip2 | ||
-- www/file-gzip -- | ||
# contents of file-gzip | ||
-- www/file-xz -- | ||
# contents of file-xz | ||
-- www/file-zstd -- | ||
# contents of file-zstd |