repl: Various lint-related updates

This is an assortment of small changes to fix all of the remaining lint
failures in this package, based on our current golangci-lint configuration.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
This commit is contained in:
Martin Atkins 2025-01-03 14:25:10 -08:00
parent da8a2ecfce
commit 69bf43dd56
2 changed files with 13 additions and 7 deletions

View File

@ -86,9 +86,10 @@ func formatNullValue(ty cty.Type) string {
} }
func formatMultilineString(v cty.Value, indent int) (string, bool) { func formatMultilineString(v cty.Value, indent int) (string, bool) {
const minimumLines = 2
str := v.AsString() str := v.AsString()
lines := strings.Split(str, "\n") lines := strings.Split(str, "\n")
if len(lines) < 2 { if len(lines) < minimumLines {
return "", false return "", false
} }
@ -109,7 +110,7 @@ OUTER:
for _, line := range lines { for _, line := range lines {
// If the delimiter matches a line, extend it and start again // If the delimiter matches a line, extend it and start again
if strings.TrimSpace(line) == delimiter { if strings.TrimSpace(line) == delimiter {
delimiter = delimiter + "_" delimiter += "_"
continue OUTER continue OUTER
} }
} }

View File

@ -39,8 +39,8 @@ func (s *Session) Handle(line string) (string, bool, tfdiags.Diagnostics) {
case strings.TrimSpace(line) == "exit": case strings.TrimSpace(line) == "exit":
return "", true, nil return "", true, nil
case strings.TrimSpace(line) == "help": case strings.TrimSpace(line) == "help":
ret, diags := s.handleHelp() ret := s.handleHelp()
return ret, false, diags return ret, false, nil
default: default:
ret, diags := s.handleEval(line) ret, diags := s.handleEval(line)
return ret, false, diags return ret, false, diags
@ -73,7 +73,12 @@ func (s *Session) handleEval(line string) (string, tfdiags.Diagnostics) {
switch { switch {
case valType.Equals(types.TypeType): case valType.Equals(types.TypeType):
// An encapsulated type value, which should be displayed directly. // An encapsulated type value, which should be displayed directly.
valType := val.EncapsulatedValue().(*cty.Type) valType, ok := val.EncapsulatedValue().(*cty.Type)
if !ok {
// Should not get here because types.TypeType's encapsulated type
// is cty.Type, and so it can't possibly encapsulate anything else.
panic(fmt.Sprintf("types.TypeType value contains %T rather than the expected %T", val.EncapsulatedValue(), valType))
}
return typeString(*valType), diags return typeString(*valType), diags
default: default:
diags = diags.Append(tfdiags.Sourceless( diags = diags.Append(tfdiags.Sourceless(
@ -88,7 +93,7 @@ func (s *Session) handleEval(line string) (string, tfdiags.Diagnostics) {
return FormatValue(val, 0), diags return FormatValue(val, 0), diags
} }
func (s *Session) handleHelp() (string, tfdiags.Diagnostics) { func (s *Session) handleHelp() string {
text := ` text := `
The OpenTofu console allows you to experiment with OpenTofu interpolations. The OpenTofu console allows you to experiment with OpenTofu interpolations.
You may access resources in the state (if you have one) just as you would You may access resources in the state (if you have one) just as you would
@ -101,7 +106,7 @@ To exit the console, type "exit" and hit <enter>, or use Control-C or
Control-D. Control-D.
` `
return strings.TrimSpace(text), nil return strings.TrimSpace(text)
} }
// typeString returns a string representation of a given type that is // typeString returns a string representation of a given type that is