-
Notifications
You must be signed in to change notification settings - Fork 13
/
config.go
175 lines (157 loc) · 4.12 KB
/
config.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package init
import (
"encoding/json"
"fmt"
"os"
"regexp"
"github.com/buildsafedev/bsf/pkg/hcl2nix"
"github.com/buildsafedev/bsf/pkg/langdetect"
)
var (
commonDevDeps = []string{"coreutils-full@9.5", "bash@5.2.15"}
commonRTDeps = []string{"cacert@3.95"}
)
func generatehcl2NixConf(pt langdetect.ProjectType, pd *langdetect.ProjectDetails, baseImgName string) (hcl2nix.Config, error) {
switch pt {
case langdetect.GoModule:
return genGoModuleConf(pd), nil
case langdetect.PythonPoetry:
return genPythonPoetryConf(), nil
case langdetect.RustCargo:
config, err := genRustCargoConf()
if err != nil {
return hcl2nix.Config{}, err
}
return config, nil
case langdetect.JsNpm:
config, err := genJsNpmConf()
if err != nil {
return hcl2nix.Config{}, err
}
return config, nil
case langdetect.BaseImage:
return generateEmptyConf(baseImgName), nil
default:
return hcl2nix.Config{
Packages: hcl2nix.Packages{},
}, fmt.Errorf("language is not supported")
}
}
func generateEmptyConf(imageName string) hcl2nix.Config {
return hcl2nix.Config{
Packages: hcl2nix.Packages{
Development: commonDevDeps,
Runtime: commonRTDeps,
},
OCIArtifact: []hcl2nix.OCIArtifact{
{
Artifact: "pkgs",
Name: imageName,
Cmd: []string{},
Entrypoint: []string{},
EnvVars: []string{},
ExposedPorts: []string{},
ImportConfigs: []string{},
},
},
}
}
func genRustCargoConf() (hcl2nix.Config, error) {
content, err := os.ReadFile("Cargo.toml")
if err != nil {
return hcl2nix.Config{}, fmt.Errorf("error reading file: %v", err)
}
packageNameRegex, err := regexp.Compile(`name = "(.*?)"`)
if err != nil {
return hcl2nix.Config{}, fmt.Errorf("error fetching project name: %v", err)
}
match := packageNameRegex.FindStringSubmatch(string(content))
var CrateName string
if len(match) >= 2 {
CrateName = match[1]
} else {
CrateName = "my-project"
}
rustDevDeps := append(commonDevDeps, "cargo@1.75.0")
return hcl2nix.Config{
Packages: hcl2nix.Packages{
Development: rustDevDeps,
Runtime: commonRTDeps,
},
RustApp: &hcl2nix.RustApp{
WorkspaceSrc: "./.",
CrateName: CrateName,
RustVersion: "1.75.0",
Release: true,
},
}, nil
}
func genPythonPoetryConf() hcl2nix.Config {
// TODO: maybe we should note down the path of the poetry.lock file and use it here.
poetryDevDeps := append(commonDevDeps, "python3@3.12.2", "poetry@1.8.2")
return hcl2nix.Config{
Packages: hcl2nix.Packages{
Development: poetryDevDeps,
Runtime: commonRTDeps,
},
PoetryApp: &hcl2nix.PoetryApp{
ProjectDir: "./.",
Src: "./.",
Pyproject: "./pyproject.toml",
Poetrylock: "./poetry.lock",
PreferWheels: false,
CheckGroups: []string{"dev"},
},
}
}
func genGoModuleConf(pd *langdetect.ProjectDetails) hcl2nix.Config {
if pd.Name == "" {
pd.Name = "expl"
}
var name, entrypoint string
if pd != nil {
name = pd.Name
entrypoint = pd.Entrypoint
if entrypoint == "" {
entrypoint = "./."
}
}
goDevDeps := append(commonDevDeps, "go@1.22.3", "gotools@0.18.0", "delve@1.22.1")
return hcl2nix.Config{
Packages: hcl2nix.Packages{
Development: goDevDeps,
// todo: maybe we should dynamically inject the latest version of such runtime packages(cacert)?
Runtime: commonRTDeps,
},
GoModule: &hcl2nix.GoModule{
Name: name,
SourcePath: entrypoint,
},
}
}
func genJsNpmConf() (hcl2nix.Config, error) {
data, err := os.ReadFile("package-lock.json")
if err != nil {
return hcl2nix.Config{}, fmt.Errorf("error reading file: %v", err)
}
var jsonData map[string]interface{}
err = json.Unmarshal(data, &jsonData)
if err != nil {
return hcl2nix.Config{}, fmt.Errorf("error parsing json data: %v", err)
}
name, ok := jsonData["name"].(string)
if !ok {
return hcl2nix.Config{}, fmt.Errorf("error fetching project name: %v", err)
}
nodeDevDeps := append(commonDevDeps, "nodejs@20.11.1")
return hcl2nix.Config{
Packages: hcl2nix.Packages{
Development: nodeDevDeps,
Runtime: commonRTDeps,
},
JsNpmApp: &hcl2nix.JsNpmApp{
PackageName: name,
PackageRoot: "./.",
},
}, nil
}