-
Notifications
You must be signed in to change notification settings - Fork 987
fix: include subdirectories in example templates #1715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
fc75d86
0a13253
a9125fc
e94a9c7
ee7a1fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"archive/tar" | ||
"bytes" | ||
"embed" | ||
"io/fs" | ||
"path" | ||
"sync" | ||
|
||
|
@@ -13,8 +14,7 @@ import ( | |
) | ||
|
||
var ( | ||
//go:embed */*.md | ||
//go:embed */*.tf | ||
//go:embed templates | ||
files embed.FS | ||
|
||
examples = make([]Example, 0) | ||
|
@@ -29,20 +29,30 @@ type Example struct { | |
Markdown string `json:"markdown"` | ||
} | ||
|
||
const rootDir = "templates" | ||
|
||
// List returns all embedded examples. | ||
func List() ([]Example, error) { | ||
var returnError error | ||
parseExamples.Do(func() { | ||
dirs, err := files.ReadDir(".") | ||
files, err := fs.Sub(files, rootDir) | ||
if err != nil { | ||
returnError = xerrors.Errorf("get example fs: %w", err) | ||
} | ||
|
||
dirs, err := fs.ReadDir(files, ".") | ||
if err != nil { | ||
returnError = xerrors.Errorf("read dir: %w", err) | ||
return | ||
} | ||
|
||
for _, dir := range dirs { | ||
if !dir.IsDir() { | ||
continue | ||
} | ||
exampleID := dir.Name() | ||
// Each one of these is a example! | ||
readme, err := files.ReadFile(path.Join(dir.Name(), "README.md")) | ||
readme, err := fs.ReadFile(files, path.Join(dir.Name(), "README.md")) | ||
if err != nil { | ||
returnError = xerrors.Errorf("example %q does not contain README.md", exampleID) | ||
return | ||
|
@@ -110,54 +120,69 @@ func Archive(exampleID string) ([]byte, error) { | |
return nil, xerrors.Errorf("example with id %q not found", exampleID) | ||
} | ||
|
||
entries, err := files.ReadDir(exampleID) | ||
exampleFiles, err := fs.Sub(files, path.Join(rootDir, exampleID)) | ||
if err != nil { | ||
return nil, xerrors.Errorf("read dir: %w", err) | ||
return nil, xerrors.Errorf("get example fs: %w", err) | ||
} | ||
|
||
var buffer bytes.Buffer | ||
tarWriter := tar.NewWriter(&buffer) | ||
|
||
for _, entry := range entries { | ||
file, err := files.Open(path.Join(exampleID, entry.Name())) | ||
if err != nil { | ||
return nil, xerrors.Errorf("open file: %w", err) | ||
} | ||
|
||
info, err := file.Stat() | ||
err = fs.WalkDir(exampleFiles, ".", func(path string, entry fs.DirEntry, err error) error { | ||
if err != nil { | ||
return nil, xerrors.Errorf("stat file: %w", err) | ||
return err | ||
} | ||
|
||
if info.IsDir() { | ||
continue | ||
} | ||
|
||
data := make([]byte, info.Size()) | ||
_, err = file.Read(data) | ||
info, err := entry.Info() | ||
if err != nil { | ||
return nil, xerrors.Errorf("read data: %w", err) | ||
return xerrors.Errorf("stat file: %w", err) | ||
} | ||
|
||
header, err := tar.FileInfoHeader(info, entry.Name()) | ||
if err != nil { | ||
return nil, xerrors.Errorf("get file header: %w", err) | ||
return xerrors.Errorf("get file header: %w", err) | ||
} | ||
header.Mode = 0644 | ||
|
||
err = tarWriter.WriteHeader(header) | ||
if err != nil { | ||
return nil, xerrors.Errorf("write file: %w", err) | ||
} | ||
|
||
_, err = tarWriter.Write(data) | ||
if err != nil { | ||
return nil, xerrors.Errorf("write: %w", err) | ||
if entry.IsDir() { | ||
header.Name = path + "/" | ||
|
||
err = tarWriter.WriteHeader(header) | ||
if err != nil { | ||
return xerrors.Errorf("write file: %w", err) | ||
} | ||
} else { | ||
header.Name = path | ||
|
||
data := make([]byte, info.Size()) | ||
file, err := exampleFiles.Open(path) | ||
if err != nil { | ||
return xerrors.Errorf("open file %s: %w", path, err) | ||
} | ||
_, err = file.Read(data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't matter if you use io.Copy, but in the future you should use io.ReadAll instead, because Read isn't guaranteed to fill your output buffer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! |
||
if err != nil { | ||
return xerrors.Errorf("read data: %w", err) | ||
} | ||
|
||
err = tarWriter.WriteHeader(header) | ||
if err != nil { | ||
return xerrors.Errorf("write file: %w", err) | ||
} | ||
|
||
_, err = tarWriter.Write(data) | ||
if err != nil { | ||
return xerrors.Errorf("write: %w", err) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should be able to io.Copy instead to avoid the extra buffer |
||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return nil, xerrors.Errorf("walk example directory: %w", err) | ||
} | ||
err = tarWriter.Flush() | ||
|
||
err = tarWriter.Close() | ||
if err != nil { | ||
return nil, xerrors.Errorf("flush archive: %w", err) | ||
return nil, xerrors.Errorf("close archive: %w", err) | ||
} | ||
|
||
return buffer.Bytes(), nil | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be closed i.e.
defer file.Close()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Technically I think we would be OK without it, because the readers returned by
embed.FS
are just pointers, but I added it anyway to make the code more obviously correct.