rc: add core/disks to enumerate attached disks
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
//go:build netbsd && 386
|
||||
|
||||
package rc
|
||||
|
||||
// getMounts returns a slice of disk mount points
|
||||
func getMounts() (mounts []string) {
|
||||
return []string{"/"}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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, "/"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user