Skip to content

fix: include subdirectories in example templates - #1715

Merged
dwahler merged 5 commits into
mainfrom
david/1669-example-subdirs
May 25, 2022
Merged

dwahler merged 5 commits into
mainfrom
david/1669-example-subdirs

Conversation

@dwahler

@dwahler dwahler commented May 24, 2022

Copy link
Copy Markdown
Contributor

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 thread examples/examples.go Outdated
Comment on lines +172 to +175
_, err = tarWriter.Write(data)
if err != nil {
return xerrors.Errorf("write: %w", err)
}

Copy link
Copy Markdown
Collaborator

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

Comment thread examples/examples.go Outdated
if err != nil {
return xerrors.Errorf("open file %s: %w", path, err)
}
_, err = file.Read(data)

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch!

Comment thread examples/examples.go
file, err := exampleFiles.Open(path)
if err != nil {
return xerrors.Errorf("open file %s: %w", path, err)
}

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
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