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