Skip to content

Commit 0a3bdbc

Browse files
committed
feat: add force flag to umount command
1 parent e1d1edd commit 0a3bdbc

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ var mountCmd = &cobra.Command{
8080
Run: mount,
8181
}
8282

83+
var umountCmdFlags struct {
84+
Force bool
85+
}
86+
87+
func init() {
88+
umountCmd.Flags().BoolVarP(&umountCmdFlags.Force, "force", "f", false, "Force unmount the drive(s)")
89+
}
90+
8391
var umountCmd = &cobra.Command{
8492
Use: "umount",
8593
Short: "Unmount a drive",

mount.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"log"
88
"os"
9+
"os/exec"
910

1011
"github.com/charmbracelet/lipgloss"
1112
"github.com/charmbracelet/lipgloss/table"
@@ -83,9 +84,21 @@ func umount(cmd *cobra.Command, args []string) {
8384
continue
8485
}
8586
log.Println("Umounted Drive:", arg)
87+
88+
if umountCmdFlags.Force {
89+
forceUmount(cmd.Context(), arg)
90+
}
8691
}
8792
}
8893

94+
// forceUmount calls fusermount -u to force unmount the drive in addition to stopping the systemd service.
95+
// This doesnt always work, but it is a good last resort.
96+
// Errors are always ignored, as fusermount -u will return an error if the drive is not mounted.
97+
func forceUmount(ctx context.Context, driveName string) {
98+
drivePath := getDriveDataPath(driveName)
99+
exec.CommandContext(ctx, "/bin/fusermount", "-u", drivePath).Run() //nolint:errcheck
100+
}
101+
89102
func list(cmd *cobra.Command, _ []string) {
90103
conn, err := dbus.NewUserConnectionContext(cmd.Context())
91104
if err != nil {

0 commit comments

Comments
 (0)