Skip to content

Commit 6ff57ce

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

File tree

3 files changed

+7
-27
lines changed

3 files changed

+7
-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,

0 commit comments

Comments
 (0)