-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstartAndStopCommand.go
117 lines (93 loc) · 2.81 KB
/
startAndStopCommand.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"github.com/mkideal/cli"
)
type LocalServerCommand string
const (
LocalServerStart LocalServerCommand = "start"
LocalServerStop LocalServerCommand = "stop"
)
func (command LocalServerCommand) String() string {
switch command {
case LocalServerStart:
return "local:server:start --daemon"
case LocalServerStop:
return "local:server:stop"
default:
return ""
}
}
type startT struct {
cli.Helper
}
var startCommand = &cli.Command{
Name: "start",
Desc: "start all apps linked to your symfony binary",
Argv: func() interface{} { return new(startT) },
Fn: func(ctx *cli.Context) error {
printHeader()
startOrStopCommand(LocalServerStart, "started ✔")
return nil
},
}
type stopT struct {
cli.Helper
}
var stopCommand = &cli.Command{
Name: "stop",
Desc: "stop all apps linked to your symfony binary",
Argv: func() interface{} { return new(stopT) },
Fn: func(ctx *cli.Context) error {
printHeader()
startOrStopCommand(LocalServerStop, "stopped 🛑")
runCommand("symfony proxy:stop")
return nil
},
}
func startOrStopCommand(serverCommand LocalServerCommand, message string) {
command := serverCommand.String()
if command == "" {
return
}
if false == symfonyProxyRunning() {
runCommand("symfony proxy:start")
fmt.Print("Started Symfony proxy server 👮\n")
}
symfonyProxyConfigPath := fmt.Sprintf("%s/proxy.json", getSymfonyCliConfigPath())
if _, err := os.Stat(symfonyProxyConfigPath); os.IsNotExist(err) {
fmt.Printf("Symfony proxy configuration does not exists 🙊. Maybe you should run %s before %s ? 🧐\n", yellow("init"), yellow("start"))
return
}
file, _ := ioutil.ReadFile(symfonyProxyConfigPath)
symfonyJSONData := SymfonyJSONProxy{}
json.Unmarshal(file, &symfonyJSONData)
for domain, path := range symfonyJSONData.Domains {
forcedPort := symfonyJSONData.Ports[domain]
formattedCommand := fmt.Sprintf("symfony %s --dir=%s", command, path)
serverRunning := symfonyServerRunning(path)
if (serverCommand == LocalServerStart && !serverRunning) || (serverCommand == LocalServerStop && serverRunning) {
if serverCommand == LocalServerStart && forcedPort != 0 {
formattedCommand = fmt.Sprintf("symfony %s --port=%d --dir=%s", command, forcedPort, path)
}
runCommand(formattedCommand)
}
fmt.Printf("%s %s\n", message, yellow(fmt.Sprintf("%s.%s", domain, symfonyJSONData.Tld)))
}
return
}
func symfonyProxyRunning() bool {
response, err := http.Get("http://127.0.0.1:7080/")
if nil != err || 200 != response.StatusCode {
return false
}
return true
}
func symfonyServerRunning(path string) bool {
grepNotRunning := " grep -A 1 'Local Web Server' | grep 'Not Running'"
return len(outputCommand(fmt.Sprintf("symfony local:server:status --dir=%s | %s", path, grepNotRunning))) == 0
}