Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure the specs are uppercase #22

Merged
merged 1 commit into from
May 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cmd/machina/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package machina
import (
"fmt"
"os"
"strings"

"github.com/enkodr/machina/internal/vm"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -52,11 +53,11 @@ var createCommand = &cobra.Command{
}
// Check if memory was passed by flag
if newMem != "" {
vm.Specs.Memory = newMem
vm.Specs.Memory = strings.ToUpper(newMem)
}
// Check if disk was passed by flag
if newDisk != "" {
vm.Specs.Disk = newDisk
vm.Specs.Disk = strings.ToUpper(newDisk)
}

// Prepare necessary files for machine creation
Expand Down
18 changes: 10 additions & 8 deletions internal/vm/tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@ import (

var endpoint = "https://raw.githubusercontent.com/enkodr/machina/main/templates"

type Filer interface {
type Templater interface {
Load() (*VMConfig, error)
}

type LocalFile struct {
type LocalTemplate struct {
path string
}
type RemoteFile struct {
type RemoteTemplate struct {
name string
}

func NewTemplate(name string) Filer {
func NewTemplate(name string) Templater {
if strings.Contains(name, ".yaml") {
return &LocalFile{path: name}
return &LocalTemplate{path: name}
} else {
return &RemoteFile{name: name}
return &RemoteTemplate{name: name}
}
}

func (f *LocalFile) Load() (*VMConfig, error) {
func (f *LocalTemplate) Load() (*VMConfig, error) {
// Get the template content
tpl, err := os.ReadFile(f.path)

Expand All @@ -50,7 +50,7 @@ func (f *LocalFile) Load() (*VMConfig, error) {
return vm, nil
}

func (f *RemoteFile) Load() (*VMConfig, error) {
func (f *RemoteTemplate) Load() (*VMConfig, error) {
// Get the template content
tplFile := fmt.Sprintf("%s/%s.yaml", endpoint, f.name)
tpl, err := netutil.Download(tplFile)
Expand Down Expand Up @@ -97,6 +97,8 @@ func parseTemplate(tpl []byte) (*VMConfig, error) {
base.Mount = Mount{}
mergo.Merge(vm, base)
}
vm.Specs.Disk = strings.ToUpper(vm.Specs.Disk)
vm.Specs.Memory = strings.ToUpper(vm.Specs.Memory)

return vm, nil
}
Expand Down
18 changes: 9 additions & 9 deletions internal/vm/tpl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
func TestNewTemplate(t *testing.T) {
testCases := []struct {
name string
wantType Filer
wantType Templater
}{
{"local.yaml", &LocalFile{}},
{"default", &RemoteFile{}},
{"ubuntu.yaml", &LocalFile{}},
{"ubuntu", &RemoteFile{}},
{"local.yaml", &LocalTemplate{}},
{"default", &RemoteTemplate{}},
{"ubuntu.yaml", &LocalTemplate{}},
{"ubuntu", &RemoteTemplate{}},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -45,7 +45,7 @@ specs:
defer f.Close()
defer os.Remove(f.Name())
f.Write(want)
localFile := &LocalFile{path: f.Name()}
localFile := &LocalTemplate{path: f.Name()}
got, _ := localFile.Load()
assert.Equal(t, got.Name, "TestVM")
assert.Equal(t, got.Specs.CPUs, "2")
Expand All @@ -58,7 +58,7 @@ func TestLocalFileLoadInvalidData(t *testing.T) {
name := "template.yaml"
f, _ := os.CreateTemp("", name)
f.Write([]byte(want))
localFile := &LocalFile{path: f.Name()}
localFile := &LocalTemplate{path: f.Name()}
vm, err := localFile.Load()
assert.Error(t, err)
assert.Nil(t, vm)
Expand All @@ -73,7 +73,7 @@ func TestRemoteFileValidName(t *testing.T) {
err := yaml.Unmarshal(data, want)
assert.NoError(t, err)

remoteFile := &RemoteFile{name: name}
remoteFile := &RemoteTemplate{name: name}
got, _ := remoteFile.Load()

assert.Equal(t, want.Name, got.Name)
Expand All @@ -86,7 +86,7 @@ func TestRemoteFileValidName(t *testing.T) {
func TestRemoteFileInvalidName(t *testing.T) {
name := "invalid"

remoteFile := &RemoteFile{name: name}
remoteFile := &RemoteTemplate{name: name}
vm, err := remoteFile.Load()

assert.Error(t, err)
Expand Down