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

     1  // Package rcd provides the rcd command.
     2  package rcd
     3  
     4  import (
     5  	"context"
     6  	"log"
     7  
     8  	"github.com/rclone/rclone/cmd"
     9  	"github.com/rclone/rclone/fs/rc/rcflags"
    10  	"github.com/rclone/rclone/fs/rc/rcserver"
    11  	libhttp "github.com/rclone/rclone/lib/http"
    12  	"github.com/rclone/rclone/lib/systemd"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  func init() {
    17  	cmd.Root.AddCommand(commandDefinition)
    18  }
    19  
    20  var commandDefinition = &cobra.Command{
    21  	Use:   "rcd <path to files to serve>*",
    22  	Short: `Run rclone listening to remote control commands only.`,
    23  	Long: `This runs rclone so that it only listens to remote control commands.
    24  
    25  This is useful if you are controlling rclone via the rc API.
    26  
    27  If you pass in a path to a directory, rclone will serve that directory
    28  for GET requests on the URL passed in.  It will also open the URL in
    29  the browser when rclone is run.
    30  
    31  See the [rc documentation](/rc/) for more info on the rc flags.
    32  
    33  ` + libhttp.Help(rcflags.FlagPrefix) + libhttp.TemplateHelp(rcflags.FlagPrefix) + libhttp.AuthHelp(rcflags.FlagPrefix),
    34  	Annotations: map[string]string{
    35  		"versionIntroduced": "v1.45",
    36  		"groups":            "RC",
    37  	},
    38  	Run: func(command *cobra.Command, args []string) {
    39  		cmd.CheckArgs(0, 1, command, args)
    40  		if rcflags.Opt.Enabled {
    41  			log.Fatalf("Don't supply --rc flag when using rcd")
    42  		}
    43  
    44  		// Start the rc
    45  		rcflags.Opt.Enabled = true
    46  		if len(args) > 0 {
    47  			rcflags.Opt.Files = args[0]
    48  		}
    49  
    50  		s, err := rcserver.Start(context.Background(), &rcflags.Opt)
    51  		if err != nil {
    52  			log.Fatalf("Failed to start remote control: %v", err)
    53  		}
    54  		if s == nil {
    55  			log.Fatal("rc server not configured")
    56  		}
    57  
    58  		// Notify stopping on exit
    59  		defer systemd.Notify()()
    60  
    61  		s.Wait()
    62  	},
    63  }