Skip to content

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

Merged
merged 5 commits into from
May 25, 2022

Conversation

dwahler
Copy link
Contributor

@dwahler dwahler commented May 24, 2022

This PR makes coder templates init correctly include files in subdirectories of examples.

Subtasks

  • moved example templates to examples/templates and updated package to embed that entire directory
  • fixed examples.Archive to recursively walk directories
  • added test to make sure tar headers are written for both files and directories
  • updated documentation links

Fixes #1669

@dwahler dwahler requested a review from a team May 24, 2022 17:11
Comment on lines 172 to 175
_, err = tarWriter.Write(data)
if err != nil {
return xerrors.Errorf("write: %w", err)
}
Copy link
Member

Choose a reason for hiding this comment

The 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

if err != nil {
return xerrors.Errorf("open file %s: %w", path, err)
}
_, err = file.Read(data)
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

@@ -154,22 +155,17 @@ func Archive(exampleID string) ([]byte, error) {
} 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)
}
Copy link
Member

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()

Copy link
Contributor Author

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.

@dwahler dwahler merged commit f8410de into main May 25, 2022
@dwahler dwahler deleted the david/1669-example-subdirs branch May 25, 2022 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: subfolders in example templates are not bundled
3 participants