github.com/opentofu/opentofu@v1.7.1/internal/backend/cli.go (about)

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