Skip to content

Commit ce53911

Browse files
committed
Remove direct colorizing in configs package, added support for implicit colorizing of diags details & abiding to -no-color
1 parent 77bdd76 commit ce53911

File tree

4 files changed

+25
-27
lines changed

4 files changed

+25
-27
lines changed

internal/command/format/diagnostic.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ func DiagnosticFromJSON(diag *viewsjson.Diagnostic, color *colorstring.Colorize,
8787
if !strings.HasPrefix(line, " ") {
8888
line = wordwrap.WrapString(line, uint(paraWidth))
8989
}
90-
fmt.Fprintf(&buf, "%s\n", line)
90+
fmt.Fprintf(&buf, "%s\n", color.Color(line))
9191
}
9292
} else {
93-
fmt.Fprintf(&buf, "%s\n", diag.Detail)
93+
fmt.Fprintf(&buf, "%s\n", color.Color(diag.Detail))
9494
}
9595
}
9696

@@ -160,10 +160,10 @@ func DiagnosticPlainFromJSON(diag *viewsjson.Diagnostic, width int) string {
160160
if !strings.HasPrefix(line, " ") {
161161
line = wordwrap.WrapString(line, uint(width-1))
162162
}
163-
fmt.Fprintf(&buf, "%s\n", line)
163+
fmt.Fprintf(&buf, "%s\n", disabledColorize.Color(line))
164164
}
165165
} else {
166-
fmt.Fprintf(&buf, "%s\n", diag.Detail)
166+
fmt.Fprintf(&buf, "%s\n", disabledColorize.Color(diag.Detail))
167167
}
168168
}
169169

internal/configs/module_version_deprecations.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"strings"
99

1010
"github.com/hashicorp/hcl/v2"
11-
"github.com/mitchellh/colorstring"
1211
)
1312

