github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/command/views/state_locker.go (about)

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