GetActiveAppRoot returns the fully rooted directory of the active app, or an error
(siteName string)
| 3625 | |
| 3626 | // GetActiveAppRoot returns the fully rooted directory of the active app, or an error |
| 3627 | func GetActiveAppRoot(siteName string) (string, error) { |
| 3628 | var siteDir string |
| 3629 | var err error |
| 3630 | |
| 3631 | if siteName == "" { |
| 3632 | siteDir, err = os.Getwd() |
| 3633 | if err != nil { |
| 3634 | return "", fmt.Errorf("error determining the current directory: %s", err) |
| 3635 | } |
| 3636 | _, err = CheckForConf(siteDir) |
| 3637 | if err != nil { |
| 3638 | return "", fmt.Errorf("could not find a project in %s. Have you run 'ddev config'? Please specify a project name or change directories: %s", siteDir, err) |
| 3639 | } |
| 3640 | // Handle the case where it's registered globally but stopped |
| 3641 | } else if p := globalconfig.GetProject(siteName); p != nil { |
| 3642 | return p.AppRoot, nil |
| 3643 | // Or find it by looking at Docker containers |
| 3644 | } else { |
| 3645 | var ok bool |
| 3646 | |
| 3647 | labels := map[string]string{ |
| 3648 | "com.ddev.site-name": siteName, |
| 3649 | "com.docker.compose.service": "web", |
| 3650 | "com.docker.compose.oneoff": "False", |
| 3651 | } |
| 3652 | |
| 3653 | webContainer, err := dockerutil.FindContainerByLabels(labels) |
| 3654 | if err != nil { |
| 3655 | return "", err |
| 3656 | } |
| 3657 | if webContainer == nil { |
| 3658 | return "", fmt.Errorf("could not find a project named '%s'. Run 'ddev list' to see currently active projects", siteName) |
| 3659 | } |
| 3660 | |
| 3661 | siteDir, ok = webContainer.Labels["com.ddev.approot"] |
| 3662 | if !ok { |
| 3663 | return "", fmt.Errorf("could not determine the location of %s from container: %s", siteName, dockerutil.ContainerName(webContainer)) |
| 3664 | } |
| 3665 | } |
| 3666 | appRoot, err := CheckForConf(siteDir) |
| 3667 | if err != nil { |
| 3668 | return siteDir, err |
| 3669 | } |
| 3670 | |
| 3671 | return appRoot, nil |
| 3672 | } |
| 3673 | |
| 3674 | // GetActiveApp returns the active App based on the current working directory or running siteName provided. |
| 3675 | // To use the current working directory, siteName should be "" |