github.com/ddev/ddev@v1.23.2-0.20240519125000-d824ffe36ff3/pkg/ddevapp/shopware6.go (about)

     1  package ddevapp
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"github.com/ddev/ddev/pkg/util"
     7  	"os"
     8  	"path/filepath"
     9  
    10  	"github.com/ddev/ddev/pkg/archive"
    11  	"github.com/ddev/ddev/pkg/fileutil"
    12  )
    13  
    14  // isShopware6App returns true if the app is of type shopware6
    15  func isShopware6App(app *DdevApp) bool {
    16  	isShopware6, err := fileutil.FgrepStringInFile(filepath.Join(app.AppRoot, "composer.json"), `"name": "shopware/production"`)
    17  	if err == nil && isShopware6 {
    18  		return true
    19  	}
    20  	return false
    21  }
    22  
    23  // setShopware6SiteSettingsPaths sets the paths to .env.local file.
    24  func setShopware6SiteSettingsPaths(app *DdevApp) {
    25  	app.SiteSettingsPath = filepath.Join(app.AppRoot, ".env.local")
    26  }
    27  
    28  // shopware6ImportFilesAction defines the shopware6 workflow for importing user-generated files.
    29  func shopware6ImportFilesAction(app *DdevApp, uploadDir, importPath, extPath string) error {
    30  	destPath := app.calculateHostUploadDirFullPath(uploadDir)
    31  
    32  	// parent of destination dir should exist
    33  	if !fileutil.FileExists(filepath.Dir(destPath)) {
    34  		return fmt.Errorf("unable to import to %s: parent directory does not exist", destPath)
    35  	}
    36  
    37  	// parent of destination dir should be writable.
    38  	if err := os.Chmod(filepath.Dir(destPath), 0755); err != nil {
    39  		return err
    40  	}
    41  
    42  	// If the destination path exists, remove it as was warned
    43  	if fileutil.FileExists(destPath) {
    44  		if err := os.RemoveAll(destPath); err != nil {
    45  			return fmt.Errorf("failed to cleanup %s before import: %v", destPath, err)
    46  		}
    47  	}
    48  
    49  	if isTar(importPath) {
    50  		if err := archive.Untar(importPath, destPath, extPath); err != nil {
    51  			return fmt.Errorf("failed to extract provided archive: %v", err)
    52  		}
    53  
    54  		return nil
    55  	}
    56  
    57  	if isZip(importPath) {
    58  		if err := archive.Unzip(importPath, destPath, extPath); err != nil {
    59  			return fmt.Errorf("failed to extract provided archive: %v", err)
    60  		}
    61  
    62  		return nil
    63  	}
    64  
    65  	//nolint: revive
    66  	if err := fileutil.CopyDir(importPath, destPath); err != nil {
    67  		return err
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  // getShopwareUploadDirs will return the default paths.
    74  func getShopwareUploadDirs(_ *DdevApp) []string {
    75  	return []string{"media"}
    76  }
    77  
    78  // shopware6PostStartAction checks to see if the .env.local file is set up
    79  func shopware6PostStartAction(app *DdevApp) error {
    80  	if app.DisableSettingsManagement {
    81  		return nil
    82  	}
    83  	envFilePath := filepath.Join(app.AppRoot, ".env.local")
    84  	_, envText, err := ReadProjectEnvFile(envFilePath)
    85  	var envMap = map[string]string{
    86  		"DATABASE_URL": `mysql://db:db@db:3306/db`,
    87  		"APP_ENV":      "dev",
    88  		"APP_URL":      app.GetPrimaryURL(),
    89  		"MAILER_DSN":   `smtp://127.0.0.1:1025?encryption=&auth_mode=`,
    90  	}
    91  	// If the .env.local doesn't exist, create it.
    92  	switch {
    93  	case err == nil:
    94  		util.Warning("Updating %s with %v", envFilePath, envMap)
    95  		fallthrough
    96  	case errors.Is(err, os.ErrNotExist):
    97  		err := WriteProjectEnvFile(envFilePath, envMap, envText)
    98  		if err != nil {
    99  			return err
   100  		}
   101  	default:
   102  		util.Warning("error opening %s: %v", envFilePath, err)
   103  	}
   104  
   105  	return nil
   106  }