-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
153 lines (129 loc) · 3.17 KB
/
utils.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
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
"net"
"os"
"path/filepath"
"reflect"
"strings"
"github.com/BurntSushi/toml"
"github.com/samber/lo"
)
func ptr[T any](v T) *T {
return &v
}
func loadTemplates() {
// Load deployment templates
deploymentTemplates = make(map[string]TemplateConfig)
matches, err := filepath.Glob("./templates/*.toml")
if err != nil {
log.Fatal(err)
}
for _, template := range matches {
var config TemplateConfig
_, err := toml.DecodeFile(template, &config)
if err != nil {
log.Fatal(err)
}
configKey := strings.TrimSuffix(filepath.Base(template), ".toml")
deploymentTemplates[configKey] = config
}
}
func findTemplateByDependencies(deps map[string]string) (string, error) {
for key, value := range deploymentTemplates {
for _, dep := range value.MatchDependencies {
if _, ok := deps[dep]; ok {
return key, nil
}
}
}
return "", errors.New("No template found")
}
func loadPackageJSON(srcPath string) (map[string]interface{}, error) {
pkgB, err := os.ReadFile(filepath.Join(srcPath, "package.json"))
if err != nil {
return nil, err
}
var pkgJson map[string]interface{}
err = json.Unmarshal(pkgB, &pkgJson)
if err != nil {
return nil, err
}
return pkgJson, nil
}
func parseDependencies(pkgJson map[string]interface{}) (map[string]string, error) {
deps := make(map[string]string)
switch v := pkgJson["dependencies"].(type) {
case map[string]interface{}:
for key, value := range v {
switch v2 := value.(type) {
case string:
deps[key] = v2
break
default:
return nil, errors.New(fmt.Sprintf("Package.json -> Dependencies -> Value: Must be a string. Got: %s", reflect.TypeOf(v2)))
}
}
break
default:
return nil, errors.New(fmt.Sprintf("Package.json -> Dependencies: Must be a map[string]string. Got: %s", reflect.TypeOf(v)))
}
return deps, nil
}
func getFreePort() (port int, err error) {
var a *net.TCPAddr
if a, err = net.ResolveTCPAddr("tcp", "localhost:0"); err == nil {
var l *net.TCPListener
if l, err = net.ListenTCP("tcp", a); err == nil {
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}
}
return
}
func createDirIfNotExists(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
return os.Mkdir(path, 755)
}
return nil
}
// Trying to not copy structs here
// TODO: check if we break references here
func getAllDeployments() []*Deployment {
return lo.FlatMap(apps, func(app *App, index int) []*Deployment {
return apps[index].Deployments
})
}
func getDeploymentByDomain(domain string) *Deployment {
deployment, found := lo.Find(getAllDeployments(), func(deployment *Deployment) bool {
return deployment.GetDomain() == domain
})
if !found {
return nil
}
return deployment
}
func getAppById(id string) *App {
app, found := lo.Find(apps, func(app *App) bool {
return app.Id == id
})
if !found {
return nil
}
return app
}
func getDeploymentById(id string) *Deployment {
deployment, found := lo.Find(getAllDeployments(), func(deployment *Deployment) bool {
return deployment.Id == id
})
if !found {
return nil
}
return deployment
}
func makeId() string {
return lo.RandomString(12, lo.LettersCharset)
}