packr
packr copied to clipboard
Clarification on relative path
I would like to clarify the behavior of New
function in packr2
. In the README:
Packr takes file resolution a step further. When declaring a new box you use a relative path, ./templates. When Packr receives this call it calculates out the absolute path to that directory.
I'd like to confirm that the relative path here means the path relative to the file that is calling packr.New
. I have found the following code that seems to indicate such behavior, but it wasn't clear from the README.
https://github.com/gobuffalo/packr/blob/dc520c910ea91354b3ae131bbb029270e4fc1af4/v2/helpers.go#L59-L63
It it is the case, would you consider a small PR clarifying this behavior in the README?
I just came here to submit an issue for this very thing. The answer is that the path is relative to the .go
file where you're calling the New
function. It is NOT relative to the root project directory or the build directory.
For example, with the following directory structure:
├── cmd
└──myApp
└── main.go
├── templates
├── admin
│ └── index.html
└── index.html
You would use something like this in the main.go
file to create a new box:
package main
import (
"net/http"
"github.com/gobuffalo/packr/v2"
)
func main() {
box := packr.New("someBoxName", "../templates")
http.Handle("/", http.FileServer(box))
http.ListenAndServe(":3000", nil)
}
Notice that the templates/
directory's relative location is to the main.go
file, which is contained in the cmd/myApp
directory. The relative location is thus up one directory, and then down into the templates/
directory (i.e. "../templates"
).
It would be great to have this spelled out a little more clearly in the README
. I could submit a PR if you'd like, but I believe I've clarified it fairly well here.