-
Notifications
You must be signed in to change notification settings - Fork 1
/
composer.go
222 lines (177 loc) · 6.89 KB
/
composer.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
package command
import (
"strings"
"github.com/urfave/cli/v2" // imports as package "cli"
)
type composer struct {
usage map[string]string
aliases map[string]string
description map[string]string
initFunction func()
containerList []string
locaton func(string, string) (string, error)
ctype string
command string
}
func (cmp *composer) GetCommandLocaton() func(string, string) (string, error) {
return cmp.locaton
}
func (cmp *composer) GetCallType() string {
return cmp.ctype
}
func (cmp *composer) GetComposerCommand() string {
return cmp.command
}
func parseCommand(composercommand string) (string, string, string) {
commandstack := strings.Split(composercommand, ":")
var calltype, dockercmd string
if len(commandstack) > 1 {
dockercmd = strings.Trim(commandstack[1], " ")
}
if len(commandstack) > 2 {
calltype = commandstack[2]
}
index := strings.Join(commandstack[1:], ":")
if index == "" {
index = "composer"
} else if index == "memory" {
index = "composer:memory"
dockercmd = ""
calltype = "memory"
}
return index, calltype, dockercmd
}
type callComposerCommandProjectConfig interface {
GetProjectDockerPath() string
GetProjectMainContainer() string
SaveContainerNameToProjectConfig(string) error
GetCommandInactveStatus(string) bool
SaveContainerUserToProjectConfig(string) error
GetMainContainerUser() string
}
type callComposerCommandDialog interface {
SetMainContaner([]string) (int, string, error)
SetMainContanerUser() (string, error)
}
type callComposerCommandOptions interface {
GetInitFunction() func(bool) string
GetContainerList() ([]string, error)
GetExecCommand() func(ExecOptions, *cli.App) error
GetCommandLocation() func(string, string) (string, error)
}
// CallComposerCommand generates composer commands
// https://medium.com/@ssttehrani/containers-from-scratch-with-golang-5276576f9909
// https://phase2.github.io/devtools/common-tasks/ssh-into-a-container/
func CallComposerCommand(composercommand string, cfg callComposerCommandProjectConfig, d callComposerCommandDialog, options callComposerCommandOptions) *cli.Command {
index, calltype, dockercmd := parseCommand(composercommand)
initf := options.GetInitFunction()
execCommand := options.GetExecCommand()
cmp := &composer{
usage: map[string]string{
"composer": "Runs composer: {docker exec -it phpContainer composer} [custom parameters]",
"composer:memory": "Runs composer with no memory constraint: {docker exec -i phpContainer /usr/bin/php -d memory_limit=-1 /usr/local/bin/composer} [custom parameters]",
"install": "Runs composer install: {docker exec -it phpContainer composer install} [custom parameters]",
"install:memory": "Runs composer install with no memory constraint: {docker exec -i phpContainer /usr/bin/php -d memory_limit=-1 /usr/local/bin/composer install} [custom parameters]",
"update": "Runs composer update: {docker exec -it phpContainer composer update} [custom parameters]",
"update:memory": "Runs composer update with no memory constraint: {docker exec -i phpContainer /usr/bin/php -d memory_limit=-1 /usr/local/bin/composer update} [custom parameters]",
},
aliases: map[string]string{
"composer": "cmp",
"composer:memory": "cmpm",
"install": "cmpi",
"update": "cmpu",
"install:memory": "cmpim",
"update:memory": "cmpum",
},
description: map[string]string{
"composer": "phpContainer is taken from project config file",
"composer:memory": "phpContainer is taken from project config file, php and composer commands will be found automatically",
"install": "phpContainer is taken from project config file",
"install:memory": "phpContainer is taken from project config file, php and composer commands will be found automatically",
"update": "phpContainer is taken from project config file",
"update:memory": "phpContainer is taken from project config file, php and composer commands will be found automatically",
},
locaton: options.GetCommandLocation(),
ctype: calltype,
command: dockercmd,
}
return &cli.Command{
Name: composercommand,
Aliases: []string{cmp.aliases[index]},
Usage: cmp.usage[index],
Description: cmp.description[index],
Hidden: cfg.GetCommandInactveStatus("composer"),
SkipFlagParsing: true,
Action: func(c *cli.Context) (err error) {
initf(true)
var args []string
if args, err = composerHandle(cfg, d, cmp, options, c.Args()); err != nil {
return err
}
eo := ExecOptions{
command: "docker",
args: args,
tty: true,
detach: true,
}
return execCommand(eo, c.App)
},
}
}
type composerInterface interface {
GetCommandLocaton() func(string, string) (string, error)
GetCallType() string
GetComposerCommand() string
}
type composerHandleProjectConfig interface {
GetProjectMainContainer() string
SaveContainerNameToProjectConfig(string) error
GetMainContainerUser() string
SaveContainerUserToProjectConfig(string) error
}
type composerHandleDialog interface {
SetMainContaner([]string) (int, string, error)
SetMainContanerUser() (string, error)
}
func composerHandle(cfg composerHandleProjectConfig, d composerHandleDialog, c composerInterface, options containerlist, a cli.Args) ([]string, error) {
var err error
var cl []string
if cl, err = options.GetContainerList(); err != nil {
return []string{}, err
}
if err = defineProjectMainContainer(cfg, d, cl); err != nil {
return []string{}, err
}
if err = defineProjectMainContainerUser(cfg, d); err != nil {
return []string{}, err
}
var initArgs = []string{"exec"}
composerArgs := []string{"-it", cfg.GetProjectMainContainer(), "composer"}
if cfg.GetMainContainerUser() != "root" && cfg.GetMainContainerUser() != "" {
composerArgs = []string{"-it", "-u", cfg.GetMainContainerUser(), cfg.GetProjectMainContainer(), "composer"}
}
if c.GetCallType() == "memory" || a.Get(0) == "m" {
var phpLocation, composerLocation string
getCommandLocation := c.GetCommandLocaton()
if phpLocation, err = getCommandLocation(cfg.GetProjectMainContainer(), "php"); err != nil {
return nil, err
}
if composerLocation, err = getCommandLocation(cfg.GetProjectMainContainer(), "composer"); err != nil {
return nil, err
}
composerArgs = []string{"-i", cfg.GetProjectMainContainer(), phpLocation, "-d", "memory_limit=-1", composerLocation}
if cfg.GetMainContainerUser() != "root" && cfg.GetMainContainerUser() != "" {
composerArgs = []string{"-i", "-u", cfg.GetMainContainerUser(), cfg.GetProjectMainContainer(), phpLocation, "-d", "memory_limit=-1", composerLocation}
}
}
if len(c.GetComposerCommand()) > 0 {
composerArgs = append(composerArgs, c.GetComposerCommand())
}
initArgs = append(initArgs, composerArgs...)
extraInitArgs := a.Slice()
if a.Get(0) == "m" {
extraInitArgs = a.Tail()
}
args := append(initArgs, extraInitArgs...)
return args, nil
}