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

     1  // Package godoc_vfs contains a driver that reads migrations from a virtual file
     2  // system.
     3  //
     4  // Implementations of the filesystem interface that read from zip files and
     5  // maps, as well as the definition of the filesystem interface can be found in
     6  // the golang.org/x/tools/godoc/vfs package.
     7  package godoc_vfs
     8  
     9  import (
    10  	"github.com/golang-migrate/migrate/v4/source"
    11  	"github.com/golang-migrate/migrate/v4/source/httpfs"
    12  
    13  	"golang.org/x/tools/godoc/vfs"
    14  	vfs_httpfs "golang.org/x/tools/godoc/vfs/httpfs"
    15  )
    16  
    17  func init() {
    18  	source.Register("godoc-vfs", &VFS{})
    19  }
    20  
    21  // VFS is an implementation of driver that returns migrations from a virtual
    22  // file system.
    23  type VFS struct {
    24  	httpfs.PartialDriver
    25  	fs   vfs.FileSystem
    26  	path string
    27  }
    28  
    29  // Open implements the source.Driver interface for VFS.
    30  //
    31  // Calling this function panics, instead use the WithInstance function.
    32  // See the package level documentation for an example.
    33  func (b *VFS) Open(url string) (source.Driver, error) {
    34  	panic("not implemented")
    35  }
    36  
    37  // WithInstance creates a new driver from a virtual file system.
    38  // If a tree named searchPath exists in the virtual filesystem, WithInstance
    39  // searches for migration files there.
    40  // It defaults to "/".
    41  func WithInstance(fs vfs.FileSystem, searchPath string) (source.Driver, error) {
    42  	if searchPath == "" {
    43  		searchPath = "/"
    44  	}
    45  
    46  	bn := &VFS{
    47  		fs:   fs,
    48  		path: searchPath,
    49  	}
    50  
    51  	if err := bn.Init(vfs_httpfs.New(fs), searchPath); err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	return bn, nil
    56  }