2018-02-07 02:23:47 +08:00
|
|
|
package alias
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
2018-05-15 01:06:57 +08:00
|
|
|
"github.com/ncw/rclone/fs/config/configmap"
|
|
|
|
"github.com/ncw/rclone/fs/config/configstruct"
|
2018-02-07 02:23:47 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Register with Fs
|
|
|
|
func init() {
|
|
|
|
fsi := &fs.RegInfo{
|
|
|
|
Name: "alias",
|
|
|
|
Description: "Alias for a existing remote",
|
|
|
|
NewFs: NewFs,
|
|
|
|
Options: []fs.Option{{
|
2018-05-15 01:06:57 +08: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-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"`
|
|
|
|
}
|
|
|
|
|
2018-02-07 02:23:47 +08:00
|
|
|
// NewFs contstructs an Fs from the path.
|
|
|
|
//
|
|
|
|
// The returned Fs is the actual Fs, referenced by remote in the config
|
2018-05-15 01:06:57 +08: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-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")
|
|
|
|
}
|
2018-05-15 01:06:57 +08:00
|
|
|
_, configName, fsPath, err := fs.ParseRemote(opt.Remote)
|
2018-02-07 02:23:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-15 01:06:57 +08:00
|
|
|
if configName == "local" {
|
2018-09-21 17:12:57 +08:00
|
|
|
root = filepath.Join(fsPath, root)
|
2018-05-15 01:06:57 +08:00
|
|
|
return fs.NewFs(root)
|
|
|
|
}
|
2018-09-21 17:12:57 +08:00
|
|
|
root = path.Join(fsPath, filepath.ToSlash(root))
|
2018-05-15 01:06:57 +08:00
|
|
|
return fs.NewFs(configName + ":" + root)
|
2018-02-07 02:23:47 +08:00
|
|
|
}
|