github.com/anovateam/migrate@v3.5.4+incompatible/source/go_bindata/go-bindata.go (about) 1 package bindata 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "io/ioutil" 8 "os" 9 10 "github.com/golang-migrate/migrate/source" 11 ) 12 13 type AssetFunc func(name string) ([]byte, error) 14 15 func Resource(names []string, afn AssetFunc) *AssetSource { 16 return &AssetSource{ 17 Names: names, 18 AssetFunc: afn, 19 } 20 } 21 22 type AssetSource struct { 23 Names []string 24 AssetFunc AssetFunc 25 } 26 27 func init() { 28 source.Register("go-bindata", &Bindata{}) 29 } 30 31 type Bindata struct { 32 path string 33 assetSource *AssetSource 34 migrations *source.Migrations 35 } 36 37 func (b *Bindata) Open(url string) (source.Driver, error) { 38 return nil, fmt.Errorf("not yet implemented") 39 } 40 41 var ( 42 ErrNoAssetSource = fmt.Errorf("expects *AssetSource") 43 ) 44 45 func WithInstance(instance interface{}) (source.Driver, error) { 46 if _, ok := instance.(*AssetSource); !ok { 47 return nil, ErrNoAssetSource 48 } 49 as := instance.(*AssetSource) 50 51 bn := &Bindata{ 52 path: "<go-bindata>", 53 assetSource: as, 54 migrations: source.NewMigrations(), 55 } 56 57 for _, fi := range as.Names { 58 m, err := source.DefaultParse(fi) 59 if err != nil { 60 continue // ignore files that we can't parse 61 } 62 63 if !bn.migrations.Append(m) { 64 return nil, fmt.Errorf("unable to parse file %v", fi) 65 } 66 } 67 68 return bn, nil 69 } 70 71 func (b *Bindata) Close() error { 72 return nil 73 } 74 75 func (b *Bindata) First() (version uint, err error) { 76 if v, ok := b.migrations.First(); !ok { 77 return 0, &os.PathError{"first", b.path, os.ErrNotExist} 78 } else { 79 return v, nil 80 } 81 } 82 83 func (b *Bindata) Prev(version uint) (prevVersion uint, err error) { 84 if v, ok := b.migrations.Prev(version); !ok { 85 return 0, &os.PathError{fmt.Sprintf("prev for version %v", version), b.path, os.ErrNotExist} 86 } else { 87 return v, nil 88 } 89 } 90 91 func (b *Bindata) Next(version uint) (nextVersion uint, err error) { 92 if v, ok := b.migrations.Next(version); !ok { 93 return 0, &os.PathError{fmt.Sprintf("next for version %v", version), b.path, os.ErrNotExist} 94 } else { 95 return v, nil 96 } 97 } 98 99 func (b *Bindata) ReadUp(version uint) (r io.ReadCloser, identifier string, err error) { 100 if m, ok := b.migrations.Up(version); ok { 101 body, err := b.assetSource.AssetFunc(m.Raw) 102 if err != nil { 103 return nil, "", err 104 } 105 return ioutil.NopCloser(bytes.NewReader(body)), m.Identifier, nil 106 } 107 return nil, "", &os.PathError{fmt.Sprintf("read version %v", version), b.path, os.ErrNotExist} 108 } 109 110 func (b *Bindata) ReadDown(version uint) (r io.ReadCloser, identifier string, err error) { 111 if m, ok := b.migrations.Down(version); ok { 112 body, err := b.assetSource.AssetFunc(m.Raw) 113 if err != nil { 114 return nil, "", err 115 } 116 return ioutil.NopCloser(bytes.NewReader(body)), m.Identifier, nil 117 } 118 return nil, "", &os.PathError{fmt.Sprintf("read version %v", version), b.path, os.ErrNotExist} 119 }