Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Use project_list.yaml as source of truth, fixes #5918 #5926

Merged
merged 7 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/content/users/usage/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ Files beginning with `.` are hidden because they shouldn’t be fiddled with; mo
There’s only one global `.ddev` directory, which lives in your home directory: `~/.ddev` (`$HOME/.ddev`).

`global_config.yaml`
: This YAML file defines your global configuration, which consists of various [config settings](../configuration/config.md) along with an important `project_info` key that lets DDEV keep track of the projects you’ve added.
: This YAML file defines your global configuration, which consists of various [config settings](../configuration/config.md).

`project_list.yaml`
: This YAML file defines your project list that lets DDEV keep track of the projects you’ve added.

`bin` directory
: This is where DDEV stores private executable binaries it needs, like `mutagen` and `docker-compose`.
Expand Down
4 changes: 0 additions & 4 deletions pkg/ddevapp/ddevapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2517,10 +2517,6 @@ func (app *DdevApp) Stop(removeData bool, createSnapshot bool) error {
return fmt.Errorf("failed to remove hosts entries: %v", err)
}
app.RemoveGlobalProjectInfo()
err = globalconfig.WriteGlobalConfig(globalconfig.DdevGlobalConfig)
if err != nil {
util.Warning("Could not WriteGlobalConfig: %v", err)
}

vols := []string{app.GetMariaDBVolumeName(), app.GetPostgresVolumeName(), GetMutagenVolumeName(app)}
if globalconfig.DdevGlobalConfig.NoBindMounts {
Expand Down
61 changes: 28 additions & 33 deletions pkg/globalconfig/global_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"os/exec"
"path/filepath"
"reflect"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -75,10 +74,10 @@ type GlobalConfig struct {
WSL2NoWindowsHostsMgt bool `yaml:"wsl2_no_windows_hosts_mgt"`
WebEnvironment []string `yaml:"web_environment"`
XdebugIDELocation string `yaml:"xdebug_ide_location"`
ProjectList map[string]*ProjectInfo `yaml:"project_info"`
ProjectList map[string]*ProjectInfo `yaml:"project_info,omitempty"`
}

// New() returns a default GlobalConfig
// New returns a default GlobalConfig
func New() GlobalConfig {

cfg := GlobalConfig{
Expand All @@ -93,7 +92,6 @@ func New() GlobalConfig {
NoBindMounts: nodeps.NoBindMountsDefault,
Router: types.RouterTypeDefault,
MkcertCARoot: readCAROOT(),
ProjectList: make(map[string]*ProjectInfo),
TraefikMonitorPort: nodeps.TraefikMonitorPortDefault,
ProjectTldGlobal: nodeps.DdevDefaultTLD,
}
Expand Down Expand Up @@ -475,14 +473,26 @@ func ReadProjectList() error {
return nil
}
if os.IsNotExist(err) {
// write an empty file
err := os.WriteFile(GetProjectListPath(), make([]byte, 0), 0644)
if err != nil {
// If someone upgrades from an earlier version, the global config may hold the project list.
if DdevGlobalConfig.ProjectList != nil && len(DdevGlobalConfig.ProjectList) > 0 {
DdevProjectList = DdevGlobalConfig.ProjectList
err := WriteProjectList(DdevProjectList)
if err != nil {
return err
}
// Clean up deprecated project list
DdevGlobalConfig.ProjectList = nil
err = WriteGlobalConfig(DdevGlobalConfig)
// Whether there's an error or nil here we want to return
return err
}
} else {
return err

// Write an empty file - we have no known projects
err = os.WriteFile(GetProjectListPath(), make([]byte, 0), 0644)
}

// Whether there's an error or nil here we want to return
return err
Comment on lines +494 to +495
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We either created a new file here, or we had an unexpected error. Either way there's no projects to read so we can skip the "read the projects list from the file" logic below.

}

source, err := os.ReadFile(globalProjectsFile)
Expand All @@ -496,32 +506,20 @@ func ReadProjectList() error {
return err
}

// For backwards compatability we're keeping the project_list in global config
// in sync with the list in project_list.yaml. If someone upgrades from an earlier
// version the global config will have correct content that isn't in projects.yaml.
// For now, treat global config as the source of truth when the two lists differ.
if !reflect.DeepEqual(DdevGlobalConfig.ProjectList, DdevProjectList) {
DdevProjectList = DdevGlobalConfig.ProjectList
err := WriteProjectList(DdevProjectList)
if err != nil {
return err
}
// Clean up the deprecated DdevGlobalConfig.ProjectList if it not nil,
// but only if the new one DdevProjectList is not nil (for safe migration).
if DdevGlobalConfig.ProjectList != nil && DdevProjectList != nil {
DdevGlobalConfig.ProjectList = nil
err := WriteGlobalConfig(DdevGlobalConfig)
// Whether there's an error or nil here we want to return
return err
}

return nil
}

// WriteProjectList writes the global projects list into ~/.ddev.
func WriteProjectList(projects map[string]*ProjectInfo) error {
// Write to global config for backwards compatability.
// This allows devs to downgrade to an earlier version without
// worrying about copying project info into their global config file.
DdevGlobalConfig.ProjectList = projects
err := WriteGlobalConfig(DdevGlobalConfig)
if err != nil {
return err
}

// Prepare projects file content
projectsBytes, err := yaml.Marshal(projects)
if err != nil {
Expand Down Expand Up @@ -693,12 +691,9 @@ func RemoveProjectInfo(projectName string) error {
_, ok := DdevProjectList[projectName]
if ok {
delete(DdevProjectList, projectName)
err := WriteProjectList(DdevProjectList)
if err != nil {
return err
}
}
return nil
err := WriteProjectList(DdevProjectList)
return err
}

// GetGlobalProjectList returns the global project list map
Expand Down
8 changes: 0 additions & 8 deletions pkg/globalconfig/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,6 @@
},
"uniqueItems": true
},
"project_info": {
"description": "List of all projects DDEV currently knows about. DDEV manages this list when using `config` and `delete` commands.",
"type": "array",
"items": {
"type": "array"
},
"uniqueItems": true
},
"performance_mode": {
"description": "Defines the performance optimization mode to be used. Currently Mutagen asynchronous caching and NFS are supported. Mutagen is enabled by default on Mac and Windows.",
"type": "string",
Expand Down