-
-
Notifications
You must be signed in to change notification settings - Fork 196
/
export.go
76 lines (67 loc) · 1.63 KB
/
export.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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
)
func exportUpstart(cfg *config, path string) error {
for i, proc := range procs {
f, err := os.Create(filepath.Join(path, "app-"+proc.name+".conf"))
if err != nil {
return err
}
fmt.Fprintf(f, "start on starting app-%s\n", proc.name)
fmt.Fprintf(f, "stop on stopping app-%s\n", proc.name)
fmt.Fprintf(f, "respawn\n")
fmt.Fprintf(f, "\n")
env := map[string]string{}
procfile, err := filepath.Abs(cfg.Procfile)
if err != nil {
return err
}
b, err := os.ReadFile(filepath.Join(filepath.Dir(procfile), ".env"))
if err == nil {
for _, line := range strings.Split(string(b), "\n") {
token := strings.SplitN(line, "=", 2)
if len(token) != 2 {
continue
}
if strings.HasPrefix(token[0], "export ") {
token[0] = token[0][7:]
}
token[0] = strings.TrimSpace(token[0])
token[1] = strings.TrimSpace(token[1])
env[token[0]] = token[1]
}
}
fmt.Fprintf(f, "env PORT=%d\n", cfg.BasePort+uint(i))
for k, v := range env {
fmt.Fprintf(f, "env %s='%s'\n", k, strings.Replace(v, "'", "\\'", -1))
}
fmt.Fprintf(f, "\n")
fmt.Fprintf(f, "setuid app\n")
fmt.Fprintf(f, "\n")
fmt.Fprintf(f, "chdir %s\n", filepath.ToSlash(filepath.Dir(procfile)))
fmt.Fprintf(f, "\n")
fmt.Fprintf(f, "exec %s\n", proc.cmdline)
f.Close()
}
return nil
}
// command: export.
func export(cfg *config, format, path string) error {
err := readProcfile(cfg)
if err != nil {
return err
}
err = os.MkdirAll(path, 0755)
if err != nil {
return err
}
switch format {
case "upstart":
return exportUpstart(cfg, path)
}
return nil
}