github.com/opentofu/opentofu@v1.7.1/internal/command/console_interactive.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 command
     7  
     8  import (
     9  	"fmt"
    10  	"io"
    11  	"os"
    12  
    13  	"github.com/opentofu/opentofu/internal/repl"
    14  
    15  	"github.com/chzyer/readline"
    16  	"github.com/mitchellh/cli"
    17  )
    18  
    19  func (c *ConsoleCommand) modeInteractive(session *repl.Session, ui cli.Ui) int {
    20  	// Configure input
    21  	l, err := readline.NewEx(&readline.Config{
    22  		Prompt:            "> ",
    23  		InterruptPrompt:   "^C",
    24  		EOFPrompt:         "exit",
    25  		HistorySearchFold: true,
    26  		Stdin:             os.Stdin,
    27  		Stdout:            os.Stdout,
    28  		Stderr:            os.Stderr,
    29  	})
    30  	if err != nil {
    31  		c.Ui.Error(fmt.Sprintf(
    32  			"Error initializing console: %s",
    33  			err))
    34  		return 1
    35  	}
    36  	defer l.Close()
    37  
    38  	for {
    39  		// Read a line
    40  		line, err := l.Readline()
    41  		if err == readline.ErrInterrupt {
    42  			if len(line) == 0 {
    43  				break
    44  			} else {
    45  				continue
    46  			}
    47  		} else if err == io.EOF {
    48  			break
    49  		}
    50  
    51  		out, exit, diags := session.Handle(line)
    52  		if diags.HasErrors() {
    53  			c.showDiagnostics(diags)
    54  		}
    55  		if exit {
    56  			break
    57  		}
    58  
    59  		ui.Output(out)
    60  	}
    61  
    62  	return 0
    63  }