github.com/xhghs/rclone@v1.51.1-0.20200430155106-e186a28cced8/backend/alias/alias.go (about)

     1  package alias
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  
     7  	"github.com/rclone/rclone/fs"
     8  	"github.com/rclone/rclone/fs/config/configmap"
     9  	"github.com/rclone/rclone/fs/config/configstruct"
    10  	"github.com/rclone/rclone/fs/fspath"
    11  )
    12  
    13  // Register with Fs
    14  func init() {
    15  	fsi := &fs.RegInfo{
    16  		Name:        "alias",
    17  		Description: "Alias for an existing remote",
    18  		NewFs:       NewFs,
    19  		Options: []fs.Option{{
    20  			Name:     "remote",
    21  			Help:     "Remote or path to alias.\nCan be \"myremote:path/to/dir\", \"myremote:bucket\", \"myremote:\" or \"/local/path\".",
    22  			Required: true,
    23  		}},
    24  	}
    25  	fs.Register(fsi)
    26  }
    27  
    28  // Options defines the configuration for this backend
    29  type Options struct {
    30  	Remote string `config:"remote"`
    31  }
    32  
    33  // NewFs constructs an Fs from the path.
    34  //
    35  // The returned Fs is the actual Fs, referenced by remote in the config
    36  func NewFs(name, root string, m configmap.Mapper) (fs.Fs, error) {
    37  	// Parse config into Options struct
    38  	opt := new(Options)
    39  	err := configstruct.Set(m, opt)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	if opt.Remote == "" {
    44  		return nil, errors.New("alias can't point to an empty remote - check the value of the remote setting")
    45  	}
    46  	if strings.HasPrefix(opt.Remote, name+":") {
    47  		return nil, errors.New("can't point alias remote at itself - check the value of the remote setting")
    48  	}
    49  	fsInfo, configName, fsPath, config, err := fs.ConfigFs(opt.Remote)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	return fsInfo.NewFs(configName, fspath.JoinRootPath(fsPath, root), config)
    54  }