github.com/artpar/rclone@v1.67.3/backend/alias/alias.go (about)

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