refactor: break the dependency between jsonformat/computed/renderers and command/format

Signed-off-by: Diogenes Fernandes <diofeher@gmail.com>
This commit is contained in:
Diogenes Fernandes
2025-07-14 16:14:32 -03:00
committed by Diógenes Fernandes
parent 3ff2dedd4b
commit 4a4d06cee1
3 changed files with 30 additions and 33 deletions

View File

@@ -11,7 +11,6 @@ import (
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/opentofu/opentofu/internal/command/format"
"github.com/opentofu/opentofu/internal/command/jsonformat/computed"
"github.com/opentofu/opentofu/internal/plans"
)
@@ -80,6 +79,33 @@ func hclEscapeString(str string) string {
return fmt.Sprintf("%q", str)
}
// DiffActionSymbol returns a string that, once passed through a
// colorstring.Colorize, will produce a result that can be written
// to a terminal to produce a symbol made of three printable
// characters, possibly interspersed with VT100 color codes.
func DiffActionSymbol(action plans.Action) string {
switch action {
case plans.DeleteThenCreate:
return "[red]-[reset]/[green]+[reset]"
case plans.CreateThenDelete:
return "[green]+[reset]/[red]-[reset]"
case plans.Create:
return " [green]+[reset]"
case plans.Delete:
return " [red]-[reset]"
case plans.Read:
return " [cyan]<=[reset]"
case plans.Update:
return " [yellow]~[reset]"
case plans.NoOp:
return " "
case plans.Forget:
return " [red].[reset]"
default:
return " ?"
}
}
// writeDiffActionSymbol writes out the symbols for the associated action, and
// handles localized colorization of the symbol as well as indenting the symbol
// to be 4 spaces wide.
@@ -91,5 +117,5 @@ func writeDiffActionSymbol(action plans.Action, opts computed.RenderHumanOpts) s
return ""
}
return fmt.Sprintf("%s ", opts.Colorize.Color(format.DiffActionSymbol(action)))
return fmt.Sprintf("%s ", opts.Colorize.Color(DiffActionSymbol(action)))
}

View File

@@ -296,7 +296,7 @@ func renderHumanDiffOutputs(renderer Renderer, outputs map[string]computed.Diff)
for _, key := range keys {
output := outputs[key]
if output.Action != plans.NoOp {
rendered = append(rendered, fmt.Sprintf("%s %-*s = %s", renderer.Colorize.Color(format.DiffActionSymbol(output.Action)), escapedKeyMaxLen, escapedKeys[key], output.RenderHuman(0, computed.NewRenderHumanOpts(renderer.Colorize, renderer.ShowSensitive))))
rendered = append(rendered, fmt.Sprintf("%s %-*s = %s", renderer.Colorize.Color(renderers.DiffActionSymbol(output.Action)), escapedKeyMaxLen, escapedKeys[key], output.RenderHuman(0, computed.NewRenderHumanOpts(renderer.Colorize, renderer.ShowSensitive))))
}
}
return strings.Join(rendered, "\n")
@@ -386,7 +386,7 @@ func renderHumanDiff(renderer Renderer, diff diff, cause string) (string, bool)
}
opts.ShowUnchangedChildren = diff.Importing()
buf.WriteString(fmt.Sprintf("%s %s %s", renderer.Colorize.Color(format.DiffActionSymbol(action)), resourceChangeHeader(diff.change), diff.diff.RenderHuman(0, opts)))
buf.WriteString(fmt.Sprintf("%s %s %s", renderer.Colorize.Color(renderers.DiffActionSymbol(action)), resourceChangeHeader(diff.change), diff.diff.RenderHuman(0, opts)))
return buf.String(), true
}