github.com/drud/ddev@v1.21.5-alpha1.0.20230226034409-94fcc4b94453/pkg/ddevapp/php.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/pkg/errors"
     8  	"os"
     9  	"path/filepath"
    10  )
    11  
    12  func phpPostStartAction(app *DdevApp) error {
    13  	if !app.DisableSettingsManagement {
    14  		if _, err := app.CreateSettingsFile(); err != nil {
    15  			return fmt.Errorf("failed to write settings file %s: %v", app.SiteDdevSettingsFile, err)
    16  		}
    17  	}
    18  	return nil
    19  }
    20  
    21  // getPHPUploadDir will return a custom upload dir if defined
    22  func getPHPUploadDir(app *DdevApp) string {
    23  	return app.UploadDir
    24  }
    25  
    26  // phpImportFilesAction defines the workflow for importing project files.
    27  func phpImportFilesAction(app *DdevApp, importPath, extPath string) error {
    28  	if app.UploadDir == "" {
    29  		return errors.Errorf("No upload_dir is set for this (php-generic) project")
    30  	}
    31  	destPath := app.GetHostUploadDirFullPath()
    32  
    33  	// parent of destination dir should exist
    34  	if !fileutil.FileExists(filepath.Dir(destPath)) {
    35  		return fmt.Errorf("unable to import to %s: parent directory does not exist", destPath)
    36  	}
    37  
    38  	// parent of destination dir should be writable.
    39  	if err := os.Chmod(filepath.Dir(destPath), 0755); err != nil {
    40  		return err
    41  	}
    42  
    43  	// If the destination path exists, remove it as was warned
    44  	if fileutil.FileExists(destPath) {
    45  		if err := os.RemoveAll(destPath); err != nil {
    46  			return fmt.Errorf("failed to cleanup %s before import: %v", destPath, err)
    47  		}
    48  	}
    49  
    50  	if isTar(importPath) {
    51  		if err := archive.Untar(importPath, destPath, extPath); err != nil {
    52  			return fmt.Errorf("failed to extract provided archive: %v", err)
    53  		}
    54  
    55  		return nil
    56  	}
    57  
    58  	if isZip(importPath) {
    59  		if err := archive.Unzip(importPath, destPath, extPath); err != nil {
    60  			return fmt.Errorf("failed to extract provided archive: %v", err)
    61  		}
    62  
    63  		return nil
    64  	}
    65  
    66  	//nolint: revive
    67  	if err := fileutil.CopyDir(importPath, destPath); err != nil {
    68  		return err
    69  	}
    70  
    71  	return nil
    72  }