Skip to content

Commit

Permalink
feat: add unmount volume command
Browse files Browse the repository at this point in the history
  • Loading branch information
alvinrizki-gtf committed Dec 23, 2022
1 parent 222f2df commit e300cb0
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 4 deletions.
8 changes: 4 additions & 4 deletions cmd/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,23 @@ var mount = &cobra.Command{
return
}

Box := box.New(box.Config{
infoBox := box.New(box.Config{
Px: 2,
Py: 1,
Type: "Classic",
Color: "Cyan"})

fmt.Println()
Box.Print(
infoBox.Print(
"NTFS volume mounted succesfully!",
fmt.Sprintf(`Partition mounted on /Volumes/%s. Please do copy-paste there.
Finder will open automatically. Or You can open the directory using the command below.
$ ntfs-wr open /Volumes/%s
$ ntfs-wr open %s
Use this command to unmount the partition:
$ sudo ntfs-wr unmount /Volumes/%s
$ sudo ntfs-wr unmount %s
`, dirName, dirName, dirName),
)
},
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func init() {
RootCmd.AddCommand(listExternalDisk)
RootCmd.AddCommand(mount)
RootCmd.AddCommand(openVolume)
RootCmd.AddCommand(unmount)
}

var RootCmd = &cobra.Command{
Expand Down
58 changes: 58 additions & 0 deletions cmd/unmount.go
Original file line number Diff line number Diff line change
@@ -1 +1,59 @@
package cmd

import (
"fmt"

"github.com/cikupin/ntfs-wr/pkg"
zlog "github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

var unmount = &cobra.Command{
Use: "unmount",
Short: "Unmount external disk partition",
Long: "Unmount external disk partition",
Example: "$ ntfs-wr unmount <volume_dir_name>",
PreRun: func(*cobra.Command, []string) {
if !pkg.IsMacOS() {
zlog.Fatal().Msg("this binary runs only for MacOS")
}

if !pkg.IsRoot() {
zlog.Fatal().Msg("this binary runs only on root user")
}
},
Run: func(c *cobra.Command, args []string) {
if len(args) == 0 {
zlog.Error().Msg("please supply volume dirname as an argument. exiting now...")
return
}

dirName := args[0]
if !pkg.IsVolumeExist(dirName) {
zlog.Error().
Str("volume_dir", fmt.Sprintf("/Volumes/%s", dirName)).
Msg("volume directory is not exist")
return
}

err := pkg.UnmountDisk(dirName)
if err != nil {
zlog.Fatal().Err(err).
Str("full_path", fmt.Sprintf("/Volumes/%s", dirName)).
Msg("cannot unmount volume")
return
}

err = pkg.RemoveVolumeDir(dirName)
if err != nil {
zlog.Warn().
Str("full_path", fmt.Sprintf("/Volumes/%s", dirName)).
Msg("volume unmounted succesfully, but failed to remove temporary volume dir. you can manually delete it.")
return
}

zlog.Info().
Str("full_path", fmt.Sprintf("/Volumes/%s", dirName)).
Msg("volume unmounted succesfully")
},
}
18 changes: 18 additions & 0 deletions pkg/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os/exec"
"regexp"
"strings"
"time"

zlog "github.com/rs/zerolog/log"
)
Expand Down Expand Up @@ -42,6 +43,7 @@ func getMappedDisk(info string) map[string]string {
return mappedDisk
}

// MountDisk mounts partition to the given volume dir name
func MountDisk(disk string, volumeDirName string) error {
zlog.Info().Msg("mounting volume is in progres...")

Expand All @@ -53,3 +55,19 @@ func MountDisk(disk string, volumeDirName string) error {
}
return nil
}

func UnmountDisk(volumeDirName string) error {
zlog.Info().Msg("unmounting volume is in progres...")

commands := fmt.Sprintf("umount /Volumes/%s", volumeDirName)

_, err := exec.Command("bash", "-c", commands).Output()
if err != nil {
return err
}

// giving some time to make sure that the volume unmounted safely
time.Sleep(2 * time.Second)

return nil
}
5 changes: 5 additions & 0 deletions pkg/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,8 @@ func OpenVolumeDir(volumeDirName string) error {
}
return nil
}

// RemoveVolumeDir removes the given directory on /Volumes path
func RemoveVolumeDir(volumeDirName string) error {
return os.RemoveAll(fmt.Sprintf("/Volumes/%s", volumeDirName))
}

0 comments on commit e300cb0

Please sign in to comment.