-
Notifications
You must be signed in to change notification settings - Fork 469
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for zip archives to .chezmoiexternal
- Loading branch information
Showing
5 changed files
with
144 additions
and
47 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,96 @@ | ||
package chezmoi | ||
|
||
import ( | ||
"archive/tar" | ||
"archive/zip" | ||
"bytes" | ||
"compress/bzip2" | ||
"compress/gzip" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"io/fs" | ||
"path" | ||
"strings" | ||
) | ||
|
||
var errUnknownFormat = errors.New("unknown format") | ||
|
||
// An walkArchiveFunc is called once for each entry in an archive. | ||
type walkArchiveFunc func(name string, info fs.FileInfo, r io.Reader, linkname string) error | ||
|
||
// walkArchive walks over all the entries in an archive. path is used as a hint | ||
// for the archive format. | ||
func walkArchive(path string, data []byte, f walkArchiveFunc) error { | ||
pathLower := strings.ToLower(path) | ||
if strings.HasSuffix(pathLower, ".zip") { | ||
return walkArchiveZip(bytes.NewReader(data), int64(len(data)), f) | ||
} | ||
var r io.Reader = bytes.NewReader(data) | ||
switch { | ||
case strings.HasSuffix(pathLower, ".tar"): | ||
case strings.HasSuffix(pathLower, ".tar.bz2") || strings.HasSuffix(pathLower, ".tbz2"): | ||
r = bzip2.NewReader(r) | ||
case strings.HasSuffix(pathLower, ".tar.gz") || strings.HasSuffix(pathLower, ".tgz"): | ||
var err error | ||
r, err = gzip.NewReader(r) | ||
if err != nil { | ||
return err | ||
} | ||
default: | ||
return errUnknownFormat | ||
} | ||
return walkArchiveTar(r, f) | ||
} | ||
|
||
// walkArchiveTar walks over all the entries in a tar archive. | ||
func walkArchiveTar(r io.Reader, f walkArchiveFunc) error { | ||
tarReader := tar.NewReader(r) | ||
for { | ||
header, err := tarReader.Next() | ||
switch { | ||
case errors.Is(err, io.EOF): | ||
return nil | ||
case err != nil: | ||
return err | ||
} | ||
name := strings.TrimSuffix(header.Name, "/") | ||
switch header.Typeflag { | ||
case tar.TypeDir, tar.TypeReg: | ||
if err := f(name, header.FileInfo(), tarReader, ""); err != nil { | ||
return err | ||
} | ||
case tar.TypeSymlink: | ||
if err := f(name, header.FileInfo(), nil, header.Linkname); err != nil { | ||
return err | ||
} | ||
case tar.TypeXGlobalHeader: | ||
default: | ||
return fmt.Errorf("%s: unsupported typeflag '%c'", header.Name, header.Typeflag) | ||
} | ||
} | ||
} | ||
|
||
// walkArchiveZip walks over all the entries in a zip archive. | ||
func walkArchiveZip(r io.ReaderAt, size int64, f walkArchiveFunc) error { | ||
zipReader, err := zip.NewReader(r, size) | ||
if err != nil { | ||
return err | ||
} | ||
for _, zipFile := range zipReader.File { | ||
zipFileReader, err := zipFile.Open() | ||
if err != nil { | ||
return err | ||
} | ||
name := path.Clean(zipFile.Name) | ||
if strings.HasPrefix(name, "../") { | ||
return fmt.Errorf("%s: invalid filename", zipFile.Name) | ||
} | ||
err = f(name, zipFile.FileInfo(), zipFileReader, "") | ||
zipFileReader.Close() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
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
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,22 @@ | ||
[!exec:zip] skip 'zip not found in $PATH' | ||
exec zip -r www/archive.zip archive | ||
|
||
httpd www | ||
|
||
# test that chezmoi reads external zip archives from .chezmoiexternal.json | ||
chezmoi apply --force | ||
cmp $HOME/.dir/dir/file golden/dir/file | ||
|
||
-- archive/dir/file -- | ||
# contents of dir/file | ||
-- golden/dir/file -- | ||
# contents of dir/file | ||
-- home/user/.local/share/chezmoi/.chezmoiexternal.json -- | ||
{ | ||
".dir": { | ||
"type": "archive", | ||
"url": "{{ env "HTTPD_URL" }}/archive.zip", | ||
"stripComponents": 1 | ||
} | ||
} | ||
-- www/.keep -- |