cachefs/pkg/provider/parse/parse.go

34 lines
667 B
Go

// Copyright (C) 2022 Marius Schellenberger
package parse
import (
"cachefs/pkg/provider"
"cachefs/pkg/provider/mount"
"errors"
"fmt"
neturl "net/url"
"path/filepath"
)
var (
ErrPathNotAbsolute = errors.New("path is not absolute")
ErrUnsupportedScheme = errors.New("unsupported scheme")
)
func FS(url string) (provider.FS, error) {
u, err := neturl.Parse(url)
if err != nil {
return nil, err
}
if !filepath.IsAbs(u.Path) {
return nil, ErrPathNotAbsolute
}
switch u.Scheme {
case "file":
return mount.NewFS(u.Path), nil
//case "sftp":
// return sftp.FS(u.Path), nil
}
return nil, fmt.Errorf("%w: %s", ErrUnsupportedScheme, u.Scheme)
}