fix: fix nested dirs in example tars (#5447)

This commit is contained in:
Dean Sheather 2022-12-17 02:19:19 +10:00 committed by GitHub
parent fcd5511403
commit 0c0e3f0e4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 8 deletions

View File

@ -7,6 +7,7 @@ import (
"io"
"io/fs"
"path"
"strings"
"sync"
"github.com/gohugoio/hugo/parser/pageparser"
@ -162,28 +163,36 @@ func Archive(exampleID string) ([]byte, error) {
if err != nil {
return err
}
if path == "." {
// Tar files don't have a root directory.
return nil
}
info, err := entry.Info()
if err != nil {
return xerrors.Errorf("stat file: %w", err)
}
header, err := tar.FileInfoHeader(info, entry.Name())
header, err := tar.FileInfoHeader(info, "")
if err != nil {
return xerrors.Errorf("get file header: %w", err)
}
header.Name = strings.TrimPrefix(path, "./")
header.Mode = 0644
if entry.IsDir() {
header.Name = path + "/"
// Trailing slash on entry name is not required. Our tar
// creation code for tarring up a local directory doesn't
// include slashes so this we don't include them here for
// consistency.
// header.Name += "/"
header.Mode = 0755
header.Typeflag = tar.TypeDir
err = tarWriter.WriteHeader(header)
if err != nil {
return xerrors.Errorf("write file: %w", err)
}
} else {
header.Name = path
file, err := exampleFiles.Open(path)
if err != nil {
return xerrors.Errorf("open file %s: %w", path, err)

View File

@ -51,6 +51,6 @@ func TestSubdirs(t *testing.T) {
entryPaths[header.Typeflag] = append(entryPaths[header.Typeflag], header.Name)
}
require.Subset(t, entryPaths[tar.TypeDir], []string{"./", "images/"})
require.Subset(t, entryPaths[tar.TypeDir], []string{"images"})
require.Subset(t, entryPaths[tar.TypeReg], []string{"README.md", "main.tf", "images/base.Dockerfile"})
}

View File

@ -81,11 +81,11 @@ func Tar(directory string, limit int64) ([]byte, error) {
return filepath.SkipDir
}
// Don't archive hidden files!
return err
return nil
}
if strings.Contains(rel, ".tfstate") {
// Don't store tfstate!
return err
return nil
}
// Use unix paths in the tar archive.
header.Name = filepath.ToSlash(rel)