/
config.go
745 lines (619 loc) · 22 KB
/
config.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
package ddevapp
import (
"bytes"
"fmt"
"html/template"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/drud/ddev/pkg/globalconfig"
"regexp"
"runtime"
"github.com/drud/ddev/pkg/appports"
"github.com/drud/ddev/pkg/exec"
"github.com/drud/ddev/pkg/fileutil"
"github.com/drud/ddev/pkg/output"
"github.com/drud/ddev/pkg/util"
"github.com/drud/ddev/pkg/version"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
// Regexp pattern to determine if a hostname is valid per RFC 1123.
var hostRegex = regexp.MustCompile(`^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$`)
// Command defines commands to be run as pre/post hooks
type Command struct {
Exec string `yaml:"exec,omitempty"`
ExecHost string `yaml:"exec-host,omitempty"`
}
// Provider is the interface which all provider plugins must implement.
type Provider interface {
Init(app *DdevApp) error
ValidateField(string, string) error
PromptForConfig() error
Write(string) error
Read(string) error
Validate() error
GetBackup(string, string) (fileLocation string, importPath string, err error)
}
// init() is for testing situations only, allowing us to override the default webserver type
// or caching behavior
func init() {
// This is for automated testing only. It allows us to override the webserver type.
if testWebServerType := os.Getenv("DDEV_TEST_WEBSERVER_TYPE"); testWebServerType != "" {
WebserverDefault = testWebServerType
}
if testWebCache := os.Getenv("DDEV_TEST_USE_WEBCACHE"); testWebCache != "" {
WebCacheEnabledDefault = true
}
}
// NewApp creates a new DdevApp struct with defaults set and overridden by any existing config.yml.
func NewApp(AppRoot string, provider string) (*DdevApp, error) {
// Set defaults.
app := &DdevApp{}
app.AppRoot = AppRoot
app.ConfigPath = app.GetConfigPath("config.yaml")
app.APIVersion = version.DdevVersion
app.PHPVersion = PHPDefault
app.WebserverType = WebserverDefault
app.WebcacheEnabled = WebCacheEnabledDefault
app.RouterHTTPPort = DdevDefaultRouterHTTPPort
app.RouterHTTPSPort = DdevDefaultRouterHTTPSPort
app.MariaDBVersion = version.MariaDBDefaultVersion
// These should always default to the latest image/tag names from the Version package.
app.WebImage = version.GetWebImage()
app.DBImage = version.GetDBImage(app.MariaDBVersion)
app.DBAImage = version.GetDBAImage()
// Load from file if available. This will return an error if the file doesn't exist,
// and it is up to the caller to determine if that's an issue.
if _, err := os.Stat(app.ConfigPath); !os.IsNotExist(err) {
err = app.ReadConfig()
if err != nil {
return app, fmt.Errorf("%v exists but cannot be read. It may be invalid due to a syntax error.: %v", app.ConfigPath, err)
}
}
// Allow override with provider.
// Otherwise we accept whatever might have been in config file if there was anything.
if provider == "" && app.Provider != "" {
// Do nothing. This is the case where the config has a provider and no override is provided. Config wins.
} else if provider == ProviderPantheon || provider == ProviderDrudS3 || provider == ProviderDefault {
app.Provider = provider // Use the provider passed-in. Function argument wins.
} else if provider == "" && app.Provider == "" {
app.Provider = ProviderDefault // Nothing passed in, nothing configured. Set c.Provider to default
} else {
return app, fmt.Errorf("provider '%s' is not implemented", provider)
}
app.SetRavenTags()
return app, nil
}
// GetConfigPath returns the path to an application config file specified by filename.
func (app *DdevApp) GetConfigPath(filename string) string {
return filepath.Join(app.AppRoot, ".ddev", filename)
}
// WriteConfig writes the app configuration into the .ddev folder.
func (app *DdevApp) WriteConfig() error {
// Work against a copy of the DdevApp, since we don't want to actually change it.
appcopy := *app
// Update the "APIVersion" to be the ddev version.
appcopy.APIVersion = version.DdevVersion
// Only set the images on write if non-default values have been specified.
if appcopy.WebImage == version.GetWebImage() {
appcopy.WebImage = ""
}
if appcopy.DBImage == version.GetDBImage(appcopy.MariaDBVersion) {
appcopy.DBImage = ""
}
if appcopy.DBAImage == version.GetDBAImage() {
appcopy.DBAImage = ""
}
if appcopy.DBAImage == version.GetDBAImage() {
appcopy.DBAImage = ""
}
if appcopy.BgsyncImage == version.GetBgsyncImage() {
appcopy.BgsyncImage = ""
}
// Don't write default working dir values to config
defaults := appcopy.DefaultWorkingDirMap()
for service, defaultWorkingDir := range defaults {
if app.WorkingDir[service] == defaultWorkingDir {
delete(appcopy.WorkingDir, service)
}
}
err := PrepDdevDirectory(filepath.Dir(appcopy.ConfigPath))
if err != nil {
return err
}
cfgbytes, err := yaml.Marshal(appcopy)
if err != nil {
return err
}
// Append current image information
cfgbytes = append(cfgbytes, []byte(fmt.Sprintf("\n\n# This config.yaml was created with ddev version %s \n# webimage: %s\n# dbimage: %s\n# dbaimage: %s\n# bgsyncimage: %s\n# However we do not recommend explicitly wiring these images into the\n# config.yaml as they may break future versions of ddev.\n# You can update this config.yaml using 'ddev config'.\n", version.DdevVersion, version.GetWebImage(), version.GetDBImage(), version.GetDBAImage(), version.GetBgsyncImage()))...)
// Append hook information and sample hook suggestions.
cfgbytes = append(cfgbytes, []byte(ConfigInstructions)...)
cfgbytes = append(cfgbytes, appcopy.GetHookDefaultComments()...)
err = ioutil.WriteFile(appcopy.ConfigPath, cfgbytes, 0644)
if err != nil {
return err
}
provider, err := appcopy.GetProvider()
if err != nil {
return err
}
err = provider.Write(appcopy.GetConfigPath("import.yaml"))
if err != nil {
return err
}
// Allow project-specific post-config action
err = appcopy.PostConfigAction()
if err != nil {
return err
}
return nil
}
// ReadConfig reads app configuration from a specified location on disk, falling
// back to defaults for config values not defined in the read config file.
func (app *DdevApp) ReadConfig() error {
source, err := ioutil.ReadFile(app.ConfigPath)
if err != nil {
return fmt.Errorf("could not find an active ddev configuration at %s have you run 'ddev config'? %v", app.ConfigPath, err)
}
// validate extend command keys
err = validateCommandYaml(source)
if err != nil {
return fmt.Errorf("invalid configuration in %s: %v", app.ConfigPath, err)
}
// ReadConfig config values from file.
err = yaml.Unmarshal(source, app)
if err != nil {
return err
}
// If any of these values aren't defined in the config file, set them to defaults.
if app.Name == "" {
app.Name = filepath.Base(app.AppRoot)
}
if app.PHPVersion == "" {
app.PHPVersion = PHPDefault
}
if app.WebserverType == "" {
app.WebserverType = WebserverDefault
}
if WebCacheEnabledDefault == true {
app.WebcacheEnabled = WebCacheEnabledDefault
}
if app.RouterHTTPPort == "" {
app.RouterHTTPPort = DdevDefaultRouterHTTPPort
}
if app.RouterHTTPSPort == "" {
app.RouterHTTPSPort = DdevDefaultRouterHTTPSPort
}
if app.WebImage == "" {
app.WebImage = version.GetWebImage()
}
app.DBImage = version.GetDBImage(app.MariaDBVersion)
if app.DBAImage == "" {
app.DBAImage = version.GetDBAImage()
}
if app.BgsyncImage == "" {
app.BgsyncImage = version.GetBgsyncImage()
}
if app.OmitContainers == nil {
app.OmitContainers = globalconfig.DdevGlobalConfig.OmitContainers
}
app.ImportDir = app.GetConfigPath("import-db")
app.SetApptypeSettingsPaths()
return nil
}
// WarnIfConfigReplace just messages user about whether config is being replaced or created
func (app *DdevApp) WarnIfConfigReplace() {
if app.ConfigExists() {
util.Warning("You are reconfiguring the project at %s. \nThe existing configuration will be updated and replaced.", app.AppRoot)
} else {
util.Success("Creating a new ddev project config in the current directory (%s)", app.AppRoot)
util.Success("Once completed, your configuration will be written to %s\n", app.ConfigPath)
}
}
// PromptForConfig goes through a set of prompts to receive user input and generate an Config struct.
func (app *DdevApp) PromptForConfig() error {
app.WarnIfConfigReplace()
for {
err := app.promptForName()
if err == nil {
break
}
output.UserOut.Printf("%v", err)
}
if err := app.docrootPrompt(); err != nil {
return err
}
err := app.AppTypePrompt()
if err != nil {
return err
}
err = app.ConfigFileOverrideAction()
if err != nil {
return err
}
err = app.providerInstance.PromptForConfig()
return err
}
// ValidateConfig ensures the configuration meets ddev's requirements.
func (app *DdevApp) ValidateConfig() error {
provider, err := app.GetProvider()
if err != nil {
return err.(invalidProvider)
}
// validate project name
if err = provider.ValidateField("Name", app.Name); err != nil {
return err.(invalidAppName)
}
// validate hostnames
for _, hn := range app.GetHostnames() {
if !hostRegex.MatchString(hn) {
return fmt.Errorf("invalid hostname: %s. See https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_hostnames for valid hostname requirements", hn).(invalidHostname)
}
}
// validate apptype
if !IsValidAppType(app.Type) {
return fmt.Errorf("invalid app type: %s", app.Type).(invalidAppType)
}
// validate PHP version
if !IsValidPHPVersion(app.PHPVersion) {
return fmt.Errorf("invalid PHP version: %s, must be one of %v", app.PHPVersion, GetValidPHPVersions()).(invalidPHPVersion)
}
// validate webserver type
if !IsValidWebserverType(app.WebserverType) {
return fmt.Errorf("invalid webserver type: %s, must be one of %s", app.WebserverType, GetValidWebserverTypes()).(invalidWebserverType)
}
if !IsValidOmitContainers(app.OmitContainers) {
return fmt.Errorf("Invalid omit_containers: %s, must be one of %s", app.OmitContainers, GetValidOmitContainers()).(InvalidOmitContainers)
}
// Validate mariadb version
if !IsValidMariaDBVersion(app.MariaDBVersion) {
return fmt.Errorf("Invalid mariadb_version: %s, must be one of %s", app.MariaDBVersion, GetValidMariaDBVersions()).(invalidMariaDBVersion)
}
// Validate webcache_enabled
if runtime.GOOS != "darwin" && app.WebcacheEnabled && !globalconfig.DdevGlobalConfig.DeveloperMode {
return fmt.Errorf("webcache_enabled: true is not yet avalable for %s", runtime.GOOS)
}
return nil
}
// DockerComposeYAMLPath returns the absolute path to where the
// docker-compose.yaml should exist for this app.
func (app *DdevApp) DockerComposeYAMLPath() string {
return app.GetConfigPath("docker-compose.yaml")
}
// GetHostname returns the primary hostname of the app.
func (app *DdevApp) GetHostname() string {
return app.Name + "." + version.DDevTLD
}
// GetHostnames returns an array of all the configured hostnames.
func (app *DdevApp) GetHostnames() []string {
// Use a map to make sure that we have unique hostnames
// The value is useless, so just use the int 1 for assignment.
nameListMap := make(map[string]int)
nameListMap[app.GetHostname()] = 1
for _, name := range app.AdditionalHostnames {
nameListMap[name+"."+version.DDevTLD] = 1
}
for _, name := range app.AdditionalFQDNs {
nameListMap[name] = 1
}
// Now walk the map and extract the keys into an array.
nameListArray := make([]string, 0, len(nameListMap))
for k := range nameListMap {
nameListArray = append(nameListArray, k)
}
return nameListArray
}
// WriteDockerComposeConfig writes a docker-compose.yaml to the app configuration directory.
func (app *DdevApp) WriteDockerComposeConfig() error {
var err error
if fileutil.FileExists(app.DockerComposeYAMLPath()) {
found, err := fileutil.FgrepStringInFile(app.DockerComposeYAMLPath(), DdevFileSignature)
util.CheckErr(err)
// If we did *not* find the ddev file signature in docker-compose.yaml, we'll back it up and warn about it.
if !found {
util.Warning("User-managed docker-compose.yaml will be replaced with ddev-generated docker-compose.yaml. Original file will be placed in docker-compose.yaml.bak")
_ = os.Remove(app.DockerComposeYAMLPath() + ".bak")
err = os.Rename(app.DockerComposeYAMLPath(), app.DockerComposeYAMLPath()+".bak")
util.CheckErr(err)
}
}
f, err := os.Create(app.DockerComposeYAMLPath())
if err != nil {
return err
}
defer util.CheckClose(f)
rendered, err := app.RenderComposeYAML()
if err != nil {
return err
}
_, err = f.WriteString(rendered)
if err != nil {
return err
}
return err
}
// CheckCustomConfig warns the user if any custom configuration files are in use.
func (app *DdevApp) CheckCustomConfig() {
// Get the path to .ddev for the current app.
ddevDir := filepath.Dir(app.ConfigPath)
customConfig := false
if _, err := os.Stat(filepath.Join(ddevDir, "nginx-site.conf")); err == nil && app.WebserverType == WebserverNginxFPM {
util.Warning("Using custom nginx configuration in nginx-site.conf")
customConfig = true
}
if _, err := os.Stat(filepath.Join(ddevDir, "apache", "apache-site.conf")); err == nil && app.WebserverType != WebserverNginxFPM {
util.Warning("Using custom apache configuration in apache/apache-site.conf")
customConfig = true
}
mysqlPath := filepath.Join(ddevDir, "mysql")
if _, err := os.Stat(mysqlPath); err == nil {
mysqlFiles, err := filepath.Glob(mysqlPath + "/*.cnf")
util.CheckErr(err)
if len(mysqlFiles) > 0 {
util.Warning("Using custom mysql configuration: %v", mysqlFiles)
customConfig = true
}
}
phpPath := filepath.Join(ddevDir, "php")
if _, err := os.Stat(phpPath); err == nil {
phpFiles, err := filepath.Glob(phpPath + "/*.ini")
util.CheckErr(err)
if len(phpFiles) > 0 {
util.Warning("Using custom PHP configuration: %v", phpFiles)
customConfig = true
}
}
if customConfig {
util.Warning("Custom configuration takes effect when container is created, \nusually on start, use 'ddev restart' or 'ddev rm && ddev start' if you're not seeing it take effect.")
}
}
type composeYAMLVars struct {
Name string
Plugin string
AppType string
MailhogPort string
DBAPort string
DBPort string
DdevGenerated string
ExtraHost string
ComposeVersion string
MountType string
WebMount string
OmitDBA bool
OmitSSHAgent bool
WebcacheEnabled bool
IsWindowsFS bool
}
// RenderComposeYAML renders the contents of docker-compose.yaml.
func (app *DdevApp) RenderComposeYAML() (string, error) {
var doc bytes.Buffer
var err error
var docker0Addr = "127.0.0.1"
var docker0Hostname = "unneeded"
templ := template.New("compose template")
templ, err = templ.Parse(DDevComposeTemplate)
if err != nil {
return "", err
}
// Docker 18.03 on linux doesn't define host.docker.internal
// so we need to go get the ip address of docker0
// We would hope to be able to remove this when
// https://github.com/docker/for-linux/issues/264 gets resolved.
if runtime.GOOS == "linux" {
out, err := exec.RunCommandPipe("ip", []string{"address", "show", "dev", "docker0"})
// Do not process if ip command fails, we'll just ignore and not act.
if err == nil {
addr := regexp.MustCompile(`inet *[0-9\.]+`).FindString(out)
components := strings.Split(addr, " ")
if len(components) == 2 {
docker0Addr = components[1]
docker0Hostname = "host.docker.internal"
}
}
}
templateVars := composeYAMLVars{
Name: app.Name,
Plugin: "ddev",
AppType: app.Type,
MailhogPort: appports.GetPort("mailhog"),
DBAPort: appports.GetPort("dba"),
DBPort: appports.GetPort("db"),
DdevGenerated: DdevFileSignature,
ExtraHost: docker0Hostname + `:` + docker0Addr,
ComposeVersion: version.DockerComposeFileFormatVersion,
OmitDBA: util.ArrayContainsString(app.OmitContainers, "dba"),
OmitSSHAgent: util.ArrayContainsString(app.OmitContainers, "ddev-ssh-agent"),
WebcacheEnabled: app.WebcacheEnabled,
IsWindowsFS: runtime.GOOS == "windows",
MountType: "bind",
WebMount: "../",
}
if templateVars.WebcacheEnabled {
templateVars.MountType = "volume"
templateVars.WebMount = "webcachevol"
}
err = templ.Execute(&doc, templateVars)
return doc.String(), err
}
// prompt for a project name.
func (app *DdevApp) promptForName() error {
provider, err := app.GetProvider()
if err != nil {
return err
}
if app.Name == "" {
dir, err := os.Getwd()
// if working directory name is invalid for hostnames, we shouldn't suggest it
if err == nil && hostRegex.MatchString(filepath.Base(dir)) {
app.Name = filepath.Base(dir)
}
}
app.Name = util.Prompt("Project name", app.Name)
return provider.ValidateField("Name", app.Name)
}
// AvailableDocrootLocations returns an of default docroot locations to look for.
func AvailableDocrootLocations() []string {
return []string{
"web/public",
"web",
"docroot",
"htdocs",
"_www",
"public",
}
}
// DiscoverDefaultDocroot returns the default docroot directory.
func DiscoverDefaultDocroot(app *DdevApp) string {
// Provide use the app.Docroot as the default docroot option.
var defaultDocroot = app.Docroot
if defaultDocroot == "" {
for _, docroot := range AvailableDocrootLocations() {
if _, err := os.Stat(filepath.Join(app.AppRoot, docroot)); err != nil {
continue
}
if fileutil.FileExists(filepath.Join(app.AppRoot, docroot, "index.php")) {
defaultDocroot = docroot
break
}
}
}
return defaultDocroot
}
// Determine the document root.
func (app *DdevApp) docrootPrompt() error {
provider, err := app.GetProvider()
if err != nil {
return err
}
// Determine the document root.
util.Warning("\nThe docroot is the directory from which your site is served.\nThis is a relative path from your project root at %s", app.AppRoot)
output.UserOut.Println("You may leave this value blank if your site files are in the project root")
var docrootPrompt = "Docroot Location"
var defaultDocroot = DiscoverDefaultDocroot(app)
// If there is a default docroot, display it in the prompt.
if defaultDocroot != "" {
docrootPrompt = fmt.Sprintf("%s (%s)", docrootPrompt, defaultDocroot)
} else if cd, _ := os.Getwd(); cd == filepath.Join(app.AppRoot, defaultDocroot) {
// Preserve the case where the docroot is the current directory
docrootPrompt = fmt.Sprintf("%s (current directory)", docrootPrompt)
} else {
// Explicitly state 'project root' when in a subdirectory
docrootPrompt = fmt.Sprintf("%s (project root)", docrootPrompt)
}
fmt.Print(docrootPrompt + ": ")
app.Docroot = util.GetInput(defaultDocroot)
// Ensure the docroot exists. If it doesn't, prompt the user to verify they entered it correctly.
fullPath := filepath.Join(app.AppRoot, app.Docroot)
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
util.Warning("Warning: the provided docroot at %s does not currently exist.", fullPath)
// Ask the user for permission to create the docroot
if !util.Confirm(fmt.Sprintf("Create docroot at %s?", fullPath)) {
return fmt.Errorf("docroot must exist to continue configuration")
}
if err = os.MkdirAll(fullPath, 0755); err != nil {
return fmt.Errorf("unable to create docroot: %v", err)
}
util.Success("Created docroot at %s.", fullPath)
}
return provider.ValidateField("Docroot", app.Docroot)
}
// ConfigExists determines if a ddev config file exists for this application.
func (app *DdevApp) ConfigExists() bool {
if _, err := os.Stat(app.ConfigPath); os.IsNotExist(err) {
return false
}
return true
}
// AppTypePrompt handles the Type workflow.
func (app *DdevApp) AppTypePrompt() error {
provider, err := app.GetProvider()
if err != nil {
return err
}
validAppTypes := strings.Join(GetValidAppTypes(), ", ")
typePrompt := fmt.Sprintf("Project Type [%s]", validAppTypes)
// First, see if we can auto detect what kind of site it is so we can set a sane default.
detectedAppType := app.DetectAppType()
// If the detected detectedAppType is php, we'll ask them to confirm,
// otherwise go with it.
// If we found an application type just set it and inform the user.
util.Success("Found a %s codebase at %s.", detectedAppType, filepath.Join(app.AppRoot, app.Docroot))
typePrompt = fmt.Sprintf("%s (%s)", typePrompt, detectedAppType)
fmt.Printf(typePrompt + ": ")
appType := strings.ToLower(util.GetInput(detectedAppType))
for !IsValidAppType(appType) {
output.UserOut.Errorf("'%s' is not a valid project type. Allowed project types are: %s\n", appType, validAppTypes)
fmt.Printf(typePrompt + ": ")
appType = strings.ToLower(util.GetInput(appType))
}
app.Type = appType
return provider.ValidateField("Type", app.Type)
}
// PrepDdevDirectory creates a .ddev directory in the current working directory
func PrepDdevDirectory(dir string) error {
if _, err := os.Stat(dir); os.IsNotExist(err) {
log.WithFields(log.Fields{
"directory": dir,
}).Debug("Config Directory does not exist, attempting to create.")
err := os.MkdirAll(dir, 0755)
if err != nil {
return err
}
}
err := CreateGitIgnore(dir, "import.yaml", "docker-compose.yaml", "db_snapshots", "sequelpro.spf", "import-db", ".bgsync*")
if err != nil {
return fmt.Errorf("failed to create gitignore in %s: %v", dir, err)
}
return nil
}
// validateCommandYaml validates command hooks and tasks defined in hooks for config.yaml
func validateCommandYaml(source []byte) error {
validHooks := []string{
"pre-start",
"post-start",
"pre-import-db",
"post-import-db",
"pre-import-files",
"post-import-files",
}
validTasks := []string{
"exec",
"exec-host",
}
type Validate struct {
Commands map[string][]map[string]interface{} `yaml:"hooks,omitempty"`
}
val := &Validate{}
err := yaml.Unmarshal(source, val)
if err != nil {
return err
}
for command, tasks := range val.Commands {
var match bool
for _, hook := range validHooks {
if command == hook {
match = true
}
}
if !match {
return fmt.Errorf("invalid command hook %s defined in config.yaml", command)
}
for _, taskSet := range tasks {
for taskName := range taskSet {
var match bool
for _, validTask := range validTasks {
if taskName == validTask {
match = true
}
}
if !match {
return fmt.Errorf("invalid task '%s' defined for %s hook in config.yaml", taskName, command)
}
}
}
}
return nil
}