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

     1  package ddevapp
     2  
     3  import (
     4  	"embed"
     5  	"github.com/ddev/ddev/pkg/globalconfig"
     6  	"path/filepath"
     7  
     8  	"github.com/ddev/ddev/pkg/fileutil"
     9  )
    10  
    11  // The bundled assets for the project .ddev directory are in directory dotddev_assets
    12  // And the global .ddev assets are in directory global_dotddev_assets
    13  //
    14  //go:embed dotddev_assets/* dotddev_assets/commands/.gitattributes
    15  //go:embed global_dotddev_assets/* global_dotddev_assets/.gitignore global_dotddev_assets/commands/.gitattributes
    16  //go:embed app_compose_template.yaml
    17  //go:embed router_compose_template.yaml
    18  //go:embed ssh_auth_compose_template.yaml
    19  //go:embed traefik_config_template.yaml
    20  //go:embed traefik_static_config_template.yaml
    21  //go:embed traefik_global_config_template.yaml
    22  //go:embed router_Dockerfile_template
    23  //go:embed django4/*
    24  //go:embed drupal/*
    25  //go:embed magento/*
    26  //go:embed wordpress/*
    27  //go:embed typo3/*
    28  //go:embed postgres/*
    29  //go:embed healthcheck/*
    30  var bundledAssets embed.FS
    31  
    32  // PopulateExamplesCommandsHomeadditions grabs embedded assets and
    33  // installs them into the named directory
    34  // Note that running this with no appName (very common) can result in updating
    35  // a *different* projects assets. So `ddev start something` will first update the current
    36  // directory's assets (if it's a project) and then later (when called with appName) update
    37  // the actual project's assets.
    38  func PopulateExamplesCommandsHomeadditions(appName string) error {
    39  
    40  	err := fileutil.CopyEmbedAssets(bundledAssets, "global_dotddev_assets", globalconfig.GetGlobalDdevDir())
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	app, err := GetActiveApp(appName)
    46  	// If we have an error from GetActiveApp, it means we're not in a project directory
    47  	// That is not an error. It means we can not do this work, so return nil.
    48  	if err != nil {
    49  		return nil
    50  	}
    51  
    52  	err = fileutil.CopyEmbedAssets(bundledAssets, "dotddev_assets", app.GetConfigPath(""))
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	return nil
    58  }
    59  
    60  func IsBundledCustomCommand(globalCommand bool, service, command string) bool {
    61  	var baseDir string
    62  	if globalCommand {
    63  		baseDir = "global_dotddev_assets"
    64  	} else {
    65  		baseDir = "dotddev_assets"
    66  	}
    67  
    68  	_, err := bundledAssets.ReadFile(filepath.Join(baseDir, "commands", service, command))
    69  
    70  	return err == nil
    71  }
    72  
    73  func IsBundledCustomProvider(provider string) bool {
    74  	paths := []string{
    75  		filepath.Join("dotddev_assets", "providers", provider) + ".yaml",
    76  		filepath.Join("dotddev_assets", "providers", provider) + ".yaml.example",
    77  	}
    78  	for _, path := range paths {
    79  		if _, err := bundledAssets.ReadFile(path); err == nil {
    80  			return true
    81  		}
    82  	}
    83  	return false
    84  }