-
Notifications
You must be signed in to change notification settings - Fork 1
/
xdebug.go
306 lines (243 loc) · 8.25 KB
/
xdebug.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package command
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
"github.com/urfave/cli/v2"
)
type xdebug struct {
usage map[string]string
aliases map[string]string
description map[string]string
}
type xdebugProjectConfig interface {
GetXDebugFpmIniPath() string
GetXDebugCliIniPath() string
GetXDebugConfigLocaton() string
GetProjectMainContainer() string
SaveContainerNameToProjectConfig(string) error
SaveDockerCliXdebugIniFilePath(string) error
SaveDockerFpmXdebugIniFilePath(string) error
SaveXDebugConifgLocaton(string) error
GetCommandInactveStatus(string) bool
}
type xdebugArgsProjectConfig interface {
GetXDebugFpmIniPath() string
GetXDebugCliIniPath() string
GetXDebugConfigLocaton() string
GetProjectMainContainer() string
}
func getXdebugArgs(cfg xdebugArgsProjectConfig, command string, currentPath string) []string {
action := `s/^\;zend_extension/zend_extension/g`
if strings.Contains(command, "disable") {
action = `s/^zend_extension/\;zend_extension/g`
}
xdebugFileConfigPath := cfg.GetXDebugFpmIniPath()
if command == "xdebug:cli:enable" || command == "xdebug:cli:disable" {
xdebugFileConfigPath = cfg.GetXDebugCliIniPath()
}
args := []string{}
if cfg.GetXDebugConfigLocaton() == "local" {
args = []string{"sed", "-i", "-e", action, strings.TrimRight(currentPath, string(os.PathSeparator)) + string(os.PathSeparator) + strings.Trim(xdebugFileConfigPath, string(os.PathSeparator))}
} else {
args = []string{"docker", "exec", "-u", "root", cfg.GetProjectMainContainer(), "sed", "-i", "-e", action, xdebugFileConfigPath}
}
return args
}
func toggleXdebugArgs(cfg xdebugArgsProjectConfig, currentPath string, xdebugAction string) []string {
xdebugFileConfigPath := cfg.GetXDebugFpmIniPath()
if strings.Contains(xdebugAction, "cli") {
xdebugFileConfigPath = cfg.GetXDebugCliIniPath()
}
args := []string{}
if cfg.GetXDebugConfigLocaton() == "local" {
args = []string{"cat", strings.TrimRight(currentPath, string(os.PathSeparator)) + string(os.PathSeparator) + strings.Trim(xdebugFileConfigPath, string(os.PathSeparator))}
} else {
args = []string{"docker", "exec", "-u", "root", cfg.GetProjectMainContainer(), "cat", xdebugFileConfigPath}
}
return args
}
type xDebugCommandDialog interface {
SetMainContaner([]string) (int, string, error)
DockerCliXdebugIniFilePath(string) (string, error)
DockerFpmXdebugIniFilePath(string) (string, error)
XDebugConfigLocation() (int, string, error)
}
type xDebugOptions interface {
GetExecCommand() func(ExecOptions, *cli.App) error
GetInitFunction() func(bool) string
GetContainerList() ([]string, error)
CheckXdebugStatus(*cli.App, []string) (bool, error)
}
//XDebugCommand enable/disable xDebug
func XDebugCommand(xdebugAction string, cfg xdebugProjectConfig, d xDebugCommandDialog, options xDebugOptions) *cli.Command {
execCommand := options.GetExecCommand()
initf := options.GetInitFunction()
descripton := `Set relative path from project root by asking path in local scope. Set absolute path for docker scope. Use local scope for mounted configs.`
x := &xdebug{
usage: map[string]string{
"xdebug:fpm:enable": "Enable fpm xdebug",
"xdebug:fpm:disable": "Disable fpm xdebug",
"xdebug:fpm:toggle": "Toggle fpm xdebug",
"xdebug:cli:enable": "Enable cli xdebug",
"xdebug:cli:disable": "Disable cli xdebug",
"xdebug:cli:toggle": "Toggle cli xdebug",
},
aliases: map[string]string{
"xdebug:fpm:enable": "xe",
"xdebug:fpm:disable": "xd",
"xdebug:fpm:toggle": "x",
"xdebug:cli:enable": "xce",
"xdebug:cli:disable": "xcd",
"xdebug:cli:toggle": "xc",
},
description: map[string]string{
"xdebug:fpm:enable": descripton,
"xdebug:fpm:disable": descripton,
"xdebug:fpm:toggle": descripton,
"xdebug:cli:enable": descripton,
"xdebug:cli:disable": descripton,
"xdebug:cli:toggle": descripton,
},
}
return &cli.Command{
Name: xdebugAction,
Aliases: []string{x.aliases[xdebugAction]},
Usage: x.usage[xdebugAction],
Description: x.description[xdebugAction],
SkipFlagParsing: false,
Hidden: cfg.GetCommandInactveStatus("xdebug"),
Action: func(c *cli.Context) (err error) {
currentPath := initf(true)
var cl []string
if cl, err = options.GetContainerList(); err != nil {
return err
}
if err = defineProjectMainContainer(cfg, d, cl); err != nil {
return err
}
if err = defineCliXdebugIniFilePath(cfg, d, "/etc/php/7.0/cli/conf.d/xdebug.ini"); strings.Contains(xdebugAction, "cli") && err != nil {
return err
}
if err = defineFpmXdebugIniFilePath(cfg, d, "/etc/php/7.0/fpm/conf.d/xdebug.ini"); strings.Contains(xdebugAction, "fpm") && err != nil {
return err
}
if err = defineXdebugIniFileLocation(cfg, d); err != nil {
return err
}
if strings.Contains(xdebugAction, "toggle") {
args := toggleXdebugArgs(cfg, currentPath, xdebugAction)
if strings.Contains(xdebugAction, "cli") {
xdebugAction = "xdebug:cli:"
} else {
xdebugAction = "xdebug:fpm:"
}
var status bool
if status, err = options.CheckXdebugStatus(c.App, args); err != nil {
return err
}
if status == false {
xdebugAction = xdebugAction + "enable"
} else {
xdebugAction = xdebugAction + "disable"
}
}
args := getXdebugArgs(cfg, xdebugAction, currentPath)
eo := ExecOptions{
command: args[0],
args: args[1:],
tty: true,
detach: true,
}
if err = execCommand(eo, c.App); err != nil {
return err
}
if xdebugAction == "xdebug:fpm:enable" {
fmt.Printf("Fpm Xdebug enabled\n")
} else if xdebugAction == "xdebug:fpm:disable" {
fmt.Printf("Fpm Xdebug disabled \n")
} else if xdebugAction == "xdebug:cli:enable" {
fmt.Printf("Cli Xdebug enabled \n")
} else if xdebugAction == "xdebug:cli:disable" {
fmt.Printf("Cli Xdebug disabled \n")
}
return restartMainContainer(cfg, options, c.App)
},
}
}
// CheckXdebugStatus checks xdebug current state
func CheckXdebugStatus(app *cli.App, args []string) (bool, error) {
eo := ExecOptions{
command: args[0],
args: args[1:],
tty: true,
detach: true,
}
out, err := exec.Command(eo.GetCommand(), eo.GetArgs()...).Output()
if strings.Contains(string(out), ";zend_extension=xdebug") {
return false, err
}
return true, err
}
type defineCliXdebugIniFilePathProjectConfig interface {
SaveDockerCliXdebugIniFilePath(string) error
GetXDebugCliIniPath() string
}
type defineCliXdebugIniFilePathDialog interface {
DockerCliXdebugIniFilePath(string) (string, error)
}
func defineCliXdebugIniFilePath(cfg defineCliXdebugIniFilePathProjectConfig, d defineCliXdebugIniFilePathDialog, defaultPath string) (err error) {
if cfg.GetXDebugCliIniPath() == "" {
var path string
if path, err = d.DockerCliXdebugIniFilePath(defaultPath); err != nil {
return err
}
if path == "" {
return errors.New("Cli Xdebug ini file path is empty")
}
return cfg.SaveDockerCliXdebugIniFilePath(path)
}
return nil
}
type defineFpmXdebugIniFilePathProjectConfig interface {
SaveDockerFpmXdebugIniFilePath(string) error
GetXDebugFpmIniPath() string
}
type defineFpmXdebugIniFilePathDialog interface {
DockerFpmXdebugIniFilePath(string) (string, error)
}
func defineFpmXdebugIniFilePath(cfg defineFpmXdebugIniFilePathProjectConfig, d defineFpmXdebugIniFilePathDialog, defaultPath string) (err error) {
if cfg.GetXDebugFpmIniPath() == "" {
var path string
if path, err = d.DockerFpmXdebugIniFilePath(defaultPath); err != nil {
return err
}
if path == "" {
return errors.New("Fpm Xdebug ini file path is empty")
}
return cfg.SaveDockerFpmXdebugIniFilePath(path)
}
return nil
}
type defineXdebugIniFileLocationProjectConfig interface {
SaveXDebugConifgLocaton(string) error
GetXDebugConfigLocaton() string
}
type defineXdebugIniFileLocationDialog interface {
XDebugConfigLocation() (int, string, error)
}
func defineXdebugIniFileLocation(cfg defineXdebugIniFileLocationProjectConfig, d defineXdebugIniFileLocationDialog) (err error) {
if cfg.GetXDebugConfigLocaton() == "" {
var path string
if _, path, err = d.XDebugConfigLocation(); err != nil {
return err
}
if path == "" {
return errors.New("Xdebug config file locaton cannot be empty")
}
return cfg.SaveXDebugConifgLocaton(path)
}
return nil
}