Skip to content
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/gruntwork-io/terratest v0.46.11
github.com/hashicorp/terraform-json v0.13.0
github.com/stretchr/testify v1.8.4
golang.org/x/oauth2 v0.8.0
)

require (
Expand Down Expand Up @@ -88,7 +89,6 @@ require (
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions pkg/gh/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ func FetchReleases(repoURL string, onlyLatest bool) ([]*github.RepositoryRelease
return []*github.RepositoryRelease{release}, nil
}

releases, _, err := client.Repositories.ListReleases(ctx, owner, repo, &github.ListOptions{})
if err != nil {
return nil, fmt.Errorf("error fetching releases: %w", err)
releases, _, releaseErr := client.Repositories.ListReleases(ctx, owner, repo, &github.ListOptions{})
if releaseErr != nil {
return nil, fmt.Errorf("error fetching releases: %w", releaseErr)
}

return releases, nil
}

// GetOwnerAndRepoFromURL extracts the owner and repo name from a GitHub URL
func GetOwnerAndRepoFromURL(repoURL string) (string, string, error) {
func GetOwnerAndRepoFromURL(repoURL string) (owner, repo string, err error) {
u, err := url.Parse(repoURL)
if err != nil {
return "", "", fmt.Errorf("error parsing URL: %w", err)
Expand Down
42 changes: 42 additions & 0 deletions pkg/tmplutils/processor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package tmplutils

import (
"fmt"
"html/template"
"os"
"path/filepath"
)

// ProcessTemplFile processes a template file with the provided data and writes the output to the destination file.
// It returns an error if any of the operations fail.
// The function map is used to define custom functions that can be called from the template.
func ProcessTemplFile(templatePath, destPath string, funcMap template.FuncMap, data interface{}) error {
content, err := os.ReadFile(templatePath)
if err != nil {
return fmt.Errorf("failed to read template file: %w", err)
}

if err := os.MkdirAll(filepath.Dir(destPath), os.ModePerm); err != nil {
return fmt.Errorf("failed to create parent directory for %s: %w", destPath, err)
}

tmpl, tmplErr := template.New(filepath.Base(templatePath)).Funcs(funcMap).Parse(string(content))
if tmplErr != nil {
return fmt.Errorf("failed to parse template: %w", tmplErr)
}

// Write to destination with processed content
file, fileErr := os.Create(destPath)
if fileErr != nil {
return fmt.Errorf("failed to create destination file: %w", fileErr)
}

defer file.Close()

// Execute the template with provided data
if err := tmpl.Execute(file, data); err != nil {
return fmt.Errorf("failed to execute template with provided data: %w", err)
}

return nil
}
100 changes: 100 additions & 0 deletions pkg/tmplutils/processor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package tmplutils

import (
"fmt"
"os"
"path/filepath"
"testing"
"text/template"

"github.com/stretchr/testify/assert"
)

func TestProcessTemplFile(t *testing.T) {
// Create a temporary directory for testing
tempDir, err := os.MkdirTemp("", "test")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
defer os.RemoveAll(tempDir)

// Define test cases
testCases := []struct {
name string
templatePath string
destPath string
funcMap template.FuncMap
data interface{}
expectError bool
}{
{
name: "Template file does not exist",
templatePath: "nonexistent",
destPath: filepath.Join(tempDir, "dest"),
expectError: true,
},
{
name: "Cannot create destination directory",
templatePath: filepath.Join(tempDir, "template"),
destPath: "/invalid/path/dest",
expectError: true,
},
{
name: "Cannot parse template",
templatePath: filepath.Join(tempDir, "invalid_template"),
destPath: filepath.Join(tempDir, "dest"),
expectError: true,
},
{
name: "Cannot create destination file",
templatePath: filepath.Join(tempDir, "template"),
destPath: "/invalid/path/dest",
expectError: true,
},
{
name: "Template execution fails",
templatePath: filepath.Join(tempDir, "template"),
destPath: filepath.Join(tempDir, "dest"),
funcMap: template.FuncMap{"fail": func() (string, error) { return "", fmt.Errorf("fail") }},
data: map[string]interface{}{"Value": "fail"},
expectError: true,
},
{
name: "Everything is correct",
templatePath: filepath.Join(tempDir, "template"),
destPath: filepath.Join(tempDir, "dest"),
expectError: false,
},
}

// Run test cases
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Create template file if it should exist
if tc.templatePath != "nonexistent" {
var content []byte
if tc.templatePath == filepath.Join(tempDir, "invalid_template") {
content = []byte("{{")
} else if tc.name == "Template execution fails" {
content = []byte("{{fail}}") // Call the "fail" function in the template
} else {
content = []byte("{{.Value}}")
}
err := os.WriteFile(tc.templatePath, content, 0644)
if err != nil {
t.Fatalf("Failed to create template file: %v", err)
}
}

// Call ProcessTemplFile
err := ProcessTemplFile(tc.templatePath, tc.destPath, tc.funcMap, tc.data)

// Check if error is expected
if tc.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/utils/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// IsAGitRepository checks if the given directory or any of its parent directories up to `levels` is a git repository.
// It returns the git root directory, the subdirectory passed relative to the git root, and any error encountered.
func IsAGitRepository(repoRoot string, levels int) (gitRoot string, subDir string, err error) {
func IsAGitRepository(repoRoot string, levels int) (gitRoot, subDir string, err error) {
if repoRoot == "" {
return "", "", fmt.Errorf("directory path cannot be empty")
}
Expand Down