-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.go
49 lines (44 loc) · 1.75 KB
/
util.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
package checks
import (
"context"
"strings"
"github.com/samber/lo"
"projectforge.dev/projectforge/app/doctor"
"projectforge.dev/projectforge/app/lib/filesystem"
"projectforge.dev/projectforge/app/lib/telemetry"
"projectforge.dev/projectforge/app/project"
"projectforge.dev/projectforge/app/util"
)
func simpleOut(path string, cmd string, args []string, outChecks ...func(ctx context.Context, r *doctor.Result, out string) *doctor.Result) doctor.CheckFn {
return func(ctx context.Context, r *doctor.Result, logger util.Logger) *doctor.Result {
fullCmd := strings.Join(append([]string{cmd}, args...), " ")
exitCode, out, err := telemetry.RunProcessSimple(ctx, fullCmd, path, logger)
if err != nil {
return r.WithError(doctor.NewError("missing", "[%s] is not present on your computer", cmd))
}
if exitCode != 0 {
return r.WithError(doctor.NewError("exitcode", "[%s] returned [%d] as an exit code", fullCmd, exitCode))
}
lo.ForEach(outChecks, func(outCheck func(ctx context.Context, r *doctor.Result, out string) *doctor.Result, _ int) {
r = outCheck(ctx, r, out)
})
return r
}
}
func loadRootProject(r *doctor.Result) (*project.Project, filesystem.FileLoader, *doctor.Result) {
const dir = "."
fs, _ := filesystem.NewFileSystem(dir, false, "")
if !fs.Exists(project.ConfigDir) {
return nil, nil, r.WithError(doctor.NewError("missing", "no project found in [%s]", dir))
}
b, err := fs.ReadFile(project.ConfigDir + "/project.json")
if err != nil {
return nil, nil, r.WithError(doctor.NewError("missing", "unable to read project from [%s]", dir))
}
p := &project.Project{}
err = util.FromJSON(b, p)
if err != nil {
return nil, nil, r.WithError(doctor.NewError("invalid", "unable to parse project JSON from [%s]", dir))
}
return p, fs, r
}