forked from jetify-com/devbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
71 lines (58 loc) · 1.53 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
// Copyright 2023 Jetpack Technologies Inc and contributors. All rights reserved.
// Use of this source code is governed by the license in the LICENSE file.
package services
import (
"fmt"
"os"
"path/filepath"
"github.com/f1bonacc1/process-compose/src/types"
"github.com/pkg/errors"
"go.jetpack.io/devbox/internal/cuecfg"
)
func FromUserProcessCompose(projectDir string, userProcessCompose string) Services {
processComposeYaml := lookupProcessCompose(projectDir, userProcessCompose)
if processComposeYaml == "" {
return nil
}
userSvcs, err := FromProcessCompose(processComposeYaml)
if err != nil {
fmt.Fprintf(os.Stderr, "error reading process-compose.yaml: %s, skipping", err)
return nil
}
return userSvcs
}
func FromProcessCompose(path string) (Services, error) {
processCompose := &types.Project{}
services := Services{}
err := errors.WithStack(cuecfg.ParseFile(path, processCompose))
if err != nil {
return nil, err
}
for name := range processCompose.Processes {
svc := Service{
Name: name,
ProcessComposePath: path,
}
services[name] = svc
}
return services, nil
}
func lookupProcessCompose(projectDir, path string) string {
if path == "" {
path = projectDir
}
if !filepath.IsAbs(path) {
path = filepath.Join(projectDir, path)
}
pathsToCheck := []string{
path,
filepath.Join(path, "process-compose.yaml"),
filepath.Join(path, "process-compose.yml"),
}
for _, p := range pathsToCheck {
if fi, err := os.Stat(p); err == nil && !fi.IsDir() {
return p
}
}
return ""
}