-
-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathexport.go
More file actions
71 lines (61 loc) · 1.49 KB
/
Copy pathexport.go
File metadata and controls
71 lines (61 loc) · 1.49 KB
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
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/joho/godotenv"
)
func exportUpstart(cfg *config, path string) error {
procfile, err := filepath.Abs(cfg.Procfile)
if err != nil {
return err
}
// parse .env the same way `goreman start` does (godotenv), so exported
// values match the runtime environment.
env, err := godotenv.Read(filepath.Join(filepath.Dir(procfile), ".env"))
if err != nil {
env = map[string]string{}
}
for _, 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")
if proc.setPort {
fmt.Fprintf(f, "env PORT=%d\n", proc.port)
}
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 errors.New("unknown format: " + format)
}