generated from brevdev/seed
/
file.go
396 lines (354 loc) · 9.24 KB
/
file.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package store
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
"github.com/brevdev/brev-cli/pkg/files"
"github.com/spf13/afero"
user "github.com/tweekmonster/luser"
"golang.org/x/text/encoding/charmap"
)
type FileStore struct {
BasicStore
fs afero.Fs
User *user.User
}
func (b *BasicStore) WithFileSystem(fs afero.Fs) *FileStore {
return &FileStore{*b, fs, nil}
}
func (f *FileStore) WithUserID(userID string) (*FileStore, error) {
var userToConfigure *user.User
var err error
userToConfigure, err = user.Lookup(userID)
if err != nil {
_, ok := err.(*user.UnknownUserError)
if !ok {
userToConfigure, err = user.LookupId(userID)
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
} else {
return nil, breverrors.WrapAndTrace(err)
}
}
f.User = userToConfigure
return f, nil
}
func (f FileStore) GetOrCreateFile(path string) (afero.File, error) {
fileExists, err := afero.Exists(f.fs, path)
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
var file afero.File
if fileExists {
file, err = f.fs.OpenFile(path, os.O_RDWR, 0o644)
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
} else {
if err = f.fs.MkdirAll(filepath.Dir(path), 0o775); err != nil {
return nil, breverrors.WrapAndTrace(err)
}
file, err = f.fs.Create(path)
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
}
return file, nil
}
func (f FileStore) FileExists(filepath string) (bool, error) {
fileExists, err := afero.Exists(f.fs, filepath)
if err != nil {
return false, breverrors.WrapAndTrace(err)
}
return fileExists, nil
}
func (f FileStore) GetDotGitConfigFile(path string) (string, error) {
dotGitConfigFile := filepath.Join(path, ".git", "config")
dotGitConfigExists, err := afero.Exists(f.fs, dotGitConfigFile)
if err != nil {
return "", breverrors.WrapAndTrace(err)
}
var file afero.File
if dotGitConfigExists {
file, err = f.fs.OpenFile(dotGitConfigFile, os.O_RDWR, 0o644)
if err != nil {
return "", breverrors.WrapAndTrace(err)
}
} else {
return "", breverrors.WrapAndTrace(errors.New("couldn't verify if " + dotGitConfigFile + " is an active git directory"))
}
buf := new(strings.Builder)
_, err = io.Copy(buf, file)
if err != nil {
return "", breverrors.WrapAndTrace(err)
}
return buf.String(), nil
}
type Dependencies struct {
Rust string
Java string
Node string
TS string
Go string
Solana string
}
func (f FileStore) GetDependenciesForImport(path string) (*Dependencies, error) {
deps := &Dependencies{
Rust: "",
Java: "",
Node: "",
TS: "",
Go: "",
Solana: "",
}
// Check Golang
filePath := filepath.Join(path, "go.mod")
gocmd := exec.Command("cat", filePath) // #nosec G204
in, err := gocmd.Output()
if err != nil {
return nil, breverrors.WrapAndTrace(err, "error reading go.mod")
} else {
d := charmap.CodePage850.NewDecoder()
out, err := d.Bytes(in)
if err != nil {
return nil, breverrors.WrapAndTrace(err, "error reading go.mod")
} else if len(string(out)) > 0 {
for _, v := range strings.Split(string(out), "\n") {
if strings.HasPrefix(v, "go ") {
versionNum := strings.Split(v, "go ")
deps.Go = versionNum[1]
}
}
}
}
return deps, nil
}
type WorkspaceMeta struct {
WorkspaceID string `json:"workspaceId"`
WorkspaceGroupID string `json:"workspaceGroupId"`
UserID string `json:"userId"`
OrganizationID string `json:"organizationId"`
}
// GetCurrentWorkspaceID will return an empty string when
// not in a brev workspace, and otherwise return its id
func (f FileStore) GetCurrentWorkspaceID() (string, error) {
meta, err := f.GetCurrentWorkspaceMeta()
if err != nil {
return "", nil
}
return meta.WorkspaceID, nil
}
func (f FileStore) IsWorkspace() (bool, error) {
id, err := f.GetCurrentWorkspaceID()
if err != nil {
return false, breverrors.WrapAndTrace(err)
}
return id != "", nil
}
func (f FileStore) GetCurrentWorkspaceGroupID() (string, error) {
meta, err := f.GetCurrentWorkspaceMeta()
if err != nil {
return "", nil
}
return meta.WorkspaceGroupID, nil
}
func (f FileStore) GetCurrentWorkspaceMeta() (*WorkspaceMeta, error) {
file, err := f.fs.Open("/etc/meta/workspace.json")
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
fileB, err := ioutil.ReadAll(file)
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
var wm WorkspaceMeta
err = json.Unmarshal(fileB, &wm)
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
err = file.Close()
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
if wm.WorkspaceID != "" {
breverrors.GetDefaultErrorReporter().AddTag("workspaceId", wm.WorkspaceID)
breverrors.GetDefaultErrorReporter().AddTag("organizationId", wm.OrganizationID)
breverrors.GetDefaultErrorReporter().AddTag("workspaceGroupId", wm.WorkspaceGroupID)
}
return &wm, nil
}
// // Check Rust Version
// filePath := filepath.Join(path, "Cargo.lock")
// doesCargoLockExist, err := afero.Exists(f.fs, filePath)
// if err != nil {
// return nil, breverrors.WrapAndTrace(err)
// }
// filePath = filepath.Join(path, "Cargo.toml")
// doesCargoTomlExist, err := afero.Exists(f.fs, filePath)
// if err != nil {
// return nil, breverrors.WrapAndTrace(err)
// }
// if doesCargoLockExist || doesCargoTomlExist {
// // should be version number
// deps.Rust = "true"
// }
// // Check Node
// // look for package.json or package_lock.json
// filePath = filepath.Join(path, "package_lock.json")
// doesPkgLockExist, err := afero.Exists(f.fs, filePath)
// if err != nil {
// return nil, breverrors.WrapAndTrace(err)
// }
// filePath = filepath.Join(path, "package.json")
// doesPkgExist, err := afero.Exists(f.fs, filePath)
// if err != nil {
// return nil, breverrors.WrapAndTrace(err)
// }
// if doesPkgLockExist || doesPkgExist {
// // should be version number
// deps.Node = "true"
// }
// // Check Typescript
// // look for package.json or package_lock.json
// filePath = filepath.Join(path, "tsconfig.json")
// doesTSConfigExist, err := afero.Exists(f.fs, filePath)
// if err != nil {
// return nil, breverrors.WrapAndTrace(err)
// }
// if doesTSConfigExist {
// // should be version number
// deps.TS = "true"
// }
// // Check Java Version
// // idea: look for JAVA_HOME or JRE_HOME. Right now uses Java CLI
// cmdddd := exec.Command("java", "--version") // #nosec G204
// in, err = cmdddd.Output()
// if err != nil {
// // return nil, breverrors.WrapAndTrace(err)
// } else {
// d := charmap.CodePage850.NewDecoder()
// out, err := d.Bytes(in)
// if err != nil {
// // return nil, breverrors.WrapAndTrace(err)
// } else {
// if len(string(out)) > 0 {
// // fmt.Println(string(out))
// deps.Java = "true"
// }
// }
// }
func (f FileStore) WriteString(path, data string) error {
file, err := f.GetOrCreateFile(path)
if err != nil {
return breverrors.WrapAndTrace(err)
}
_, err = file.WriteString(data)
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}
// CopyBin copies the running executeable to a target, creating directories as needed
func (f FileStore) CopyBin(targetBin string) error {
err := f.fs.MkdirAll(filepath.Dir(targetBin), 0o755)
if err != nil {
return breverrors.WrapAndTrace(err)
}
exe, err := os.Executable()
if err != nil {
return breverrors.WrapAndTrace(err)
}
pathTmpBin := targetBin + ".tmp"
fileTmpBin, err := f.fs.Create(pathTmpBin)
if err != nil {
return breverrors.WrapAndTrace(err)
}
self, err := f.fs.Open(exe)
if err != nil {
return breverrors.WrapAndTrace(err)
}
_, err = io.Copy(fileTmpBin, self)
_ = self.Close()
if err != nil {
_ = fileTmpBin.Close()
return breverrors.WrapAndTrace(err)
}
err = fileTmpBin.Close()
if err != nil {
return breverrors.WrapAndTrace(err)
}
err = f.fs.Chmod(pathTmpBin, 0o755)
if err != nil {
return breverrors.WrapAndTrace(err)
}
err = f.fs.Rename(pathTmpBin, targetBin)
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}
func (f FileStore) UserHomeDir() (string, error) {
if f.User != nil {
return f.User.HomeDir, nil
} else {
home, err := os.UserHomeDir()
if err != nil {
return "", breverrors.WrapAndTrace(err)
}
return home, nil
}
}
func (f FileStore) GetBrevHomePath() (string, error) {
home, err := f.UserHomeDir()
if err != nil {
return "", breverrors.WrapAndTrace(err)
}
brevHome, err := files.GetBrevHome(home)
if err != nil {
return "", breverrors.WrapAndTrace(err)
}
return brevHome, nil
}
func (f FileStore) BuildBrevHome() error {
home, err := f.UserHomeDir()
if err != nil {
return breverrors.WrapAndTrace(err)
}
err = files.BuildBrevHome(f.fs, home)
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}
func (f FileStore) GetOSUser() string {
if f.User != nil {
return f.User.Uid
} else {
return fmt.Sprint(os.Getuid())
}
}
func (f FileStore) GetServerSockFile() string {
return "/var/run/brev.sock"
}
func (f FileStore) Remove(target string) error {
err := f.fs.Remove(target)
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}
func (f FileStore) GetFileAsString(path string) (string, error) {
res, err := files.ReadString(f.fs, path)
if err != nil {
return "", breverrors.WrapAndTrace(err)
}
return res, nil
}