github.com/drud/ddev@v1.21.5-alpha1.0.20230226034409-94fcc4b94453/pkg/ddevapp/craftcms.go (about)

     1  package ddevapp
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/drud/ddev/pkg/archive"
     6  	"github.com/drud/ddev/pkg/fileutil"
     7  	"github.com/drud/ddev/pkg/nodeps"
     8  	"github.com/drud/ddev/pkg/util"
     9  	"github.com/pkg/errors"
    10  	"os"
    11  	"path/filepath"
    12  )
    13  
    14  // isCraftCmsApp returns true if the app is of type craftcms
    15  func isCraftCmsApp(app *DdevApp) bool {
    16  	return fileutil.FileExists(filepath.Join(app.AppRoot, app.ComposerRoot, "craft"))
    17  }
    18  
    19  // craftCmsImportFilesAction defines the workflow for importing project files.
    20  func craftCmsImportFilesAction(app *DdevApp, importPath, extPath string) error {
    21  	if app.UploadDir == "" {
    22  		return errors.Errorf("No upload_dir is set for this (craftcms) project")
    23  	}
    24  	destPath := app.GetHostUploadDirFullPath()
    25  
    26  	// parent of destination dir should exist
    27  	if !fileutil.FileExists(filepath.Dir(destPath)) {
    28  		return fmt.Errorf("unable to import to %s: parent directory does not exist", destPath)
    29  	}
    30  
    31  	// parent of destination dir should be writable.
    32  	if err := os.Chmod(filepath.Dir(destPath), 0755); err != nil {
    33  		return err
    34  	}
    35  
    36  	// If the destination path exists, remove it as was warned
    37  	if fileutil.FileExists(destPath) {
    38  		if err := os.RemoveAll(destPath); err != nil {
    39  			return fmt.Errorf("failed to cleanup %s before import: %v", destPath, err)
    40  		}
    41  	}
    42  
    43  	if isTar(importPath) {
    44  		if err := archive.Untar(importPath, destPath, extPath); err != nil {
    45  			return fmt.Errorf("failed to extract provided archive: %v", err)
    46  		}
    47  
    48  		return nil
    49  	}
    50  
    51  	if isZip(importPath) {
    52  		if err := archive.Unzip(importPath, destPath, extPath); err != nil {
    53  			return fmt.Errorf("failed to extract provided archive: %v", err)
    54  		}
    55  
    56  		return nil
    57  	}
    58  
    59  	//nolint: revive
    60  	if err := fileutil.CopyDir(importPath, destPath); err != nil {
    61  		return err
    62  	}
    63  
    64  	return nil
    65  }
    66  
    67  // Currently a placeholder, for possible future expansion
    68  func craftCmsPostConfigAction(app *DdevApp) error {
    69  	return nil
    70  }
    71  
    72  // Set up the .env file for ddev
    73  func craftCmsPostStartAction(app *DdevApp) error {
    74  	// If settings management is disabled, do nothing
    75  	if app.DisableSettingsManagement {
    76  		return nil
    77  	}
    78  
    79  	// If the .env file doesn't exist, try to create it by copying .env.example to .env
    80  	envFilePath := filepath.Join(app.AppRoot, app.ComposerRoot, ".env")
    81  	if !fileutil.FileExists(envFilePath) {
    82  		var exampleEnvFilePaths = []string{".env.example", ".env.example.dev"}
    83  		for _, envFileName := range exampleEnvFilePaths {
    84  			exampleEnvFilePath := filepath.Join(app.AppRoot, app.ComposerRoot, envFileName)
    85  			if fileutil.FileExists(exampleEnvFilePath) {
    86  				util.Success(fmt.Sprintf("Copied %s to .env", envFileName))
    87  				err := fileutil.CopyFile(exampleEnvFilePath, envFilePath)
    88  				if err != nil {
    89  					util.Error(fmt.Sprintf("Error copying %s to .env", exampleEnvFilePath))
    90  
    91  					return err
    92  				}
    93  			}
    94  		}
    95  	}
    96  	// If the .env file *still* doesn't exist, return early
    97  	if !fileutil.FileExists(envFilePath) {
    98  		return nil
    99  	}
   100  	// Read in the .env file
   101  	envMap, envText, err := ReadProjectEnvFile(envFilePath)
   102  	if err != nil && !os.IsNotExist(err) {
   103  		return fmt.Errorf("Unable to read .env file: %v", err)
   104  	}
   105  
   106  	port := "3306"
   107  	driver := "mysql"
   108  	if app.Database.Type == nodeps.Postgres {
   109  		driver = "pgsql"
   110  		port = "5432"
   111  	}
   112  
   113  	// If they have older version of .env with DB_DRIVER, DB_SERVER etc, use those
   114  	if _, ok := envMap["DB_SERVER"]; ok {
   115  		envMap = map[string]string{
   116  			"DB_DRIVER":             driver,
   117  			"DB_SERVER":             "db",
   118  			"DB_PORT":               port,
   119  			"DB_DATABASE":           "db",
   120  			"DB_USER":               "db",
   121  			"DB_PASSWORD":           "db",
   122  			"MAILHOG_SMTP_HOSTNAME": "127.0.0.1",
   123  			"MAILHOG_SMTP_PORT":     "1025",
   124  			"PRIMARY_SITE_URL":      app.GetPrimaryURL(),
   125  		}
   126  	} else {
   127  		// Otherwise use the current CRAFT_DB_SERVER etc.
   128  		envMap = map[string]string{
   129  			"CRAFT_DB_DRIVER":       driver,
   130  			"CRAFT_DB_SERVER":       "db",
   131  			"CRAFT_DB_PORT":         port,
   132  			"CRAFT_DB_DATABASE":     "db",
   133  			"CRAFT_DB_USER":         "db",
   134  			"CRAFT_DB_PASSWORD":     "db",
   135  			"MAILHOG_SMTP_HOSTNAME": "127.0.0.1",
   136  			"MAILHOG_SMTP_PORT":     "1025",
   137  			"PRIMARY_SITE_URL":      app.GetPrimaryURL(),
   138  		}
   139  	}
   140  
   141  	err = WriteProjectEnvFile(envFilePath, envMap, envText)
   142  	if err != nil {
   143  		return err
   144  	}
   145  
   146  	// If composer.json.default exists, rename it to composer.json
   147  	composerDefaultFilePath := filepath.Join(app.AppRoot, app.ComposerRoot, "composer.json.default")
   148  	if fileutil.FileExists(composerDefaultFilePath) {
   149  		composerFilePath := filepath.Join(app.AppRoot, app.ComposerRoot, "composer.json")
   150  		util.Warning("Renaming composer.json.default to composer.json")
   151  		err = os.Rename(composerDefaultFilePath, composerFilePath)
   152  		if err != nil {
   153  			util.Error("Error renaming composer.json.default to composer.json")
   154  
   155  			return err
   156  		}
   157  	}
   158  
   159  	return nil
   160  }