From e627b9de4bd52d5e1a011018da37252e95a6ed2c Mon Sep 17 00:00:00 2001 From: Minh Hoang Nguyen <983465+minhio@users.noreply.github.com> Date: Wed, 10 Jan 2024 05:25:59 +0000 Subject: [PATCH] refactor --- pkg/devpod/create.go | 24 +++++++----------------- pkg/multipass/client.go | 5 +++++ pkg/multipass/delete.go | 3 +++ pkg/multipass/info.go | 5 +++++ pkg/multipass/launch.go | 16 ++++++++++++++++ pkg/multipass/list.go | 5 +++++ pkg/multipass/mount.go | 3 +++ pkg/multipass/start.go | 3 +++ pkg/multipass/stop.go | 3 +++ pkg/multipass/version.go | 5 +++++ 10 files changed, 55 insertions(+), 17 deletions(-) diff --git a/pkg/devpod/create.go b/pkg/devpod/create.go index 221d76d..3c2505a 100644 --- a/pkg/devpod/create.go +++ b/pkg/devpod/create.go @@ -2,7 +2,6 @@ package devpod import ( "encoding/base64" - "errors" "os" "path/filepath" @@ -52,12 +51,6 @@ func Create() error { return err } - // init multipass client - client, err := multipass.NewClient(opts.Path) - if err != nil { - return err - } - // generate and write public key (and private key) to machine folder publicKeyBase, err := ssh.GetPublicKeyBase(machine.Folder) if err != nil { @@ -80,6 +73,12 @@ func Create() error { return err } + // init multipass client + client, err := multipass.NewClient(opts.Path) + if err != nil { + return err + } + // launch the multipass instance err = client.Launch( multipass.SetLaunchName(machine.ID), @@ -87,21 +86,12 @@ func Create() error { multipass.SetLaunchDisk(opts.DiskSize), multipass.SetLaunchMemory(opts.Memory), multipass.SetLaunchCloudInit(cloudInitFilePath), + multipass.SetMounts(mounts), multipass.SetLaunchImage(opts.Image), ) if err != nil { return err } - // mounts - mountErr := client.Mount(machine.ID, mounts...) - if err != nil { - delErr := client.Delete(machine.ID) - if delErr != nil { - return errors.Join(mountErr, delErr) - } - return err - } - return nil } diff --git a/pkg/multipass/client.go b/pkg/multipass/client.go index cabfc77..9c71f6e 100644 --- a/pkg/multipass/client.go +++ b/pkg/multipass/client.go @@ -2,6 +2,7 @@ package multipass import ( "fmt" + "log" "os/exec" "strings" ) @@ -11,6 +12,8 @@ type client struct { } func NewClient(executablePath string) (*client, error) { + log.Default().Printf("executable path: %s", executablePath) + _, err := exec.LookPath(executablePath) if err != nil { return nil, err @@ -24,6 +27,8 @@ func NewClient(executablePath string) (*client, error) { } func (c *client) GetInstance(name string) (*instanceInfo, error) { + log.Default().Printf("get instance: %s", name) + infoResult, err := c.Info(name) if err != nil { if strings.Contains(err.Error(), fmt.Sprintf("instance \"%s\" does not exist", name)) { diff --git a/pkg/multipass/delete.go b/pkg/multipass/delete.go index f109cce..d8da11b 100644 --- a/pkg/multipass/delete.go +++ b/pkg/multipass/delete.go @@ -2,6 +2,7 @@ package multipass import ( "fmt" + "log" "os" "os/exec" ) @@ -9,6 +10,8 @@ import ( func (c *client) Delete(name string) error { args := []string{"delete", "--purge", name} + log.Default().Printf("delete args: %s", args) + cmd := exec.Command(c.executablePath, args...) cmd.Env = os.Environ() diff --git a/pkg/multipass/info.go b/pkg/multipass/info.go index 0eee0fe..8898fe8 100644 --- a/pkg/multipass/info.go +++ b/pkg/multipass/info.go @@ -3,6 +3,7 @@ package multipass import ( "encoding/json" "fmt" + "log" "os" "os/exec" ) @@ -24,6 +25,8 @@ type infoResult struct { func (c *client) Info(name string) (*infoResult, error) { args := []string{"info", "--format", "json", name} + log.Default().Printf("info args: %s", args) + cmd := exec.Command(c.executablePath, args...) cmd.Env = os.Environ() @@ -32,6 +35,8 @@ func (c *client) Info(name string) (*infoResult, error) { return nil, fmt.Errorf("%s %s", string(out), err.Error()) } + log.Default().Printf("info result: %s", out) + var result infoResult err = json.Unmarshal(out, &result) if err != nil { diff --git a/pkg/multipass/launch.go b/pkg/multipass/launch.go index 9a08a9e..96cfa4d 100644 --- a/pkg/multipass/launch.go +++ b/pkg/multipass/launch.go @@ -2,6 +2,7 @@ package multipass import ( "fmt" + "log" "os" "os/exec" "strconv" @@ -14,6 +15,7 @@ type launchArgs struct { memory string cloudInit string image string + mounts []MountArg } type argSetter func(*launchArgs) @@ -47,10 +49,18 @@ func (c *client) Launch(argSetters ...argSetter) error { args = append(args, "--cloud-init", launchArgz.cloudInit) } + if len(launchArgz.mounts) > 0 { + for _, mount := range launchArgz.mounts { + args = append(args, "--mount", mount.Source+":"+mount.Target) + } + } + if launchArgz.image != "" { args = append(args, launchArgz.image) } + log.Default().Printf("launch args: %s", args) + cmd := exec.Command(c.executablePath, args...) cmd.Env = os.Environ() @@ -97,3 +107,9 @@ func SetLaunchImage(image string) argSetter { args.image = image } } + +func SetMounts(mounts []MountArg) argSetter { + return func(args *launchArgs) { + args.mounts = mounts + } +} diff --git a/pkg/multipass/list.go b/pkg/multipass/list.go index 74027be..f2a5f4a 100644 --- a/pkg/multipass/list.go +++ b/pkg/multipass/list.go @@ -3,6 +3,7 @@ package multipass import ( "encoding/json" "fmt" + "log" "os" "os/exec" ) @@ -19,6 +20,8 @@ type listResult struct { func (c *client) List() (*listResult, error) { args := []string{"list", "--format", "json"} + log.Default().Printf("list args: %s", args) + cmd := exec.Command(c.executablePath, args...) cmd.Env = os.Environ() @@ -27,6 +30,8 @@ func (c *client) List() (*listResult, error) { return nil, fmt.Errorf("%s %s", string(out), err.Error()) } + log.Default().Printf("list result: %s", out) + var result listResult err = json.Unmarshal(out, &result) if err != nil { diff --git a/pkg/multipass/mount.go b/pkg/multipass/mount.go index d316d26..9739655 100644 --- a/pkg/multipass/mount.go +++ b/pkg/multipass/mount.go @@ -2,6 +2,7 @@ package multipass import ( "fmt" + "log" "os" "os/exec" ) @@ -24,6 +25,8 @@ 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} + log.Default().Printf("mount args: %s", args) + cmd := exec.Command(c.executablePath, args...) cmd.Env = os.Environ() diff --git a/pkg/multipass/start.go b/pkg/multipass/start.go index ed3937a..bda6ee0 100644 --- a/pkg/multipass/start.go +++ b/pkg/multipass/start.go @@ -2,6 +2,7 @@ package multipass import ( "fmt" + "log" "os" "os/exec" ) @@ -9,6 +10,8 @@ import ( func (c *client) Start(name string) error { args := []string{"start", name} + log.Default().Printf("start args: %s", args) + cmd := exec.Command(c.executablePath, args...) cmd.Env = os.Environ() diff --git a/pkg/multipass/stop.go b/pkg/multipass/stop.go index 4b173ba..5a21fb0 100644 --- a/pkg/multipass/stop.go +++ b/pkg/multipass/stop.go @@ -2,6 +2,7 @@ package multipass import ( "fmt" + "log" "os" "os/exec" ) @@ -9,6 +10,8 @@ import ( func (c *client) Stop(name string) error { args := []string{"stop", name} + log.Default().Printf("stop args: %s", args) + cmd := exec.Command(c.executablePath, args...) cmd.Env = os.Environ() diff --git a/pkg/multipass/version.go b/pkg/multipass/version.go index f0a8d9b..548926a 100644 --- a/pkg/multipass/version.go +++ b/pkg/multipass/version.go @@ -3,6 +3,7 @@ package multipass import ( "encoding/json" "fmt" + "log" "os" "os/exec" ) @@ -15,6 +16,8 @@ type versionResult struct { func (c *client) Version() (*versionResult, error) { args := []string{"version", "--format", "json"} + log.Default().Printf("version args: %s", args) + cmd := exec.Command(c.executablePath, args...) cmd.Env = os.Environ() @@ -23,6 +26,8 @@ func (c *client) Version() (*versionResult, error) { return nil, fmt.Errorf("%s %s", string(out), err.Error()) } + log.Default().Printf("version result: %s", out) + var result versionResult err = json.Unmarshal(out, &result) if err != nil {