Skip to content

Commit

Permalink
refactor mounts
Browse files Browse the repository at this point in the history
  • Loading branch information
minhio committed Jan 12, 2024
1 parent b466f54 commit 74b671c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 37 deletions.
5 changes: 1 addition & 4 deletions pkg/devpod/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ func Create() error {
}

// parse mount args
mounts := parseMountArgs(opts.Mounts)

// validate mount args
err = validateMountArgs(mounts...)
mounts, err := parseMountArgs(opts.Mounts)
if err != nil {
return err
}
Expand Down
5 changes: 1 addition & 4 deletions pkg/devpod/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ func Init() error {
}

// parse mount args
mounts := parseMountArgs(opts.Mounts)

// validate mount args
err = validateMountArgs(mounts...)
_, err = parseMountArgs(opts.Mounts)
if err != nil {
return err
}
Expand Down
44 changes: 16 additions & 28 deletions pkg/devpod/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@ package devpod
import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/minhio/devpod-provider-multipass/pkg/multipass"
)

func parseMountArgs(mountOpt string) []multipass.MountArg {
mountArgs := make([]multipass.MountArg, 0)

func parseMountArgs(mountOpt string) ([]multipass.MountArg, error) {
if mountOpt == "" {
return mountArgs
return nil, nil
}

mountsFromOpt := strings.Split(mountOpt, ",")
mountArgs := make([]multipass.MountArg, 0)

for _, mount := range mountsFromOpt {
for _, mount := range strings.Split(mountOpt, ",") {
sourceAndTarget := strings.Split(mount, ":")

source := sourceAndTarget[0]
var target string

if len(sourceAndTarget) == 1 {
target = source
} else if len(sourceAndTarget) == 2 {
target = sourceAndTarget[1]
source := filepath.Join(sourceAndTarget[0])
if _, err := os.Stat(source); os.IsNotExist(err) {
return nil, fmt.Errorf("%s does not exist", source)
}

if !strings.HasPrefix(target, "/") {
target = "/home/devpod/" + target
target := ""
if len(sourceAndTarget) == 2 {
if strings.HasPrefix(sourceAndTarget[1], "/") {
target = filepath.Join(sourceAndTarget[1])
} else {
target = filepath.Join("/", "home", "devpod", sourceAndTarget[1])
}
}

mountArgs = append(mountArgs, multipass.MountArg{
Expand All @@ -39,17 +39,5 @@ func parseMountArgs(mountOpt string) []multipass.MountArg {
})
}

return mountArgs
}

func validateMountArgs(mountArgs ...multipass.MountArg) error {
for _, arg := range mountArgs {
if _, err := os.Stat(arg.Source); os.IsNotExist(err) {
return fmt.Errorf("%s does not exist", arg.Source)
}
if !strings.HasPrefix(arg.Target, "/") {
return fmt.Errorf("%s is not absolute path", arg.Target)
}
}
return nil
return mountArgs, nil
}
8 changes: 7 additions & 1 deletion pkg/multipass/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ func (c *client) Mount(name string, mounts ...MountArg) error {
}

func (c *client) mount(name string, mount MountArg) error {
args := []string{"mount", mount.Source, name + ":" + mount.Target}
args := make([]string, 0)

if mount.Target == "" {
args = append(args, "mount", mount.Source, name)
} else {
args = append(args, "mount", mount.Source, name+":"+mount.Target)
}

log.Default().Printf("[multipass] %s", args)

Expand Down

0 comments on commit 74b671c

Please sign in to comment.