Skip to content

Commit

Permalink
Remove direct colorizing in configs package, added support for implic…
Browse files Browse the repository at this point in the history
…it colorizing of diags details & abiding to -no-color
  • Loading branch information
Maed223 committed May 14, 2024
1 parent f652644 commit 6ff57ce
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 27 deletions.
8 changes: 4 additions & 4 deletions internal/command/format/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ func DiagnosticFromJSON(diag *viewsjson.Diagnostic, color *colorstring.Colorize,
if !strings.HasPrefix(line, " ") {
line = wordwrap.WrapString(line, uint(paraWidth))
}
fmt.Fprintf(&buf, "%s\n", line)
fmt.Fprintf(&buf, "%s\n", color.Color(line))
}
} else {
fmt.Fprintf(&buf, "%s\n", diag.Detail)
fmt.Fprintf(&buf, "%s\n", color.Color(diag.Detail))
}
}

Expand Down Expand Up @@ -160,10 +160,10 @@ func DiagnosticPlainFromJSON(diag *viewsjson.Diagnostic, width int) string {
if !strings.HasPrefix(line, " ") {
line = wordwrap.WrapString(line, uint(width-1))
}
fmt.Fprintf(&buf, "%s\n", line)
fmt.Fprintf(&buf, "%s\n", disabledColorize.Color(line))
}
} else {
fmt.Fprintf(&buf, "%s\n", diag.Detail)
fmt.Fprintf(&buf, "%s\n", disabledColorize.Color(diag.Detail))
}
}

Expand Down
17 changes: 2 additions & 15 deletions internal/configs/module_version_deprecations.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"

"github.com/hashicorp/hcl/v2"
"github.com/mitchellh/colorstring"
)

type DirectoryDeprecationInfo struct {
Expand Down Expand Up @@ -69,16 +68,10 @@ func (i *ModuleVersionDeprecationInfo) hasDeprecations() bool {
// as well as placed in the Diagnostic Extra for parsing for the SRO view in HCP Terraform
func (i *DirectoryDeprecationInfo) BuildDeprecationWarning() *hcl.Diagnostic {
modDeprecations := []string{}
color := colorstring.Colorize{
Colors: colorstring.DefaultColors,
Disable: false,
Reset: true,
}
deprecationList := make([]*ModuleVersionDeprecationDiagnosticExtraDeprecationItem, 0, len(i.ModuleVersionDeprecationInfos))
for _, modDeprecationInfo := range i.ModuleVersionDeprecationInfos {
if modDeprecationInfo != nil && modDeprecationInfo.RegistryDeprecation != nil {
msg := color.Color("[reset][bold]Version %s of %s[reset]")
modDeprecation := fmt.Sprintf(msg, modDeprecationInfo.RegistryDeprecation.Version, modDeprecationInfo.SourceName)
modDeprecation := fmt.Sprintf("[reset][bold]Version %s of %s[reset]", 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)
Expand Down Expand Up @@ -112,17 +105,11 @@ func (i *DirectoryDeprecationInfo) BuildDeprecationWarning() *hcl.Diagnostic {
}

func buildChildModuleDeprecations(modDeprecations []*ModuleVersionDeprecationInfo, parentMods []string) ([]string, []*ModuleVersionDeprecationDiagnosticExtraDeprecationItem) {
color := colorstring.Colorize{
Colors: colorstring.DefaultColors,
Disable: false,
Reset: true,
}
modDeprecationStrings := []string{}
var deprecationList []*ModuleVersionDeprecationDiagnosticExtraDeprecationItem
for _, deprecation := range modDeprecations {
if deprecation.RegistryDeprecation != nil {
msg := color.Color("[reset][bold]Version %s of %s %s[reset]")
modDeprecation := fmt.Sprintf(msg, deprecation.RegistryDeprecation.Version, deprecation.SourceName, buildModHierarchy(parentMods, deprecation.SourceName))
modDeprecation := fmt.Sprintf("[reset][bold]Version %s of %s %s[reset]", 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)
Expand Down
9 changes: 1 addition & 8 deletions internal/configs/module_version_deprecations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"testing"

"github.com/hashicorp/hcl/v2"
"github.com/mitchellh/colorstring"
)

func TestBuildDeprecationWarning(t *testing.T) {
Expand Down Expand Up @@ -70,14 +69,8 @@ func TestBuildDeprecationWarning(t *testing.T) {
},
}

color := colorstring.Colorize{
Colors: colorstring.DefaultColors,
Disable: false,
Reset: true,
}

detailStringArray := []string{
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",
"[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",
}
diagWant := &hcl.Diagnostic{
Severity: hcl.DiagWarning,
Expand Down

0 comments on commit 6ff57ce

Please sign in to comment.