2018-02-07 02:23:47 +08:00
|
|
|
package alias
|
|
|
|
|
|
|
|
import (
|
2020-11-05 23:18:51 +08:00
|
|
|
"context"
|
2018-02-07 02:23:47 +08:00
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
2020-09-01 00:07:26 +08:00
|
|
|
"github.com/rclone/rclone/fs/cache"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs/config/configmap"
|
|
|
|
"github.com/rclone/rclone/fs/config/configstruct"
|
|
|
|
"github.com/rclone/rclone/fs/fspath"
|
2018-02-07 02:23:47 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Register with Fs
|
|
|
|
func init() {
|
|
|
|
fsi := &fs.RegInfo{
|
|
|
|
Name: "alias",
|
2019-04-25 19:32:36 +08:00
|
|
|
Description: "Alias for an existing remote",
|
2018-02-07 02:23:47 +08:00
|
|
|
NewFs: NewFs,
|
|
|
|
Options: []fs.Option{{
|
2018-05-15 01:06:57 +08:00
|
|
|
Name: "remote",
|
2021-08-16 17:30:01 +08:00
|
|
|
Help: "Remote or path to alias.\n\nCan be \"myremote:path/to/dir\", \"myremote:bucket\", \"myremote:\" or \"/local/path\".",
|
2018-05-15 01:06:57 +08:00
|
|
|
Required: true,
|
2018-02-07 02:23:47 +08:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
fs.Register(fsi)
|
|
|
|
}
|
|
|
|
|
2018-05-15 01:06:57 +08:00
|
|
|
// Options defines the configuration for this backend
|
|
|
|
type Options struct {
|
|
|
|
Remote string `config:"remote"`
|
|
|
|
}
|
|
|
|
|
2019-02-08 01:41:17 +08:00
|
|
|
// NewFs constructs an Fs from the path.
|
2018-02-07 02:23:47 +08:00
|
|
|
//
|
|
|
|
// The returned Fs is the actual Fs, referenced by remote in the config
|
2020-11-05 23:18:51 +08:00
|
|
|
func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, error) {
|
2018-05-15 01:06:57 +08:00
|
|
|
// Parse config into Options struct
|
|
|
|
opt := new(Options)
|
|
|
|
err := configstruct.Set(m, opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if opt.Remote == "" {
|
2018-02-07 02:23:47 +08:00
|
|
|
return nil, errors.New("alias can't point to an empty remote - check the value of the remote setting")
|
|
|
|
}
|
2018-05-15 01:06:57 +08:00
|
|
|
if strings.HasPrefix(opt.Remote, name+":") {
|
2018-02-07 02:23:47 +08:00
|
|
|
return nil, errors.New("can't point alias remote at itself - check the value of the remote setting")
|
|
|
|
}
|
2020-11-05 23:18:51 +08:00
|
|
|
return cache.Get(ctx, fspath.JoinRootPath(opt.Remote, root))
|
2018-02-07 02:23:47 +08:00
|
|
|
}
|