-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
module_deprecations.go
144 lines (130 loc) · 5.85 KB
/
module_deprecations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package configs
import (
"fmt"
"github.com/hashicorp/hcl/v2"
)
type WorkspaceDeprecationInfo struct {
ModuleDeprecationInfos []*ModuleDeprecationInfo
}
type ModuleDeprecationInfo struct {
SourceName string
RegistryDeprecation *RegistryModuleDeprecation
ExternalDependencies []*ModuleDeprecationInfo
}
type RegistryModuleDeprecation struct {
Version string
Link string
Message string
}
type ModuleDeprecationDiagnosticExtra struct {
MessageCode string `json:"message_code"`
Deprecations []*ModuleDeprecationDiagnosticExtraDeprecationItem `json:"deprecations"`
}
type ModuleDeprecationDiagnosticExtraDeprecationItem struct {
Version string `json:"version"`
SourceName string `json:"source_name"`
DeprecationMessage string `json:"deprecation_message"`
Link string `json:"link"`
}
func (i *WorkspaceDeprecationInfo) HasDeprecations() bool {
if i == nil || i.ModuleDeprecationInfos == nil {
return false
}
for _, deprecationInfo := range i.ModuleDeprecationInfos {
if deprecationInfo != nil && deprecationInfo.hasDeprecations() {
return true
}
}
return false
}
func (i *ModuleDeprecationInfo) hasDeprecations() bool {
if i.RegistryDeprecation != nil {
return true
}
for _, dependencyDeprecationInfo := range i.ExternalDependencies {
if dependencyDeprecationInfo != nil && dependencyDeprecationInfo.hasDeprecations() {
return true
}
}
return false
}
// Deprecation info is placed as an string in the Diagnostic Detail for console view,
// as well as placed in the Diagnostic Extra for parsing for the SRO view in HCP Terraform
func (i *WorkspaceDeprecationInfo) BuildDeprecationWarning() *hcl.Diagnostic {
modDeprecationStrings := []string{}
var deprecationList []*ModuleDeprecationDiagnosticExtraDeprecationItem
for _, modDeprecationInfo := range i.ModuleDeprecationInfos {
if modDeprecationInfo != nil && modDeprecationInfo.RegistryDeprecation != nil {
modDeprecation := fmt.Sprintf("\x1b[1mVersion %s of %s\x1b[0m", modDeprecationInfo.RegistryDeprecation.Version, modDeprecationInfo.SourceName)
// Link and Message are optional fields, if unset they are an empty string by default
if modDeprecationInfo.RegistryDeprecation.Message != "" {
modDeprecation = modDeprecation + fmt.Sprintf("\n\n%s", modDeprecationInfo.RegistryDeprecation.Message)
}
if modDeprecationInfo.RegistryDeprecation.Link != "" {
modDeprecation = modDeprecation + fmt.Sprintf("\n\nLink for more information: %s", modDeprecationInfo.RegistryDeprecation.Link)
}
deprecationList = append(deprecationList, &ModuleDeprecationDiagnosticExtraDeprecationItem{
Version: modDeprecationInfo.RegistryDeprecation.Version,
SourceName: modDeprecationInfo.SourceName,
DeprecationMessage: modDeprecationInfo.RegistryDeprecation.Message,
Link: modDeprecationInfo.RegistryDeprecation.Link,
})
modDeprecationStrings = append(modDeprecationStrings, modDeprecation)
}
deprecationStrings, deprecationStructs := buildChildModuleDeprecations(modDeprecationInfo.ExternalDependencies, []string{modDeprecationInfo.SourceName})
deprecationList = append(deprecationList, deprecationStructs...)
modDeprecationStrings = append(modDeprecationStrings, deprecationStrings...)
}
deprecationsMessage := ""
for _, deprecationString := range modDeprecationStrings {
deprecationsMessage += deprecationString + "\n\n"
}
return &hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: "Deprecated modules found, consider installing updated versions. The following are affected:",
Detail: deprecationsMessage,
Extra: &ModuleDeprecationDiagnosticExtra{
MessageCode: "module_deprecation_warning",
Deprecations: deprecationList,
},
}
}
func buildChildModuleDeprecations(modDeprecations []*ModuleDeprecationInfo, parentMods []string) ([]string, []*ModuleDeprecationDiagnosticExtraDeprecationItem) {
modDeprecationStrings := []string{}
var deprecationList []*ModuleDeprecationDiagnosticExtraDeprecationItem
for _, deprecation := range modDeprecations {
if deprecation.RegistryDeprecation != nil {
// mdTODO: Add highlighting here, look up other examples where it's present
modDeprecation := fmt.Sprintf("\x1b[1mVersion %s of %s %s\x1b[0m", deprecation.RegistryDeprecation.Version, deprecation.SourceName, buildModHierarchy(parentMods, deprecation.SourceName))
// Link and Message are optional fields, if unset they are an empty string by default
if deprecation.RegistryDeprecation.Message != "" {
modDeprecation = modDeprecation + fmt.Sprintf("\n\n%s", deprecation.RegistryDeprecation.Message)
}
if deprecation.RegistryDeprecation.Link != "" {
modDeprecation = modDeprecation + fmt.Sprintf("\n\nLink for more information: %s", deprecation.RegistryDeprecation.Link)
}
modDeprecationStrings = append(modDeprecationStrings, modDeprecation)
}
deprecationList = append(deprecationList, &ModuleDeprecationDiagnosticExtraDeprecationItem{
Version: deprecation.RegistryDeprecation.Version,
SourceName: deprecation.SourceName,
DeprecationMessage: deprecation.RegistryDeprecation.Message,
Link: deprecation.RegistryDeprecation.Link,
})
newParentMods := append(parentMods, deprecation.SourceName)
deprecationStrings, deprecationStructs := buildChildModuleDeprecations(deprecation.ExternalDependencies, newParentMods)
modDeprecationStrings = append(modDeprecationStrings, deprecationStrings...)
deprecationList = append(deprecationList, deprecationStructs...)
}
return modDeprecationStrings, deprecationList
}
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)
}