Skip to content

Commit

Permalink
add mount ls command and allow mount rm to be optional based on a flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mhewedy committed Aug 23, 2020
1 parent eb3b340 commit 8e1c9b8
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 6 deletions.
34 changes: 31 additions & 3 deletions cmd/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ To mount the ~/MyHtmlProject directory to /var/www/html inside the VM:
$ vermin mount vm_01 ~/MyHtmlProject:/var/www/html
`,
Run: func(cmd *cobra.Command, args []string) {

vmName := args[0]
path := args[1]
remove, _ := cmd.Flags().GetBool("remove")

p := strings.Split(args[1], ":")
p := strings.Split(path, ":")
hostPath := p[0]
checkFilePath(hostPath)

Expand All @@ -58,7 +61,7 @@ $ vermin mount vm_01 ~/MyHtmlProject:/var/www/html
guestPath = p[1]
}

err := vms.Mount(vmName, hostPath, guestPath)
err := vms.Mount(vmName, hostPath, guestPath, remove)
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand All @@ -76,8 +79,33 @@ $ vermin mount vm_01 ~/MyHtmlProject:/var/www/html
ValidArgsFunction: listRunningVms,
}

var mountLsCmd = &cobra.Command{
Use: "ls",
Short: "list mounted directories",
Long: "list mounted directories",
Run: func(cmd *cobra.Command, args []string) {

vmName := args[0]

ps, err := vms.ListMounts(vmName)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Print(ps)
},
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("image required")
}
return nil
},
ValidArgsFunction: listRunningVms,
}

func init() {
rootCmd.AddCommand(mountCmd)
mountCmd.AddCommand(mountLsCmd)

// Here you will define your flags and configuration settings.

Expand All @@ -87,6 +115,6 @@ func init() {

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
//mountCmd.Flags().IntP("cpus", "c", 1, "Number of cpu cores")
mountCmd.Flags().BoolP("remove", "r", false, "remove mounts before doing the new mount")
//mountCmd.Flags().IntP("mem", "m", 1024, "Memory size in mega bytes")
}
57 changes: 54 additions & 3 deletions vms/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import (
"time"
)

func Mount(vmName, hostPath, guestPath string) error {
var (
mountFormat = "%-79s%-70s\n"
mountHeader = fmt.Sprintf(mountFormat, "HOST DIR", "GUEST DIR")
)

func Mount(vmName, hostPath, guestPath string, remove bool) error {
if err := checkRunningVM(vmName); err != nil {
return err
}
Expand All @@ -28,8 +33,10 @@ func Mount(vmName, hostPath, guestPath string) error {
return err
}

if err = removeMounts(vmName); err != nil {
return err
if remove {
if err = removeMounts(vmName); err != nil {
return err
}
}

absHostPath, err := filepath.Abs(hostPath)
Expand Down Expand Up @@ -61,6 +68,50 @@ func Mount(vmName, hostPath, guestPath string) error {
return nil
}

func ListMounts(vmName string) (string, error) {

out := mountHeader

paths, err := listMounts(vmName)
if err != nil {
return "", err
}

for _, p := range paths {
out += fmt.Sprintf(mountFormat, p.hostPath, p.guestPath)
}

return out, nil
}

type mountPath struct {
hostPath string
guestPath string
}

func listMounts(vmName string) ([]mountPath, error) {

result := make([]mountPath, 0)

hostPaths, err := props.FindByPrefix(vmName, "SharedFolderPathTransientMapping")
if err != nil {
return nil, err
}

transientMounts, err := props.FindByPrefix(vmName, "SharedFolderNameTransientMapping")
if err != nil {
return nil, err
}

for i := range transientMounts {
if guestPath, err := getMountGuestPath(vmName, transientMounts[i]); err == nil {
result = append(result, mountPath{hostPath: hostPaths[i], guestPath: guestPath})
}
}

return result, nil
}

func removeMounts(vmName string) error {
transientMounts, err := props.FindByPrefix(vmName, "SharedFolderNameTransientMapping")
if err != nil {
Expand Down

0 comments on commit 8e1c9b8

Please sign in to comment.