-
Notifications
You must be signed in to change notification settings - Fork 4
/
go_dockerfile.go
58 lines (47 loc) · 1.22 KB
/
go_dockerfile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package dockerfile
import (
_ "embed"
"fmt"
"os"
"os/exec"
"regexp"
"github.com/FelipeMCassiano/gorvus/internal/builders"
"github.com/jedib0t/go-pretty/v6/text"
)
func GoDockerFileBuilder(input DockerfileData) error {
if len(input.ProjectName) == 0 {
pN, err := setProjectName()
if err != nil {
os.Exit(1)
}
input.ProjectName = pN
}
if len(input.EntryFile) >= 1 {
fmt.Println(text.FgYellow.Sprintf("> This language doens't needs to specify the Entry File"))
os.Exit(1)
}
cmd := exec.Command("go", "version")
goVersionOutput, err := cmd.Output()
if err != nil {
return err
}
versionPattern := `go version go(\d+\.\d+\.\d+)`
versionRegex := regexp.MustCompile(versionPattern)
matches := versionRegex.FindStringSubmatch(string(goVersionOutput))
if len(matches) < 2 {
return fmt.Errorf("failed to extract Go version number")
}
goVersion := matches[1]
datafile, err := templatesContent.ReadFile("templates/go_dockerfile.tmpl")
if err != nil {
return err
}
input.Version = goVersion
file, err := os.Create("Dockerfile")
if err != nil {
return fmt.Errorf("Error creating Dockerfile: %s", err.Error())
}
defer file.Close()
builders.ApplyTemplate(file, string(datafile), input)
return nil
}