Skip to content

Commit 64d08c8

Browse files
committed
fix: embedding templates into binary
This commit fixes the problem when executing the binary from different locations. The templates are now embedded.
1 parent 026b85b commit 64d08c8

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

internal/program.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package internal
22

33
import (
4+
_ "embed"
45
"errors"
56
"log"
67
"os"
@@ -11,13 +12,17 @@ import (
1112
)
1213

1314
const (
14-
gitignoreTmplPath = "templates/.gitignore.tmpl"
15-
mainTmplPath = "templates/main.cpp.tmpl"
16-
srcPath = "src"
17-
binPath = "bin"
18-
includePath = "include"
15+
srcPath = "src"
16+
binPath = "bin"
17+
includePath = "include"
1918
)
2019

20+
//go:embed templates/.gitignore.tmpl
21+
var gitignoreTmpl string
22+
23+
//go:embed templates/main.cpp.tmpl
24+
var mainTmpl string
25+
2126
type Project struct {
2227
ProjectName string
2328
AbsolutePath string
@@ -67,14 +72,14 @@ func (p *Project) CreateProjectStructure() error {
6772
}
6873

6974
// Create .gitignore file
70-
if err := p.CreateFileFromTemplate(gitignoreTmplPath, projectPath, ".gitignore"); err != nil {
75+
if err := p.CreateFileFromTemplate(gitignoreTmpl, projectPath, ".gitignore"); err != nil {
7176
log.Printf("Error creating .gitignore file: %v\n", err)
7277
return err
7378
}
7479

7580
// Create main.cpp file
7681
mainPath := filepath.Join(projectPath, srcPath)
77-
if err := p.CreateFileFromTemplate(mainTmplPath, mainPath, "main.cpp"); err != nil {
82+
if err := p.CreateFileFromTemplate(mainTmpl, mainPath, "main.cpp"); err != nil {
7883
log.Printf("Error creating main.cpp file: %v\n", err)
7984
return err
8085
}
@@ -93,9 +98,9 @@ func (p *Project) CreateProjectStructure() error {
9398
return nil
9499
}
95100

96-
func (p *Project) CreateFileFromTemplate(tmplPath string, destinationPath string, filename string) error {
101+
func (p *Project) CreateFileFromTemplate(tmplContent string, destinationPath string, filename string) error {
97102
// read template file
98-
tmpl, err := template.ParseFiles(tmplPath)
103+
tmpl, err := template.New(filename).Parse(tmplContent)
99104
if err != nil {
100105
log.Printf("Error parsing template file: %v\n", err)
101106
return err

internal/templates/.clang-format

Whitespace-only changes.

0 commit comments

Comments
 (0)