Skip to content

Commit 0c0e3f0

Browse files
authored
fix: fix nested dirs in example tars (#5447)
1 parent fcd5511 commit 0c0e3f0

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

examples/examples.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"io/fs"
99
"path"
10+
"strings"
1011
"sync"
1112

1213
"github.com/gohugoio/hugo/parser/pageparser"
@@ -162,28 +163,36 @@ func Archive(exampleID string) ([]byte, error) {
162163
if err != nil {
163164
return err
164165
}
166+
if path == "." {
167+
// Tar files don't have a root directory.
168+
return nil
169+
}
165170

166171
info, err := entry.Info()
167172
if err != nil {
168173
return xerrors.Errorf("stat file: %w", err)
169174
}
170175

171-
header, err := tar.FileInfoHeader(info, entry.Name())
176+
header, err := tar.FileInfoHeader(info, "")
172177
if err != nil {
173178
return xerrors.Errorf("get file header: %w", err)
174179
}
180+
header.Name = strings.TrimPrefix(path, "./")
175181
header.Mode = 0644
176182

177183
if entry.IsDir() {
178-
header.Name = path + "/"
179-
184+
// Trailing slash on entry name is not required. Our tar
185+
// creation code for tarring up a local directory doesn't
186+
// include slashes so this we don't include them here for
187+
// consistency.
188+
// header.Name += "/"
189+
header.Mode = 0755
190+
header.Typeflag = tar.TypeDir
180191
err = tarWriter.WriteHeader(header)
181192
if err != nil {
182193
return xerrors.Errorf("write file: %w", err)
183194
}
184195
} else {
185-
header.Name = path
186-
187196
file, err := exampleFiles.Open(path)
188197
if err != nil {
189198
return xerrors.Errorf("open file %s: %w", path, err)

examples/examples_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ func TestSubdirs(t *testing.T) {
5151
entryPaths[header.Typeflag] = append(entryPaths[header.Typeflag], header.Name)
5252
}
5353

54-
require.Subset(t, entryPaths[tar.TypeDir], []string{"./", "images/"})
54+
require.Subset(t, entryPaths[tar.TypeDir], []string{"images"})
5555
require.Subset(t, entryPaths[tar.TypeReg], []string{"README.md", "main.tf", "images/base.Dockerfile"})
5656
}

provisionersdk/archive.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ func Tar(directory string, limit int64) ([]byte, error) {
8181
return filepath.SkipDir
8282
}
8383
// Don't archive hidden files!
84-
return err
84+
return nil
8585
}
8686
if strings.Contains(rel, ".tfstate") {
8787
// Don't store tfstate!
88-
return err
88+
return nil
8989
}
9090
// Use unix paths in the tar archive.
9191
header.Name = filepath.ToSlash(rel)

0 commit comments

Comments
 (0)