Skip to content

Commit

Permalink
refactor module deprecation structs and function to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
Maed223 committed Apr 19, 2024
1 parent 3d5db1a commit db0f78d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 51 deletions.
51 changes: 0 additions & 51 deletions internal/configs/config_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,57 +269,6 @@ func rebaseChildModule(cfg *Config, root *Config) {
}

// refactor this into it's own file, doesn't make sense to place this here
type WorkspaceDeprecations struct {
ModuleDeprecationInfos []*ModuleDeprecationInfo
}

type ModuleDeprecationInfo struct {
SourceName string
RegistryDeprecation *RegistryModuleDeprecation
ExternalDependencies []*ModuleDeprecationInfo
}

type RegistryModuleDeprecation struct {
Version string
ExternalLink string
}

func (i *WorkspaceDeprecations) BuildDeprecationWarningString() string {
modDeprecationStrings := []string{}
for _, modDeprecationInfo := range i.ModuleDeprecationInfos {
if modDeprecationInfo.RegistryDeprecation != nil {
modDeprecationStrings = append(modDeprecationStrings, fmt.Sprintf("Version %s of \"%s\" \nTo learn more visit: %s\n", modDeprecationInfo.RegistryDeprecation.Version, modDeprecationInfo.SourceName, modDeprecationInfo.RegistryDeprecation.ExternalLink))
}
modDeprecationStrings = append(modDeprecationStrings, buildChildDeprecationWarnings(modDeprecationInfo.ExternalDependencies, []string{modDeprecationInfo.SourceName})...)
}
deprecationsMessage := ""
for _, deprecationString := range modDeprecationStrings {
deprecationsMessage += deprecationString + "\n"
}

return deprecationsMessage
}

func buildChildDeprecationWarnings(modDeprecations []*ModuleDeprecationInfo, parentMods []string) []string {
modDeprecationStrings := []string{}
for _, deprecation := range modDeprecations {
if deprecation.RegistryDeprecation != nil {
modDeprecationStrings = append(modDeprecationStrings, fmt.Sprintf("Version %s of \"%s\" %s \nTo learn more visit: %s\n", deprecation.RegistryDeprecation.Version, deprecation.SourceName, buildModHierarchy(parentMods, deprecation.SourceName), deprecation.RegistryDeprecation.ExternalLink))
}
newParentMods := append(parentMods, deprecation.SourceName)
modDeprecationStrings = append(modDeprecationStrings, buildChildDeprecationWarnings(deprecation.ExternalDependencies, newParentMods)...)
}
return modDeprecationStrings
}

func buildModHierarchy(parentMods []string, modName string) string {
heirarchy := ""
for _, parent := range parentMods {
heirarchy += fmt.Sprintf("%s -> ", parent)
}
heirarchy += modName
return fmt.Sprintf("(Root: %s)", heirarchy)
}

// A ModuleWalker knows how to find and load a child module given details about
// the module to be loaded and a reference to its partially-loaded parent
Expand Down
55 changes: 55 additions & 0 deletions internal/configs/module_deprecations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package configs

import "fmt"

type WorkspaceDeprecations struct {
ModuleDeprecationInfos []*ModuleDeprecationInfo
}

type ModuleDeprecationInfo struct {
SourceName string
RegistryDeprecation *RegistryModuleDeprecation
ExternalDependencies []*ModuleDeprecationInfo
}

type RegistryModuleDeprecation struct {
Version string
ExternalLink string
}

func (i *WorkspaceDeprecations) BuildDeprecationWarningString() string {
modDeprecationStrings := []string{}
for _, modDeprecationInfo := range i.ModuleDeprecationInfos {
if modDeprecationInfo.RegistryDeprecation != nil {
modDeprecationStrings = append(modDeprecationStrings, fmt.Sprintf("Version %s of \"%s\" \nTo learn more visit: %s\n", modDeprecationInfo.RegistryDeprecation.Version, modDeprecationInfo.SourceName, modDeprecationInfo.RegistryDeprecation.ExternalLink))
}
modDeprecationStrings = append(modDeprecationStrings, buildChildDeprecationWarnings(modDeprecationInfo.ExternalDependencies, []string{modDeprecationInfo.SourceName})...)
}
deprecationsMessage := ""
for _, deprecationString := range modDeprecationStrings {
deprecationsMessage += deprecationString + "\n"
}

return deprecationsMessage
}

func buildChildDeprecationWarnings(modDeprecations []*ModuleDeprecationInfo, parentMods []string) []string {
modDeprecationStrings := []string{}
for _, deprecation := range modDeprecations {
if deprecation.RegistryDeprecation != nil {
modDeprecationStrings = append(modDeprecationStrings, fmt.Sprintf("Version %s of \"%s\" %s \nTo learn more visit: %s\n", deprecation.RegistryDeprecation.Version, deprecation.SourceName, buildModHierarchy(parentMods, deprecation.SourceName), deprecation.RegistryDeprecation.ExternalLink))
}
newParentMods := append(parentMods, deprecation.SourceName)
modDeprecationStrings = append(modDeprecationStrings, buildChildDeprecationWarnings(deprecation.ExternalDependencies, newParentMods)...)
}
return modDeprecationStrings
}

func buildModHierarchy(parentMods []string, modName string) string {
heirarchy := ""
for _, parent := range parentMods {
heirarchy += fmt.Sprintf("%s -> ", parent)
}
heirarchy += modName
return fmt.Sprintf("(Root: %s)", heirarchy)
}

0 comments on commit db0f78d

Please sign in to comment.