github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/backend/cli.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package backend
     5  
     6  import (
     7  	"github.com/mitchellh/cli"
     8  	"github.com/mitchellh/colorstring"
     9  
    10  	"github.com/terramate-io/tf/terminal"
    11  	"github.com/terramate-io/tf/terraform"
    12  )
    13  
    14  // CLI is an optional interface that can be implemented to be initialized
    15  // with information from the Terraform CLI. If this is implemented, this
    16  // initialization function will be called with data to help interact better
    17  // with a CLI.
    18  //
    19  // This interface was created to improve backend interaction with the
    20  // official Terraform CLI while making it optional for API users to have
    21  // to provide full CLI interaction to every backend.
    22  //
    23  // If you're implementing a Backend, it is acceptable to require CLI
    24  // initialization. In this case, your backend should be coded to error
    25  // on other methods (such as State, Operation) if CLI initialization was not
    26  // done with all required fields.
    27  type CLI interface {
    28  	Backend
    29  
    30  	// CLIInit is called once with options. The options passed to this
    31  	// function may not be modified after calling this since they can be
    32  	// read/written at any time by the Backend implementation.
    33  	//
    34  	// This may be called before or after Configure is called, so if settings
    35  	// here affect configurable settings, care should be taken to handle
    36  	// whether they should be overwritten or not.
    37  	CLIInit(*CLIOpts) error
    38  }
    39  
    40  // CLIOpts are the options passed into CLIInit for the CLI interface.
    41  //
    42  // These options represent the functionality the CLI exposes and often
    43  // maps to meta-flags available on every CLI (such as -input).
    44  //
    45  // When implementing a backend, it isn't expected that every option applies.
    46  // Your backend should be documented clearly to explain to end users what
    47  // options have an affect and what won't. In some cases, it may even make sense
    48  // to error in your backend when an option is set so that users don't make
    49  // a critically incorrect assumption about behavior.
    50  type CLIOpts struct {
    51  	// CLI and Colorize control the CLI output. If CLI is nil then no CLI
    52  	// output will be done. If CLIColor is nil then no coloring will be done.
    53  	CLI      cli.Ui
    54  	CLIColor *colorstring.Colorize
    55  
    56  	// Streams describes the low-level streams for Stdout, Stderr and Stdin,
    57  	// including some metadata about whether they are terminals. Most output
    58  	// should go via the object in field CLI above, but Streams can be useful
    59  	// for tailoring the output to fit the attached terminal, for example.
    60  	Streams *terminal.Streams
    61  
    62  	// StatePath is the local path where state is read from.
    63  	//
    64  	// StateOutPath is the local path where the state will be written.
    65  	// If this is empty, it will default to StatePath.
    66  	//
    67  	// StateBackupPath is the local path where a backup file will be written.
    68  	// If this is empty, no backup will be taken.
    69  	StatePath       string
    70  	StateOutPath    string
    71  	StateBackupPath string
    72  
    73  	// ContextOpts are the base context options to set when initializing a
    74  	// Terraform context. Many of these will be overridden or merged by
    75  	// Operation. See Operation for more details.
    76  	ContextOpts *terraform.ContextOpts
    77  
    78  	// Input will ask for necessary input prior to performing any operations.
    79  	//
    80  	// Validation will perform validation prior to running an operation. The
    81  	// variable naming doesn't match the style of others since we have a func
    82  	// Validate.
    83  	Input      bool
    84  	Validation bool
    85  
    86  	// RunningInAutomation indicates that commands are being run by an
    87  	// automated system rather than directly at a command prompt.
    88  	//
    89  	// This is a hint not to produce messages that expect that a user can
    90  	// run a follow-up command, perhaps because Terraform is running in
    91  	// some sort of workflow automation tool that abstracts away the
    92  	// exact commands that are being run.
    93  	RunningInAutomation bool
    94  }