Using certain components from RClone in Golang

I am looking to use some of the RClone components in another Golang programme. I have imported the components I need and all seems to be working fine. To avoid potential breaking changes in the future though, I thought it may be best to pass the Args to RClone's cmd.Main so that it is as close to the equivalent of calling RClone as an executable.

		fmt.Println("Creating encrypted configuration...")

		// Create the local storage mount
		os.Args = []string{os.Args[0]}
		os.Args = append(os.Args,
			"config",
			"create",
			"storage",
			"local",
			"--config",
			"rclone.conf")

		cmd.Main()

		// Create the encrypted storage mount
		randomPassword, err := encode.GenerateRandomString(32)

		if err != nil {
			return err
		}

		os.Args = []string{os.Args[0]}
		os.Args = append(os.Args,
			"config",
			"create",
			"encrypted_storage",
			"crypt",
			"remote=storage",
			"password="+randomPassword,
			"--non-interactive",
			"--obscure",
			"--config",
			"rclone.conf")

		cmd.Main()

The issue is that calling cmd.Main() twice creates a conflict where the flags are trying to be defined twice, all sourced from the pflag package:

panic: ./rclone-app flag redefined: verbose

I think I understand the issue, it is trying to redeclare flags it already declared on the first run (rclone/configflags.go at 1107da7247db32199fde92f42c916bcb4c60dc3f · rclone/rclone · GitHub). Has anyone found a better workflow for this?

In addition to the above two commands I will eventually look to implement a mount of encrypted_storage too, so will need a way to pass in to the mount command as well as the config.

I am not sure it is enough, I vaguely remember having seen parameters being read during the initialization before cmd.Main() such as this little hack (introduced by me):

If you really want to ensure correct setup like the command line, then your only option is to call the command line similar to this:

So I suggest you also take a look at the RC interface - it can perform most of the command line options - and has some extra options - e.g. statistics.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.