Skip to content

Commit

Permalink
refactor: move templating functions to another file
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Gągor committed Dec 19, 2024
1 parent aa4581c commit b9239e6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
34 changes: 0 additions & 34 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package parser

import (
"bytes"
"encoding/json"
"fmt"
"maps"
Expand All @@ -12,9 +11,7 @@ import (
"regexp"
"sort"
"strings"
"text/template"

"github.com/Masterminds/sprig/v3"
"github.com/rs/zerolog/log"

"github.com/tgagor/template-dockerfiles/pkg/cmd"
Expand Down Expand Up @@ -358,37 +355,6 @@ func templateLabels(labelTemplates map[string]string, configSet map[string]inter
return labels, nil
}

func templateString(pattern string, args map[string]interface{}) (string, error) {
var output bytes.Buffer
t := template.Must(template.New(pattern).Funcs(sprig.TxtFuncMap()).Parse(pattern))
if err := t.Execute(&output, args); err != nil {
return "", err
}

return output.String(), nil
}

func templateFile(templateFile string, destinationFile string, args map[string]interface{}) error {
t := template.Must(
template.New(filepath.Base(templateFile)).Funcs(sprig.TxtFuncMap()).ParseFiles(templateFile),
)

f, err := os.Create(destinationFile)
if err != nil {
log.Error().Err(err).Str("file", templateFile).Msg("Failed to create")
return err
}
defer f.Close()

// Render templates using variables
if err := t.Execute(f, args); err != nil {
log.Error().Err(err).Str("file", templateFile).Msg("Failed to template")
return err
}

return nil
}

func sanitizeForFileName(input string) string {
// Replace any character that is not a letter, number, or safe symbol (-, _) with an underscore
reg := regexp.MustCompile(`[^a-zA-Z0-9-_\.]+`)
Expand Down
42 changes: 42 additions & 0 deletions pkg/parser/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package parser

import (
"bytes"
"os"
"path/filepath"
"text/template"

"github.com/Masterminds/sprig/v3"
"github.com/rs/zerolog/log"
)

func templateString(pattern string, args map[string]interface{}) (string, error) {
var output bytes.Buffer
t := template.Must(template.New(pattern).Funcs(sprig.TxtFuncMap()).Parse(pattern))
if err := t.Execute(&output, args); err != nil {
return "", err
}

return output.String(), nil
}

func templateFile(templateFile string, destinationFile string, args map[string]interface{}) error {
t := template.Must(
template.New(filepath.Base(templateFile)).Funcs(sprig.TxtFuncMap()).ParseFiles(templateFile),
)

f, err := os.Create(destinationFile)
if err != nil {
log.Error().Err(err).Str("file", templateFile).Msg("Failed to create")
return err
}
defer f.Close()

// Render templates using variables
if err := t.Execute(f, args); err != nil {
log.Error().Err(err).Str("file", templateFile).Msg("Failed to template")
return err
}

return nil
}

0 comments on commit b9239e6

Please sign in to comment.