From 69bf43dd5630849a5509d841ff2f1daeb4abe369 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 3 Jan 2025 14:25:10 -0800 Subject: [PATCH] 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 --- internal/repl/format.go | 5 +++-- internal/repl/session.go | 15 ++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/internal/repl/format.go b/internal/repl/format.go index 13fd14a997..3ced3aff42 100644 --- a/internal/repl/format.go +++ b/internal/repl/format.go @@ -86,9 +86,10 @@ func formatNullValue(ty cty.Type) string { } func formatMultilineString(v cty.Value, indent int) (string, bool) { + const minimumLines = 2 str := v.AsString() lines := strings.Split(str, "\n") - if len(lines) < 2 { + if len(lines) < minimumLines { return "", false } @@ -109,7 +110,7 @@ OUTER: for _, line := range lines { // If the delimiter matches a line, extend it and start again if strings.TrimSpace(line) == delimiter { - delimiter = delimiter + "_" + delimiter += "_" continue OUTER } } diff --git a/internal/repl/session.go b/internal/repl/session.go index cac405f685..c9329d2720 100644 --- a/internal/repl/session.go +++ b/internal/repl/session.go @@ -39,8 +39,8 @@ func (s *Session) Handle(line string) (string, bool, tfdiags.Diagnostics) { case strings.TrimSpace(line) == "exit": return "", true, nil case strings.TrimSpace(line) == "help": - ret, diags := s.handleHelp() - return ret, false, diags + ret := s.handleHelp() + return ret, false, nil default: ret, diags := s.handleEval(line) return ret, false, diags @@ -73,7 +73,12 @@ func (s *Session) handleEval(line string) (string, tfdiags.Diagnostics) { switch { case valType.Equals(types.TypeType): // 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 default: diags = diags.Append(tfdiags.Sourceless( @@ -88,7 +93,7 @@ func (s *Session) handleEval(line string) (string, tfdiags.Diagnostics) { return FormatValue(val, 0), diags } -func (s *Session) handleHelp() (string, tfdiags.Diagnostics) { +func (s *Session) handleHelp() string { text := ` 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 @@ -101,7 +106,7 @@ To exit the console, type "exit" and hit , or use Control-C or Control-D. ` - return strings.TrimSpace(text), nil + return strings.TrimSpace(text) } // typeString returns a string representation of a given type that is