Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
minhio committed Jan 10, 2024
1 parent 6449e2f commit e627b9d
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 17 deletions.
24 changes: 7 additions & 17 deletions pkg/devpod/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package devpod

import (
"encoding/base64"
"errors"
"os"
"path/filepath"

Expand Down Expand Up @@ -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 {
Expand All @@ -80,28 +73,25 @@ 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),
multipass.SetLaunchCpus(opts.Cpus),
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
}
5 changes: 5 additions & 0 deletions pkg/multipass/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package multipass

import (
"fmt"
"log"
"os/exec"
"strings"
)
Expand All @@ -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
Expand All @@ -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)) {
Expand Down
3 changes: 3 additions & 0 deletions pkg/multipass/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package multipass

import (
"fmt"
"log"
"os"
"os/exec"
)

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()

Expand Down
5 changes: 5 additions & 0 deletions pkg/multipass/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package multipass
import (
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
)
Expand All @@ -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()

Expand All @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions pkg/multipass/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package multipass

import (
"fmt"
"log"
"os"
"os/exec"
"strconv"
Expand All @@ -14,6 +15,7 @@ type launchArgs struct {
memory string
cloudInit string
image string
mounts []MountArg
}

type argSetter func(*launchArgs)
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -97,3 +107,9 @@ func SetLaunchImage(image string) argSetter {
args.image = image
}
}

func SetMounts(mounts []MountArg) argSetter {
return func(args *launchArgs) {
args.mounts = mounts
}
}
5 changes: 5 additions & 0 deletions pkg/multipass/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package multipass
import (
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
)
Expand All @@ -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()

Expand All @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions pkg/multipass/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package multipass

import (
"fmt"
"log"
"os"
"os/exec"
)
Expand All @@ -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()

Expand Down
3 changes: 3 additions & 0 deletions pkg/multipass/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package multipass

import (
"fmt"
"log"
"os"
"os/exec"
)

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()

Expand Down
3 changes: 3 additions & 0 deletions pkg/multipass/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package multipass

import (
"fmt"
"log"
"os"
"os/exec"
)

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()

Expand Down
5 changes: 5 additions & 0 deletions pkg/multipass/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package multipass
import (
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
)
Expand All @@ -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()

Expand All @@ -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 {
Expand Down

0 comments on commit e627b9d

Please sign in to comment.