github.com/MRtecno98/afero@v1.9.3/resolver/resolver.go (about)

     1  package resolver
     2  
     3  import (
     4  	"errors"
     5  	"net/url"
     6  
     7  	"github.com/MRtecno98/afero"
     8  	"github.com/MRtecno98/afero/sftpfs"
     9  )
    10  
    11  var protocols = map[string]func(*url.URL) (afero.Fs, error){
    12  	"mem": func(*url.URL) (afero.Fs, error) {
    13  		return afero.NewMemMapFs(), nil
    14  	},
    15  
    16  	"file": func(u *url.URL) (afero.Fs, error) {
    17  		return afero.NewBasePathFs(afero.NewOsFs(), u.Path), nil
    18  	},
    19  
    20  	"sftp": sftpfs.Resolve,
    21  	"ssh":  sftpfs.Resolve,
    22  }
    23  
    24  func init() {
    25  	protocols[""] = protocols["file"] // No scheme: Default protocol
    26  }
    27  
    28  func OpenUrl(u string) (afero.Fs, error) {
    29  	url, err := url.Parse(u)
    30  
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	if proto, ok := protocols[url.Scheme]; ok {
    36  		return proto(url)
    37  	} else {
    38  		return nil, errors.New("protocol not implemented")
    39  	}
    40  }