github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/cmd/link/link.go (about)

     1  // Package link provides the link command.
     2  package link
     3  
     4  import (
     5  	"context"
     6  	"fmt"
     7  
     8  	"github.com/rclone/rclone/cmd"
     9  	"github.com/rclone/rclone/fs"
    10  	"github.com/rclone/rclone/fs/config/flags"
    11  	"github.com/rclone/rclone/fs/operations"
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  var (
    16  	expire = fs.DurationOff
    17  	unlink = false
    18  )
    19  
    20  func init() {
    21  	cmd.Root.AddCommand(commandDefinition)
    22  	cmdFlags := commandDefinition.Flags()
    23  	flags.FVarP(cmdFlags, &expire, "expire", "", "The amount of time that the link will be valid", "")
    24  	flags.BoolVarP(cmdFlags, &unlink, "unlink", "", unlink, "Remove existing public link to file/folder", "")
    25  }
    26  
    27  var commandDefinition = &cobra.Command{
    28  	Use:   "link remote:path",
    29  	Short: `Generate public link to file/folder.`,
    30  	Long: `rclone link will create, retrieve or remove a public link to the given
    31  file or folder.
    32  
    33      rclone link remote:path/to/file
    34      rclone link remote:path/to/folder/
    35      rclone link --unlink remote:path/to/folder/
    36      rclone link --expire 1d remote:path/to/file
    37  
    38  If you supply the --expire flag, it will set the expiration time
    39  otherwise it will use the default (100 years). **Note** not all
    40  backends support the --expire flag - if the backend doesn't support it
    41  then the link returned won't expire.
    42  
    43  Use the --unlink flag to remove existing public links to the file or
    44  folder. **Note** not all backends support "--unlink" flag - those that
    45  don't will just ignore it.
    46  
    47  If successful, the last line of the output will contain the
    48  link. Exact capabilities depend on the remote, but the link will
    49  always by default be created with the least constraints – e.g. no
    50  expiry, no password protection, accessible without account.
    51  `,
    52  	Annotations: map[string]string{
    53  		"versionIntroduced": "v1.41",
    54  	},
    55  	Run: func(command *cobra.Command, args []string) {
    56  		cmd.CheckArgs(1, 1, command, args)
    57  		fsrc, remote := cmd.NewFsFile(args[0])
    58  		cmd.Run(false, false, command, func() error {
    59  			link, err := operations.PublicLink(context.Background(), fsrc, remote, expire, unlink)
    60  			if err != nil {
    61  				return err
    62  			}
    63  			if link != "" {
    64  				fmt.Println(link)
    65  			}
    66  			return nil
    67  		})
    68  	},
    69  }