-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
232 lines (193 loc) · 4.96 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package utils
import (
"bytes"
"fmt"
"go/build"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"text/template"
"unicode"
tea "github.com/charmbracelet/bubbletea"
)
func MatchAllStringByRegex(regex, str string) ([]*string, error) {
r, err := regexp.Compile(regex)
if err != nil {
return nil, err
}
result := r.FindAllSubmatch([]byte(str), -1)
if len(result) < 1 {
return nil, nil
}
var data []*string
for _, v := range result {
d := string(v[1])
data = append(data, &d)
}
return data, nil
}
func DownloadFile(url string, filepath string) error {
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
func DoesDirectoryExistAndIsNotEmpty(name string) bool {
if _, err := os.Stat(name); err == nil {
dirEntries, err := os.ReadDir(name)
if err != nil {
return false
}
if len(dirEntries) > 0 {
return true
}
}
return false
}
func CopyFile(sourcePath, destinationPath string) error {
// Open the source file
sourceFile, err := os.Open(sourcePath)
if err != nil {
return err
}
defer sourceFile.Close()
// Create the destination file
destinationFile, err := os.Create(destinationPath)
if err != nil {
return err
}
defer destinationFile.Close()
// Copy the contents of the source file to the destination file
_, err = io.Copy(destinationFile, sourceFile)
if err != nil {
return err
}
// Optionally, you can also flush the destination file to ensure all data is written
err = destinationFile.Sync()
if err != nil {
return err
}
return nil
}
// ExecuteCmd provides a shorthand way to run a shell command
func ExecuteCmd(name string, args []string, dir string) error {
command := exec.Command(name, args...)
command.Dir = dir
var out bytes.Buffer
command.Stdout = &out
if err := command.Run(); err != nil {
return err
}
return nil
}
func DeferTeaPanicHandler(p *tea.Program) {
if r := recover(); r != nil {
fmt.Println("The program encountered an unexpected issue and had to exit. The error was:", r)
fmt.Println("If you continue to experience this issue, please post a message on our GitHub page.")
fmt.Println()
if releaseErr := p.ReleaseTerminal(); releaseErr != nil {
fmt.Printf("Problem releasing terminal: %v", releaseErr)
}
}
}
func DoesDirectoryOrFileExist(path string) bool {
if _, err := os.Stat(path); err == nil {
return true
}
return false
}
func GetRefiberTemplateDirPath(currentWorkingDir *string) (*string, error) {
// check is current dir is a refiber project by checking go.mod file
goModFilePath := filepath.Join(*currentWorkingDir, "go.mod")
goModFileContent, err := os.ReadFile(goModFilePath)
if err != nil {
return nil, fmt.Errorf("the current folder path is not inside the Refiber project")
}
var refiberVersion string
pattern := `github\.com/refiber/framework\s(v.+)`
match := regexp.MustCompile(pattern).FindSubmatch(goModFileContent)
if len(match) > 1 {
refiberVersion = string(match[1])
} else {
return nil, fmt.Errorf("the current folder path is not inside the Refiber project")
}
templatePathInVendor := filepath.Join(*currentWorkingDir, "vendor", "github.com", "refiber", "framework", "templates")
if DoesDirectoryOrFileExist(templatePathInVendor) {
return &templatePathInVendor, nil
}
gopath := os.Getenv("GOPATH")
if gopath == "" {
gopath = build.Default.GOPATH
}
templatePathInGoPkg := filepath.Join(gopath, "pkg", "mod", "github.com", "refiber", fmt.Sprintf("framework@%s", refiberVersion), "templates")
if DoesDirectoryOrFileExist(templatePathInGoPkg) {
return &templatePathInGoPkg, nil
}
return nil, fmt.Errorf("template folder not found. Make sure you are using the newest version of Refiber")
}
func ExecuteTemplate(t *template.Template, data interface{}) ([]byte, error) {
var contentBuf bytes.Buffer
if err := t.Execute(&contentBuf, data); err != nil {
return nil, err
}
return contentBuf.Bytes(), nil
}
func WriteFile(filename, path string, content []byte) error {
file, err := os.Create(filepath.Join(path, filename))
if err != nil {
return err
}
defer file.Close()
_, err = file.Write(content)
if err != nil {
return err
}
return nil
}
func ListFolders(dirPath string) (*[]*string, error) {
if dirPath == "" {
return nil, fmt.Errorf("unable to list folder. the provided path is invalid")
}
var folders []*string
files, err := os.ReadDir(dirPath)
if err != nil {
return nil, err
}
for _, file := range files {
if file.IsDir() {
f := file.Name()
folders = append(folders, &f)
}
}
return &folders, nil
}
func GetLowercaseFirstChar(s string) string {
if len(s) == 0 {
return s
}
r := []rune(s)
r[0] = unicode.ToLower(r[0])
return string(r)
}
func GetLastPathName(path string) string {
parts := strings.Split(path, "/")
return parts[len(parts)-1]
}