-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathproject_create.go
More file actions
286 lines (253 loc) · 10.9 KB
/
Copy pathproject_create.go
File metadata and controls
286 lines (253 loc) · 10.9 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
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
package project
import (
"fmt"
"path/filepath"
"strings"
"github.com/shyim/go-composer/repository"
"github.com/spf13/cobra"
"github.com/shopware/shopware-cli/internal/proxy"
"github.com/shopware/shopware-cli/internal/shop"
"github.com/shopware/shopware-cli/internal/system"
"github.com/shopware/shopware-cli/internal/tui"
)
const (
// projectNameHelp is the help text shown under the project name input.
projectNameHelp = "The name of the project directory to create (leave empty to use the current directory)"
)
// projectNameFieldDescription returns the description shown under the project
// name input in the interactive form. While the typed name is invalid it
// returns the rule highlighted in red, validating the input live; otherwise it
// returns the regular help text.
func projectNameFieldDescription(name string) string {
if name != "" {
if err := shop.ValidateProjectName(name); err != nil {
return tui.RedText.Render(shop.ProjectNameRule)
}
}
return projectNameHelp
}
type createOptions struct {
projectFolder string
selectedVersion string
selectedDeployment string
selectedCI string
// phpVersion is the major.minor PHP series the project uses: the Docker image
// tag for Docker projects, the local PHP lookup otherwise. Persisted as-is.
phpVersion string
// phpVersionExplicit records that --php-version was passed, so the creation
// form does not ask again.
phpVersionExplicit bool
// phpBinary is the local executable phpVersion resolved to; never persisted.
phpBinary string
useDocker bool
initGit bool
withElasticsearch bool
withAMQP bool
noAudit bool
// useLocalDomain serves the shop at a stable hostname
// (<name>.<baseDomain>) through the shared proxy instead of a fixed port.
// Only meaningful with Docker.
useLocalDomain bool
// setupProxyNow runs the one-time machine setup (DNS + HTTPS trust, needs
// sudo) inline during create, so the local domain works immediately. Set
// only when the user opts in and the machine is not configured yet.
setupProxyNow bool
interactive bool
elasticsearchExplicit bool
isVerbose bool
}
func (o *createOptions) setPHP(installation system.PHPInstallation) {
o.phpBinary = installation.Binary
o.phpVersion = system.PHPVersionPin(installation.Version)
}
// clearPHP drops a resolved local PHP, e.g. when the form switches to Docker. An
// explicit --php-version is kept, since it applies to Docker projects too.
func (o *createOptions) clearPHP() {
o.phpBinary = ""
if !o.phpVersionExplicit {
o.phpVersion = ""
}
}
// localDomainHostname returns the stable proxy hostname for a project name,
// e.g. "my-shop.shopware.local". Underscores (valid in a project name but not
// in a hostname) become dashes.
func localDomainHostname(name, baseDomain string) string {
label := strings.ReplaceAll(filepath.Base(name), "_", "-")
return label + "." + baseDomain
}
// proxyBaseDomain returns the machine-wide proxy base domain, falling back to
// the default when no settings are stored yet.
func proxyBaseDomain() string {
if s, err := proxy.LoadSettings(); err == nil {
return s.BaseDomain()
}
return proxy.DefaultDomain
}
// resolveLocalDomainChoice derives the final local-domain settings from the
// individual inputs. Local domains require Docker, so useLocalDomain is always
// gated on useDocker regardless of how the choice was made (interactive or the
// --local-domain flag). setupProxyNow — which triggers the one-time sudo setup
// inline — is only ever true when the choice came from the interactive prompt
// (promptShown), so passing --local-domain never runs sudo without asking.
func resolveLocalDomainChoice(useDocker, wantLocalDomain, promptShown, machineSetupDone, setupNowAnswer bool) (useLocalDomain, setupProxyNow bool) {
useLocalDomain = useDocker && wantLocalDomain
setupProxyNow = useLocalDomain && promptShown && !machineSetupDone && setupNowAnswer
return useLocalDomain, setupProxyNow
}
var projectCreateCmd = &cobra.Command{
Use: "create [name] [version]",
Short: "Create a new Shopware 6 project",
Args: cobra.MaximumNArgs(2),
ValidArgsFunction: func(cmd *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
return []string{}, cobra.ShellCompDirectiveFilterDirs
}
if len(args) == 1 {
pkg, err := repository.New(repository.PackagistURL, nil).GetPackage(cmd.Context(), "shopware/core")
if err != nil {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}
filteredVersions := shop.FilterInstallVersions(pkg.Versions)
versions := make([]string, 0, len(filteredVersions)+1)
versions = append(versions, shop.VersionLatest)
for _, v := range filteredVersions {
versions = append(versions, v.String())
}
return versions, cobra.ShellCompDirectiveNoFileComp
}
return []string{}, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
opts := parseCreateFlags(cmd, args)
if opts.phpVersionExplicit {
if err := shop.ValidatePHPVersion(opts.phpVersion); err != nil {
return err
}
}
// A name passed directly as an argument skips the interactive name
// prompt, which is where invalid names (e.g. wrong casing) are normally
// rejected live. Validate it up front so it is forbidden immediately
// instead of only after the rest of the form has been completed.
if opts.projectFolder != "" {
if err := shop.ValidateProjectName(opts.projectFolder); err != nil {
return err
}
}
if opts.interactive {
tui.PrintBanner()
}
pkg, err := repository.New(repository.PackagistURL, nil).GetPackage(cmd.Context(), "shopware/core")
if err != nil {
return err
}
releases := pkg.Versions
filteredVersions := shop.FilterInstallVersions(releases)
if opts.interactive {
if err := runCreateForm(cmd, &opts, releases, filteredVersions); err != nil {
return err
}
} else {
if err := applyNonInteractiveDefaults(&opts); err != nil {
return err
}
}
chosenVersion, phpConstraint, err := validateAndPreflight(cmd.Context(), &opts, releases, filteredVersions)
if err != nil {
return err
}
// Do the one-time machine setup up front (while the user is still at the
// keyboard for the sudo prompt), before the long composer install. It is
// best-effort: a blocked/declined sudo just means the domain resolves
// once the user runs `project proxy setup` later.
if opts.setupProxyNow {
fmt.Println()
fmt.Println(tui.BoldText.Render("Setting up local domains (one-time, needs sudo)"))
_ = runInlineProxySetup(cmd.Context(), proxyBaseDomain())
}
if err := scaffoldProject(cmd.Context(), &opts, chosenVersion); err != nil {
return err
}
return installAndFinalize(cmd, &opts, phpConstraint, chosenVersion)
},
}
func parseCreateFlags(cmd *cobra.Command, args []string) createOptions {
useDocker, _ := cmd.PersistentFlags().GetBool("docker")
withElasticsearch, _ := cmd.PersistentFlags().GetBool("with-elasticsearch")
withAMQP, _ := cmd.PersistentFlags().GetBool("with-amqp")
noAudit, _ := cmd.PersistentFlags().GetBool("no-audit")
initGit, _ := cmd.PersistentFlags().GetBool("git")
localDomain, _ := cmd.PersistentFlags().GetBool("local-domain")
versionFlag, _ := cmd.PersistentFlags().GetString("version")
deploymentMethod, _ := cmd.PersistentFlags().GetString("deployment")
ciSystem, _ := cmd.PersistentFlags().GetString("ci")
phpVersion, _ := cmd.PersistentFlags().GetString("php-version")
if cmd.PersistentFlags().Changed("without-elasticsearch") {
withoutElasticsearch, _ := cmd.PersistentFlags().GetBool("without-elasticsearch")
withElasticsearch = !withoutElasticsearch
}
elasticsearchExplicit := cmd.PersistentFlags().Changed("with-elasticsearch") || cmd.PersistentFlags().Changed("without-elasticsearch")
isVerbose, _ := cmd.Flags().GetBool("verbose")
opts := createOptions{
useDocker: useDocker,
withElasticsearch: withElasticsearch,
withAMQP: withAMQP,
noAudit: noAudit,
initGit: initGit,
useLocalDomain: localDomain,
selectedVersion: versionFlag,
selectedDeployment: deploymentMethod,
selectedCI: ciSystem,
phpVersion: phpVersion,
phpVersionExplicit: cmd.PersistentFlags().Changed("php-version"),
interactive: system.IsInteractionEnabled(cmd.Context()),
elasticsearchExplicit: elasticsearchExplicit,
isVerbose: isVerbose,
}
if len(args) > 0 {
opts.projectFolder = args[0]
}
if len(args) > 1 && opts.selectedVersion == "" {
opts.selectedVersion = args[1]
}
return opts
}
func applyNonInteractiveDefaults(opts *createOptions) error {
if opts.projectFolder == "" {
opts.projectFolder = "."
}
if opts.selectedVersion == "" {
opts.selectedVersion = shop.VersionLatest
}
if opts.selectedDeployment == "" {
opts.selectedDeployment = shop.DeploymentNone
}
if opts.selectedCI == "" {
opts.selectedCI = shop.CINone
}
if !opts.elasticsearchExplicit {
opts.withElasticsearch = true
}
// Local domains need Docker; drop the flag if Docker is off. Never run the
// one-time sudo setup non-interactively.
opts.useLocalDomain = opts.useDocker && opts.useLocalDomain
opts.setupProxyNow = false
return nil
}
func init() {
projectRootCmd.AddCommand(projectCreateCmd)
projectCreateCmd.PersistentFlags().Bool("docker", false, "Use Docker to run Composer instead of local installation")
projectCreateCmd.PersistentFlags().Bool("with-elasticsearch", false, "Include Elasticsearch/OpenSearch support")
projectCreateCmd.PersistentFlags().Bool("without-elasticsearch", false, "Remove Elasticsearch from the installation")
_ = projectCreateCmd.PersistentFlags().MarkDeprecated("without-elasticsearch", "use --with-elasticsearch instead")
projectCreateCmd.PersistentFlags().Bool("with-amqp", false, "Include AMQP queue support (symfony/amqp-messenger)")
projectCreateCmd.PersistentFlags().Bool("no-audit", false, "Disable composer audit blocking insecure packages")
projectCreateCmd.PersistentFlags().Bool("git", false, "Initialize a Git repository")
projectCreateCmd.PersistentFlags().Bool("local-domain", false, "Serve the shop at a stable local hostname (<name>.shopware.local) via the shared proxy instead of a port (requires Docker)")
projectCreateCmd.PersistentFlags().String("version", "", "Shopware version to install (e.g., 6.6.0.0, latest)")
projectCreateCmd.PersistentFlags().String("deployment", "", "Deployment method: none, deployer, platformsh, shopware-paas")
projectCreateCmd.PersistentFlags().String("ci", "", "CI/CD system: none, github, gitlab")
projectCreateCmd.PersistentFlags().String("php-version", "", "PHP version to use (e.g. 8.3); selects the local PHP for local projects and the image tag for --docker projects")
_ = projectCreateCmd.RegisterFlagCompletionFunc("php-version", func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
return shop.SupportedPHPVersions, cobra.ShellCompDirectiveNoFileComp
})
}