diff --git a/pkg/devpod/create.go b/pkg/devpod/create.go index 8937f64..47d95a4 100644 --- a/pkg/devpod/create.go +++ b/pkg/devpod/create.go @@ -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 } diff --git a/pkg/devpod/init.go b/pkg/devpod/init.go index 29810d6..3721df1 100644 --- a/pkg/devpod/init.go +++ b/pkg/devpod/init.go @@ -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 } diff --git a/pkg/devpod/utils.go b/pkg/devpod/utils.go index f2789bd..d31576f 100644 --- a/pkg/devpod/utils.go +++ b/pkg/devpod/utils.go @@ -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{ @@ -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 } diff --git a/pkg/multipass/mount.go b/pkg/multipass/mount.go index 93f3b54..ca00120 100644 --- a/pkg/multipass/mount.go +++ b/pkg/multipass/mount.go @@ -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)