mirror of
https://github.com/hanzoai/migrate.git
synced 2026-09-30 22:55:10 +00:00
* Add pkger source driver support As go-bindata has been abandoned [1] there are open requests, #116, for alternative sources with similar functionality. The Buffalo project [2] created packr and recently pkger [3] was announced [4] with the intention to supersede packr. This change adds support for using pkger as a source. The implementation relies on httpfs.PartialDriver for pretty much all functionality. [1] https://github.com/jteeuwen/go-bindata/issues/5 [2] https://gobuffalo.io/ [3] https://github.com/markbates/pkger [4] https://blog.gobuffalo.io/introducing-pkger-static-file-embedding-in-go-1ce76dc79c65 * pkger: rename Instance to Pkger * pkger: make WithInstance accept *Pkger * pkger: refactor and add access to global pkging.Pkger instance * pkger: fix typo and cleanup debug logging
84 lines
2.2 KiB
Go
84 lines
2.2 KiB
Go
package pkger
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
stdurl "net/url"
|
|
|
|
"github.com/golang-migrate/migrate/v4/source"
|
|
"github.com/golang-migrate/migrate/v4/source/httpfs"
|
|
"github.com/markbates/pkger"
|
|
"github.com/markbates/pkger/pkging"
|
|
)
|
|
|
|
func init() {
|
|
source.Register("pkger", &Pkger{})
|
|
}
|
|
|
|
// Pkger is a source.Driver that reads migrations from instances of
|
|
// pkging.Pkger.
|
|
type Pkger struct {
|
|
httpfs.PartialDriver
|
|
}
|
|
|
|
// Open implements source.Driver. The path component of url will be used as the
|
|
// relative location of migrations. The returned driver will use the package
|
|
// scoped pkger.Open to access migrations. The relative root and any
|
|
// migrations must be added to the global pkger.Pkger instance by calling
|
|
// pkger.Apply. Refer to Pkger documentation for more information.
|
|
func (p *Pkger) Open(url string) (source.Driver, error) {
|
|
u, err := stdurl.Parse(url)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// wrap pkger to implement http.FileSystem.
|
|
fs := fsFunc(func(name string) (http.File, error) {
|
|
f, err := pkger.Open(name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return f.(http.File), nil
|
|
})
|
|
|
|
if err := p.Init(fs, u.Path); err != nil {
|
|
return nil, fmt.Errorf("failed to init driver with relative path %q: %w", u.Path, err)
|
|
}
|
|
|
|
return p, nil
|
|
}
|
|
|
|
// WithInstance returns a source.Driver that is backed by an instance of
|
|
// pkging.Pkger. The relative location of migrations is indicated by path. The
|
|
// path must exist on the pkging.Pkger instance for the driver to initialize
|
|
// successfully.
|
|
func WithInstance(instance pkging.Pkger, path string) (source.Driver, error) {
|
|
if instance == nil {
|
|
return nil, fmt.Errorf("expected instance of pkging.Pkger")
|
|
}
|
|
|
|
// wrap pkger to implement http.FileSystem.
|
|
fs := fsFunc(func(name string) (http.File, error) {
|
|
f, err := instance.Open(name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return f.(http.File), nil
|
|
})
|
|
|
|
var p Pkger
|
|
|
|
if err := p.Init(fs, path); err != nil {
|
|
return nil, fmt.Errorf("failed to init driver with relative path %q: %w", path, err)
|
|
}
|
|
|
|
return &p, nil
|
|
}
|
|
|
|
type fsFunc func(name string) (http.File, error)
|
|
|
|
// Open implements http.FileSystem.
|
|
func (f fsFunc) Open(name string) (http.File, error) {
|
|
return f(name)
|
|
}
|