- Notifications
You must be signed in to change notification settings - Fork928
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
fc75d86
0a13253
a9125fc
e94a9c7
ee7a1fb
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -4,6 +4,8 @@ import ( | ||
"archive/tar" | ||
"bytes" | ||
"embed" | ||
"io" | ||
"io/fs" | ||
"path" | ||
"sync" | ||
@@ -13,8 +15,7 @@ import ( | ||
) | ||
var ( | ||
//go:embed templates | ||
files embed.FS | ||
examples = make([]Example, 0) | ||
@@ -29,20 +30,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() { | ||
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 :=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 +121,65 @@ func Archive(exampleID string) ([]byte, error) { | ||
return nil, xerrors.Errorf("example with id %q not found", exampleID) | ||
} | ||
exampleFiles, err :=fs.Sub(files, path.Join(rootDir,exampleID)) | ||
if err != nil { | ||
return nil, xerrors.Errorf("get example fs: %w", err) | ||
} | ||
var buffer bytes.Buffer | ||
tarWriter := tar.NewWriter(&buffer) | ||
err = fs.WalkDir(exampleFiles, ".", func(path string, entry fs.DirEntry, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
info, err := entry.Info() | ||
if err != nil { | ||
return xerrors.Errorf("stat file: %w", err) | ||
} | ||
header, err := tar.FileInfoHeader(info, entry.Name()) | ||
if err != nil { | ||
return xerrors.Errorf("get file header: %w", err) | ||
} | ||
header.Mode = 0644 | ||
if entry.IsDir() { | ||
header.Name = path + "/" | ||
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) | ||
} | ||
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. Should be closed i.e. 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 point. Technically I think we would be OK without it, because the readers returned by | ||
defer file.Close() | ||
err = tarWriter.WriteHeader(header) | ||
if err != nil { | ||
return xerrors.Errorf("write file: %w", err) | ||
} | ||
_, err = io.Copy(tarWriter, file) | ||
if err != nil { | ||
return xerrors.Errorf("write: %w", err) | ||
} | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return nil, xerrors.Errorf("walk example directory: %w", err) | ||
} | ||
err = tarWriter.Close() | ||
if err != nil { | ||
return nil, xerrors.Errorf("close archive: %w", err) | ||
} | ||
return buffer.Bytes(), nil | ||