kubeform.dev/terraform-backend-sdk@v0.0.0-20220310143633-45f07fe731c5/command/views/refresh.go (about)

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