github.com/kubecost/golang-migrate-duckdb/v4@v4.17.0-duckdb.1/source/go_bindata/go-bindata.go (about)

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