Skip to content

Commit

Permalink
chore: clean code-related to template
Browse files Browse the repository at this point in the history
  • Loading branch information
aajkl committed Jul 15, 2024
1 parent bd4456d commit 7ddf6bb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 45 deletions.
49 changes: 13 additions & 36 deletions internal/types/cloud_init.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package types

import (
"bytes"
_ "embed"
"encoding/base64"
"os"
"os/exec"
"path/filepath"
"text/template"

"github.com/Masterminds/sprig/v3"
"github.com/projecteru2/yavirt/configs"
"github.com/projecteru2/yavirt/internal/utils/template"

"github.com/kdomanski/iso9660/util"

Expand All @@ -23,10 +22,6 @@ var (
metaData string
//go:embed templates/network-config.yaml
networkData string

userDataTpl *template.Template
metaDataTpl *template.Template
networkTpl *template.Template
)

type CloudInitGateway struct {
Expand Down Expand Up @@ -54,29 +49,7 @@ type CloudInitConfig struct {
DefaultGW CloudInitGateway `json:"-"`
}

func initTpls() (err error) {
if userDataTpl == nil {
if userDataTpl, err = template.New("userdata").Funcs(sprig.TxtFuncMap()).Parse(userData); err != nil {
return
}
}
if metaDataTpl == nil {
if metaDataTpl, err = template.New("metadata").Funcs(sprig.TxtFuncMap()).Parse(metaData); err != nil {
return
}
}
if networkTpl == nil {
if networkTpl, err = template.New("network").Funcs(sprig.TxtFuncMap()).Parse(networkData); err != nil {
return
}
}
return
}

func (ciCfg *CloudInitConfig) GenFilesContent() (string, string, string, error) {
if err := initTpls(); err != nil {
return "", "", "", err
}
d1 := map[string]any{
"username": ciCfg.Username,
"password": ciCfg.Password,
Expand All @@ -98,24 +71,28 @@ func (ciCfg *CloudInitConfig) GenFilesContent() (string, string, string, error)
"content": base64.StdEncoding.EncodeToString(v),
})
}
var userDataBs bytes.Buffer
if err := userDataTpl.Execute(&userDataBs, d1); err != nil {
udataTmplFile := filepath.Join(configs.Conf.VirtTmplDir, "user-data.yaml")
mdataTmplFile := filepath.Join(configs.Conf.VirtTmplDir, "meta-data.yaml")
networkTmplFile := filepath.Join(configs.Conf.VirtTmplDir, "network-config.yaml")

uDataBS, err := template.Render(udataTmplFile, userData, d1)
if err != nil {
return "", "", "", err
}

d2 := map[string]string{
"instanceID": ciCfg.InstanceID,
"hostname": ciCfg.Hostname,
}
var metaDataBs bytes.Buffer
if err := metaDataTpl.Execute(&metaDataBs, d2); err != nil {
mDataBS, err := template.Render(mdataTmplFile, metaData, d2)
if err != nil {
return "", "", "", err
}
var networkBs bytes.Buffer
if err := networkTpl.Execute(&networkBs, d1); err != nil {
networkBS, err := template.Render(networkTmplFile, networkData, d1)
if err != nil {
return "", "", "", err
}
return userDataBs.String(), metaDataBs.String(), networkBs.String(), nil
return string(uDataBS), string(mDataBS), string(networkBS), nil
}

func (ciCfg *CloudInitConfig) GenerateISO(fname string) (err error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"
"os"
"strings"
"sync"
text "text/template"

Expand Down Expand Up @@ -52,9 +51,6 @@ func (t *Templates) get(fpth string, defaultTemplStr string) (tmpl *text.Templat
}

func (t *Templates) parse(fpth string, defaultTemplStr string) (*text.Template, error) {
if !t.isTempl(fpth) {
return nil, errors.Errorf("%s is not a template file", fpth)
}
f, err := os.Open(fpth)
// if file path doesn't exist, then use default template string
if err != nil && os.IsNotExist(err) {
Expand All @@ -73,7 +69,3 @@ func (t *Templates) parse(fpth string, defaultTemplStr string) (*text.Template,
}
return text.New(fpth).Funcs(sprig.TxtFuncMap()).Parse(string(buf))
}

func (t *Templates) isTempl(fpth string) bool {
return strings.HasSuffix(fpth, ".xml")
}
2 changes: 1 addition & 1 deletion internal/virt/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/projecteru2/yavirt/internal/models"
"github.com/projecteru2/yavirt/internal/network"
"github.com/projecteru2/yavirt/internal/types"
"github.com/projecteru2/yavirt/internal/virt/template"
"github.com/projecteru2/yavirt/internal/utils/template"
"github.com/projecteru2/yavirt/internal/vmcache"
"github.com/projecteru2/yavirt/pkg/libvirt"
"github.com/projecteru2/yavirt/pkg/terrors"
Expand Down

0 comments on commit 7ddf6bb

Please sign in to comment.