1413
type DirectoryDeprecationInfo struct {
@@ -69,16 +68,10 @@ func (i *ModuleVersionDeprecationInfo) hasDeprecations() bool {
6968
// as well as placed in the Diagnostic Extra for parsing for the SRO view in HCP Terraform
7069
func (i *DirectoryDeprecationInfo) BuildDeprecationWarning() *hcl.Diagnostic {
7170
modDeprecations := []string{}
72-
color := colorstring.Colorize{
73-
Colors: colorstring.DefaultColors,
74-
Disable: false,
75-
Reset: true,
76-
}
7771
deprecationList := make([]*ModuleVersionDeprecationDiagnosticExtraDeprecationItem, 0, len(i.ModuleVersionDeprecationInfos))
7872
for _, modDeprecationInfo := range i.ModuleVersionDeprecationInfos {
7973
if modDeprecationInfo != nil && modDeprecationInfo.RegistryDeprecation != nil {
80-
msg := color.Color("[reset][bold]Version %s of %s[reset]")
81-
modDeprecation := fmt.Sprintf(msg, modDeprecationInfo.RegistryDeprecation.Version, modDeprecationInfo.SourceName)
74+
modDeprecation := fmt.Sprintf("[reset][bold]Version %s of %s[reset]", modDeprecationInfo.RegistryDeprecation.Version, modDeprecationInfo.SourceName)
8275
// Link and Message are optional fields, if unset they are an empty string by default
8376
if modDeprecationInfo.RegistryDeprecation.Message != "" {
8477
modDeprecation = modDeprecation + fmt.Sprintf("\n\n%s", modDeprecationInfo.RegistryDeprecation.Message)
@@ -112,17 +105,11 @@ func (i *DirectoryDeprecationInfo) BuildDeprecationWarning() *hcl.Diagnostic {
112105
}
113106

114107
func buildChildModuleDeprecations(modDeprecations []*ModuleVersionDeprecationInfo, parentMods []string) ([]string, []*ModuleVersionDeprecationDiagnosticExtraDeprecationItem) {
115-
color := colorstring.Colorize{
116-
Colors: colorstring.DefaultColors,
117-
Disable: false,
118-
Reset: true,
119-
}
120108
modDeprecationStrings := []string{}
121109
var deprecationList []*ModuleVersionDeprecationDiagnosticExtraDeprecationItem
122110
for _, deprecation := range modDeprecations {
123111
if deprecation.RegistryDeprecation != nil {
124-
msg := color.Color("[reset][bold]Version %s of %s %s[reset]")
125-
modDeprecation := fmt.Sprintf(msg, deprecation.RegistryDeprecation.Version, deprecation.SourceName, buildModHierarchy(parentMods, deprecation.SourceName))
112+
modDeprecation := fmt.Sprintf("[reset][bold]Version %s of %s %s[reset]", deprecation.RegistryDeprecation.Version, deprecation.SourceName, buildModHierarchy(parentMods, deprecation.SourceName))
126113
// Link and Message are optional fields, if unset they are an empty string by default
127114
if deprecation.RegistryDeprecation.Message != "" {
128115
modDeprecation = modDeprecation + fmt.Sprintf("\n\n%s", deprecation.RegistryDeprecation.Message)

internal/configs/module_version_deprecations_test.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"testing"
99

1010
"github.com/hashicorp/hcl/v2"
11-
"github.com/mitchellh/colorstring"
1211
)
1312

1413
func TestBuildDeprecationWarning(t *testing.T) {
@@ -70,14 +69,8 @@ func TestBuildDeprecationWarning(t *testing.T) {
7069
},
7170
}
7271

73-
color := colorstring.Colorize{
74-
Colors: colorstring.DefaultColors,
75-
Disable: false,
76-
Reset: true,
77-
}
78-
7972
detailStringArray := []string{
80-
color.Color("[reset][bold]Version 1.0.0 of test1[reset]"), "Deprecation message for module test1", "Link for more information: https://test1.com", color.Color("[reset][bold]Version 1.0.0 of test1-external-dependency (Root: test1 -> test1-external-dependency)[reset]"), "Deprecation message for module test1-external-dependency", "Link for more information: https://test1-external-dependency.com", color.Color("[reset][bold]Version 1.0.0 of test2[reset]"), "Deprecation message for module test2", "Link for more information: https://test2.com", color.Color("[reset][bold]Version 1.0.0 of test2-external-dependency (Root: test2 -> test2-external-dependency)[reset]"), "Deprecation message for module test2-external-dependency", "Link for more information: https://test2-external-dependency.com", color.Color("[reset][bold]Version 1.0.0 of test2b-external-dependency (Root: test2 -> test2b-external-dependency)[reset]"), "Deprecation message for module test2b-external-dependency", "Link for more information: https://test2b-external-dependency.com", color.Color("[reset][bold]Version 1.0.0 of test3[reset]"), "Deprecation message for module test3", "Link for more information: https://test3.com",
73+
"[reset][bold]Version 1.0.0 of test1[reset]", "Deprecation message for module test1", "Link for more information: https://test1.com", "[reset][bold]Version 1.0.0 of test1-external-dependency (Root: test1 -> test1-external-dependency)[reset]", "Deprecation message for module test1-external-dependency", "Link for more information: https://test1-external-dependency.com", "[reset][bold]Version 1.0.0 of test2[reset]", "Deprecation message for module test2", "Link for more information: https://test2.com", "[reset][bold]Version 1.0.0 of test2-external-dependency (Root: test2 -> test2-external-dependency)[reset]", "Deprecation message for module test2-external-dependency", "Link for more information: https://test2-external-dependency.com", "[reset][bold]Version 1.0.0 of test2b-external-dependency (Root: test2 -> test2b-external-dependency)[reset]", "Deprecation message for module test2b-external-dependency", "Link for more information: https://test2b-external-dependency.com", "[reset][bold]Version 1.0.0 of test3[reset]", "Deprecation message for module test3", "Link for more information: https://test3.com",
8174
}
8275
diagWant := &hcl.Diagnostic{
8376
Severity: hcl.DiagWarning,

internal/initwd/module_install.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@ func NewModuleInstaller(modsDir string, loader *configload.Loader, reg *registry
5959
}
6060
}
6161

62+
func injectMockDeprecations(modules *response.ModuleVersions) {
63+
if modules == nil || modules.Modules == nil {
64+
log.Println("modules or modules.Modules is nil")
65+
return // Exit the function to avoid the panic
66+
}
67+
for _, module := range modules.Modules {
68+
for _, version := range module.Versions {
69+
// Inject a mock deprecation into each version
70+
version.Deprecation = &response.Deprecation{
71+
Reason: "Mock deprecation message: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
72+
Link: "https://example.com/mock-deprecation",
73+
}
74+
}
75+
}
76+
}
77+
6278
// InstallModules analyses the root module in the given directory and installs
6379
// all of its direct and transitive dependencies into the given modules
6480
// directory, which must already exist.
@@ -269,6 +285,7 @@ func (i *ModuleInstaller) moduleInstallWalker(ctx context.Context, manifest mods
269285

270286
regsrcAddr := regsrc.ModuleFromRegistryPackageAddr(addr.Package)
271287
resp, err := regClient.ModuleVersions(ctx, regsrcAddr)
288+
injectMockDeprecations(resp)
272289
if err != nil {
273290
log.Printf("[DEBUG] Deprecation for %s could not be checked: call to registry failed: %v", addr.Package.Namespace, err)
274291

@@ -477,6 +494,7 @@ func (i *ModuleInstaller) installRegistryModule(ctx context.Context, req *config
477494
var err error
478495
log.Printf("[DEBUG] %s listing available versions of %s at %s", key, addr, hostname)
479496
resp, err = reg.ModuleVersions(ctx, regsrcAddr)
497+
injectMockDeprecations(resp)
480498
if err != nil {
481499
if registry.IsModuleNotFound(err) {
482500
diags = diags.Append(&hcl.Diagnostic{

0 commit comments

Comments
 (0)