-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathroot.go
188 lines (169 loc) · 5.66 KB
/
root.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
/*
* Copyright (c) 2021-present Fabien Potencier <fabien@symfony.com>
*
* This file is part of Symfony CLI project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package commands
import (
"os/exec"
"path/filepath"
"github.com/pkg/errors"
"github.com/symfony-cli/console"
"github.com/symfony-cli/symfony-cli/local/platformsh"
"github.com/symfony-cli/symfony-cli/reexec"
"github.com/symfony-cli/symfony-cli/updater"
"github.com/symfony-cli/symfony-cli/util"
"github.com/symfony-cli/terminal"
)
var (
dirFlag = &console.StringFlag{Name: "dir", Usage: "Project directory"}
projectFlag = &console.StringFlag{Name: "project", Aliases: []string{"p"}, Usage: "The project ID or URL"}
environmentFlag = &console.StringFlag{Name: "environment", Aliases: []string{"e"}, Usage: "The environment ID"}
)
func CommonCommands() []*console.Command {
adminCommands := []*console.Command{
// common cloud commands
// FIXME: this command should be renamed (it's a special command as it's local but creates cloud files)
projectInitCmd,
// FIXME: this command should be renamed (it works locally AND in the cloud)
variableExportCmd,
// commands that we override to provide more features
//environmentSSHCmd,
}
localCommands := []*console.Command{
binConsoleWrapper,
composerWrapper,
phpWrapper,
bookCheckReqsCmd,
bookCheckoutCmd,
cloudEnvDebugCmd,
doctrineCheckServerVersionSettingCmd,
localNewCmd,
localPhpListCmd,
localPhpRefreshCmd,
localProxyTLD,
localProxyAttachDomainCmd,
localProxyDetachDomainCmd,
localProxyStartCmd,
localProxyStatusCmd,
localProxyUrlCmd,
localProxyStopCmd,
localRequirementsCheckCmd,
localRunCmd,
localServerCAInstallCmd,
localServerCAUninstallCmd,
localServerListCmd,
localServerLogCmd,
localServerProdCmd,
localServerStartCmd,
localServerStatusCmd,
localServerStopCmd,
localVariableExposeFromTunnelCmd,
localSecurityCheckCmd,
projectLocalMailCatcherOpenCmd,
projectLocalRabbitMQManagementOpenCmd,
projectLocalServiceOpenCmd,
projectLocalOpenCmd,
}
return append(localCommands, adminCommands...)
}
func CheckGitIsAvailable(c *console.Context) error {
if _, err := exec.LookPath("git"); err != nil {
return errors.New("Git is a requirement and it cannot be found, please install it first.")
}
return nil
}
func init() {
initCLI()
}
func InitAppFunc(c *console.Context) error {
psh, err := platformsh.Get()
if err != nil {
return err
}
for name, f := range platformshBeforeHooks {
psh.AddBeforeHook(name, f)
}
checkForUpdates(c)
return nil
}
func checkForUpdates(c *console.Context) {
if c.App.Channel != "stable" {
return
}
// do not run auto-update in the cloud, CI or background jobs
if util.InCloud() || !terminal.Stdin.IsInteractive() || reexec.IsChild() {
return
}
updater := updater.NewUpdater(filepath.Join(util.GetHomeDir(), "update"), c.App.ErrWriter, terminal.IsDebug())
updater.CheckForNewVersion(c.App.Version)
}
// WelcomeAction displays a message when no command
func WelcomeAction(c *console.Context) error {
console.ShowVersion(c)
terminal.Println(c.App.Usage)
terminal.Println("")
terminal.Println("These are common commands used in various situations:")
terminal.Println("")
terminal.Println("<comment>Work on a project locally</>")
terminal.Println("")
displayCommandsHelp(c, []*console.Command{
localNewCmd,
localServerStartCmd,
localServerStopCmd,
localSecurityCheckCmd,
composerWrapper,
binConsoleWrapper,
phpWrapper,
})
terminal.Println("")
terminal.Println("<comment>Manage a project on Cloud</>")
terminal.Println("")
psh, err := platformsh.Get()
if err != nil {
return err
}
displayCommandsHelp(c, append([]*console.Command{projectInitCmd}, psh.PSHMainCommands()...))
terminal.Println("")
terminal.Printf("Show all commands with <info>%s help</>,\n", c.App.HelpName)
terminal.Printf("Get help for a specific command with <info>%s help COMMAND</>.\n", c.App.HelpName)
return nil
}
func displayCommandsHelp(c *console.Context, cmds []*console.Command) {
console.HelpPrinter(c.App.Writer, `{{range .}} <info>{{.PreferredName}}</>{{"\t"}}{{.Usage}}{{"\n"}}{{end}}`, cmds)
}
func initCLI() {
formatter := terminal.Stdout.GetFormatter()
formatter.SetStyle("sc", terminal.NewFormatterStyle("cyan", "", nil))
formatter.SetStyle("bold", terminal.NewFormatterStyle("", "", []string{"bold"}))
formatter.SetStyle("failure", terminal.NewFormatterStyle("red", "", nil))
formatter.AddAlias("notes", "warning")
console.AppHelpTemplate += `
<comment>Available wrappers:</>
Runs PHP (version depends on project's configuration).
Environment variables to use Platform.sh/Upsun relationships or Docker services are automatically defined.
{{with .Command "composer"}} <info>{{.PreferredName}}</>{{"\t"}}{{.Usage}}{{end}}
{{with .Command "console"}} <info>{{.PreferredName}}</>{{"\t"}}{{.Usage}}{{end}}
{{with .Command "php"}} <info>{{.PreferredName}}</>{{"\t"}}{{.Usage}}{{end}}
`
}
func getProjectDir(dir string) (string, error) {
var err error
if dir, err = filepath.Abs(dir); err != nil {
return "", err
}
return filepath.EvalSymlinks(dir)
}