github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/views/state_locker.go (about)

     1  package views
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/hashicorp/terraform/internal/command/arguments"
     9  )
    10  
    11  // The StateLocker view is used to display locking/unlocking status messages
    12  // if the state lock process takes longer than expected.
    13  type StateLocker interface {
    14  	Locking()
    15  	Unlocking()
    16  }
    17  
    18  // NewStateLocker returns an initialized StateLocker implementation for the given ViewType.
    19  func NewStateLocker(vt arguments.ViewType, view *View) StateLocker {
    20  	switch vt {
    21  	case arguments.ViewHuman:
    22  		return &StateLockerHuman{view: view}
    23  	case arguments.ViewJSON:
    24  		return &StateLockerJSON{view: view}
    25  	default:
    26  		panic(fmt.Sprintf("unknown view type %v", vt))
    27  	}
    28  }
    29  
    30  // StateLockerHuman is an implementation of StateLocker which prints status to
    31  // a terminal.
    32  type StateLockerHuman struct {
    33  	view *View
    34  }
    35  
    36  var _ StateLocker = (*StateLockerHuman)(nil)
    37  var _ StateLocker = (*StateLockerJSON)(nil)
    38  
    39  func (v *StateLockerHuman) Locking() {
    40  	v.view.streams.Println("Acquiring state lock. This may take a few moments...")
    41  }
    42  
    43  func (v *StateLockerHuman) Unlocking() {
    44  	v.view.streams.Println("Releasing state lock. This may take a few moments...")
    45  }
    46  
    47  // StateLockerJSON is an implementation of StateLocker which prints the state lock status
    48  // to a terminal in machine-readable JSON form.
    49  type StateLockerJSON struct {
    50  	view *View
    51  }
    52  
    53  func (v *StateLockerJSON) Locking() {
    54  	current_timestamp := time.Now().Format(time.RFC3339)
    55  
    56  	json_data := map[string]string{
    57  		"@level":     "info",
    58  		"@message":   "Acquiring state lock. This may take a few moments...",
    59  		"@module":    "terraform.ui",
    60  		"@timestamp": current_timestamp,
    61  		"type":       "state_lock_acquire"}
    62  
    63  	lock_info_message, _ := json.Marshal(json_data)
    64  	v.view.streams.Println(string(lock_info_message))
    65  }
    66  
    67  func (v *StateLockerJSON) Unlocking() {
    68  	current_timestamp := time.Now().Format(time.RFC3339)
    69  
    70  	json_data := map[string]string{
    71  		"@level":     "info",
    72  		"@message":   "Releasing state lock. This may take a few moments...",
    73  		"@module":    "terraform.ui",
    74  		"@timestamp": current_timestamp,
    75  		"type":       "state_lock_release"}
    76  
    77  	lock_info_message, _ := json.Marshal(json_data)
    78  	v.view.streams.Println(string(lock_info_message))
    79  }