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

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package views
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/terramate-io/tf/command/arguments"
    10  	"github.com/terramate-io/tf/command/views/json"
    11  	"github.com/terramate-io/tf/states"
    12  	"github.com/terramate-io/tf/terraform"
    13  	"github.com/terramate-io/tf/tfdiags"
    14  )
    15  
    16  // The Refresh view is used for the refresh command.
    17  type Refresh interface {
    18  	Outputs(outputValues map[string]*states.OutputValue)
    19  
    20  	Operation() Operation
    21  	Hooks() []terraform.Hook
    22  
    23  	Diagnostics(diags tfdiags.Diagnostics)
    24  	HelpPrompt()
    25  }
    26  
    27  // NewRefresh returns an initialized Refresh implementation for the given ViewType.
    28  func NewRefresh(vt arguments.ViewType, view *View) Refresh {
    29  	switch vt {
    30  	case arguments.ViewJSON:
    31  		return &RefreshJSON{
    32  			view: NewJSONView(view),
    33  		}
    34  	case arguments.ViewHuman:
    35  		return &RefreshHuman{
    36  			view:         view,
    37  			inAutomation: view.RunningInAutomation(),
    38  			countHook:    &countHook{},
    39  		}
    40  	default:
    41  		panic(fmt.Sprintf("unknown view type %v", vt))
    42  	}
    43  }
    44  
    45  // The RefreshHuman implementation renders human-readable text logs, suitable for
    46  // a scrolling terminal.
    47  type RefreshHuman struct {
    48  	view *View
    49  
    50  	inAutomation bool
    51  
    52  	countHook *countHook
    53  }
    54  
    55  var _ Refresh = (*RefreshHuman)(nil)
    56  
    57  func (v *RefreshHuman) Outputs(outputValues map[string]*states.OutputValue) {
    58  	if len(outputValues) > 0 {
    59  		v.view.streams.Print(v.view.colorize.Color("[reset][bold][green]\nOutputs:\n\n"))
    60  		NewOutput(arguments.ViewHuman, v.view).Output("", outputValues)
    61  	}
    62  }
    63  
    64  func (v *RefreshHuman) Operation() Operation {
    65  	return NewOperation(arguments.ViewHuman, v.inAutomation, v.view)
    66  }
    67  
    68  func (v *RefreshHuman) Hooks() []terraform.Hook {
    69  	return []terraform.Hook{
    70  		v.countHook,
    71  		NewUiHook(v.view),
    72  	}
    73  }
    74  
    75  func (v *RefreshHuman) Diagnostics(diags tfdiags.Diagnostics) {
    76  	v.view.Diagnostics(diags)
    77  }
    78  
    79  func (v *RefreshHuman) HelpPrompt() {
    80  	v.view.HelpPrompt("refresh")
    81  }
    82  
    83  // The RefreshJSON implementation renders streaming JSON logs, suitable for
    84  // integrating with other software.
    85  type RefreshJSON struct {
    86  	view *JSONView
    87  }
    88  
    89  var _ Refresh = (*RefreshJSON)(nil)
    90  
    91  func (v *RefreshJSON) Outputs(outputValues map[string]*states.OutputValue) {
    92  	outputs, diags := json.OutputsFromMap(outputValues)
    93  	if diags.HasErrors() {
    94  		v.Diagnostics(diags)
    95  	} else {
    96  		v.view.Outputs(outputs)
    97  	}
    98  }
    99  
   100  func (v *RefreshJSON) Operation() Operation {
   101  	return &OperationJSON{view: v.view}
   102  }
   103  
   104  func (v *RefreshJSON) Hooks() []terraform.Hook {
   105  	return []terraform.Hook{
   106  		newJSONHook(v.view),
   107  	}
   108  }
   109  
   110  func (v *RefreshJSON) Diagnostics(diags tfdiags.Diagnostics) {
   111  	v.view.Diagnostics(diags)
   112  }
   113  
   114  func (v *RefreshJSON) HelpPrompt() {
   115  }