rc: add core/disks to enumerate attached disks

This commit is contained in:
Nick Craig-Wood
2026-04-23 18:47:46 +01:00
parent 9dedb12b9d
commit 79379faeac
4 changed files with 92 additions and 0 deletions
+14
View File
@@ -0,0 +1,14 @@
//go:build !(netbsd && 386)
package rc
import "github.com/shirou/gopsutil/v4/disk"
// getMounts returns a slice of disk mount points
func getMounts() (mounts []string) {
partitions, _ := disk.Partitions(false)
for _, partition := range partitions {
mounts = append(mounts, partition.Mountpoint)
}
return mounts
}
+8
View File
@@ -0,0 +1,8 @@
//go:build netbsd && 386
package rc
// getMounts returns a slice of disk mount points
func getMounts() (mounts []string) {
return []string{"/"}
}
+50
View File
@@ -614,3 +614,53 @@ func rcRunCommand(ctx context.Context, in Params) (out Params, err error) {
err = cmd.Run()
return nil, err
}
func init() {
Add(Call{
Path: "core/disks",
Fn: rcDisks,
Title: "List the local disks",
Help: `This does not take any parameters
This call is for rclone GUI programs to enumerate local disks and
important directories for doing transfers to and from. The list
returned will include the root directory and the user's home directory
and any mounted disks. The returned items should be usable directly as
remotes.
Returns:
- disks
- This is an array of strings of local disk names
`,
})
}
// Disks returns likely local disks and some other useful positions
func rcDisks(ctx context.Context, in Params) (out Params, err error) {
disks := []string{}
home, err := os.UserHomeDir()
tidy := func(s string) string {
if s != "/" {
s, _ = strings.CutSuffix(s, "/")
}
return s
}
if err == nil {
disks = append(disks, tidy(home))
}
for _, mount := range getMounts() {
mount = tidy(mount)
if runtime.GOOS == "linux" {
if strings.HasPrefix(mount, "/snap/") || strings.HasPrefix(mount, "/var/snap/") || strings.HasPrefix(mount, "/boot/") || mount == "/boot" {
// ignore boring mounts
continue
}
}
disks = append(disks, mount)
}
out = Params{
"disks": disks,
}
return out, nil
}
+20
View File
@@ -199,3 +199,23 @@ func TestCoreCommand(t *testing.T) {
test("unknown_command", "STREAM", version+errorString, true)
})
}
// core/disks: Tests local disks
func TestCoreDisks(t *testing.T) {
call := Calls.Get("core/disks")
assert.NotNil(t, call)
in := Params{}
out, err := call.Fn(context.Background(), in)
require.NoError(t, err)
require.NotNil(t, out)
require.NotNil(t, out["disks"])
disks, ok := out["disks"].([]string)
require.True(t, ok)
assert.True(t, len(disks) >= 2)
for _, disk := range disks {
assert.NotEqual(t, disk, "")
if disk != "/" {
assert.False(t, strings.HasSuffix(disk, "/"))
}
}
}