From 67fc227684616fd210e81b26766c58e780e5e199 Mon Sep 17 00:00:00 2001
From: Nick Craig-Wood <nick@craig-wood.com>
Date: Fri, 2 Dec 2022 13:44:08 +0000
Subject: [PATCH] config: add config/setpath for setting config path via
 rc/librclone

---
 fs/config/rc.go         | 24 ++++++++++++++++++++++++
 fs/config/rc_test.go    | 18 ++++++++++++++++++
 librclone/ctest/ctest.c |  3 +++
 3 files changed, 45 insertions(+)

diff --git a/fs/config/rc.go b/fs/config/rc.go
index 78f6d4866..f5902628d 100644
--- a/fs/config/rc.go
+++ b/fs/config/rc.go
@@ -217,3 +217,27 @@ func rcDelete(ctx context.Context, in rc.Params) (out rc.Params, err error) {
 	DeleteRemote(name)
 	return nil, nil
 }
+
+func init() {
+	rc.Add(rc.Call{
+		Path:         "config/setpath",
+		Fn:           rcSetPath,
+		Title:        "Set the path of the config file",
+		AuthRequired: true,
+		Help: `
+Parameters:
+
+- path - path to the config file to use
+`,
+	})
+}
+
+// Set the config file path
+func rcSetPath(ctx context.Context, in rc.Params) (out rc.Params, err error) {
+	path, err := in.GetString("path")
+	if err != nil {
+		return nil, err
+	}
+	err = SetConfigPath(path)
+	return nil, err
+}
diff --git a/fs/config/rc_test.go b/fs/config/rc_test.go
index 0136f869a..fc9f37563 100644
--- a/fs/config/rc_test.go
+++ b/fs/config/rc_test.go
@@ -153,3 +153,21 @@ func TestRcProviders(t *testing.T) {
 	}
 	assert.True(t, foundLocal, "didn't find local provider")
 }
+
+func TestRcSetPath(t *testing.T) {
+	oldPath := config.GetConfigPath()
+	newPath := oldPath + ".newPath"
+	call := rc.Calls.Get("config/setpath")
+	assert.NotNil(t, call)
+	in := rc.Params{
+		"path": newPath,
+	}
+	_, err := call.Fn(context.Background(), in)
+	require.NoError(t, err)
+	assert.Equal(t, newPath, config.GetConfigPath())
+
+	in["path"] = oldPath
+	_, err = call.Fn(context.Background(), in)
+	require.NoError(t, err)
+	assert.Equal(t, oldPath, config.GetConfigPath())
+}
diff --git a/librclone/ctest/ctest.c b/librclone/ctest/ctest.c
index a3ee72b26..64b1bb1e8 100644
--- a/librclone/ctest/ctest.c
+++ b/librclone/ctest/ctest.c
@@ -113,6 +113,9 @@ int main(int argc, char** argv) {
     /* testCopyFile(); */
     /* testListRemotes(); */
 
+    /* testRPC("config/setpath", "{\"path\":\"/tmp/rclone.conf\"}"); */
+    /* testRPC("config/listremotes", "{}"); */
+
     RcloneFinalize();
     return EXIT_SUCCESS;
 }