mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
The previous implementation of views was copying and embedding the base View struct in each individual view. While this allowed for easy access to the interface of that struct (both in the view and externally), it more importantly completely broke the ability of the diagnostic printer to output source code snippets. This is because the `configSources` field on the base view is lazily set after the config loader is initialized. In the commands ported to use views, this happens after the base View struct is copied, so we are updating the wrong copy of the struct. This commit fixes this with a simple mechanical refactor: keep a pointer to the base View struct instead, and update all of the individual views to explicitly refer to that struct to access its fields and methods. This is not a particularly satisfying solution, but I can't find anything clearly better. It might be worth exploring the alternative approach in the view for the new test command, which explicitly pulls its dependencies out of the base view, rather than retaining a full reference. Maybe there's a third way which is better still.
41 lines
1013 B
Go
41 lines
1013 B
Go
package views
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/terraform/command/arguments"
|
|
)
|
|
|
|
// The StateLocker view is used to display locking/unlocking status messages
|
|
// if the state lock process takes longer than expected.
|
|
type StateLocker interface {
|
|
Locking()
|
|
Unlocking()
|
|
}
|
|
|
|
// NewStateLocker returns an initialized StateLocker implementation for the given ViewType.
|
|
func NewStateLocker(vt arguments.ViewType, view *View) StateLocker {
|
|
switch vt {
|
|
case arguments.ViewHuman:
|
|
return &StateLockerHuman{view: view}
|
|
default:
|
|
panic(fmt.Sprintf("unknown view type %v", vt))
|
|
}
|
|
}
|
|
|
|
// StateLockerHuman is an implementation of StateLocker which prints status to
|
|
// a terminal.
|
|
type StateLockerHuman struct {
|
|
view *View
|
|
}
|
|
|
|
var _ StateLocker = (*StateLockerHuman)(nil)
|
|
|
|
func (v *StateLockerHuman) Locking() {
|
|
v.view.streams.Println("Acquiring state lock. This may take a few moments...")
|
|
}
|
|
|
|
func (v *StateLockerHuman) Unlocking() {
|
|
v.view.streams.Println("Releasing state lock. This may take a few moments...")
|
|
}
|