github.com/opentofu/opentofu@v1.7.1/internal/command/cli_ui.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  
    11  	"github.com/mitchellh/cli"
    12  	"github.com/mitchellh/colorstring"
    13  )
    14  
    15  // ColoredUi is a Ui implementation that colors its output according
    16  // to the given color schemes for the given type of output.
    17  type ColorizeUi struct {
    18  	Colorize    *colorstring.Colorize
    19  	OutputColor string
    20  	InfoColor   string
    21  	ErrorColor  string
    22  	WarnColor   string
    23  	Ui          cli.Ui
    24  }
    25  
    26  func (u *ColorizeUi) Ask(query string) (string, error) {
    27  	return u.Ui.Ask(u.colorize(query, u.OutputColor))
    28  }
    29  
    30  func (u *ColorizeUi) AskSecret(query string) (string, error) {
    31  	return u.Ui.AskSecret(u.colorize(query, u.OutputColor))
    32  }
    33  
    34  func (u *ColorizeUi) Output(message string) {
    35  	u.Ui.Output(u.colorize(message, u.OutputColor))
    36  }
    37  
    38  func (u *ColorizeUi) Info(message string) {
    39  	u.Ui.Info(u.colorize(message, u.InfoColor))
    40  }
    41  
    42  func (u *ColorizeUi) Error(message string) {
    43  	u.Ui.Error(u.colorize(message, u.ErrorColor))
    44  }
    45  
    46  func (u *ColorizeUi) Warn(message string) {
    47  	u.Ui.Warn(u.colorize(message, u.WarnColor))
    48  }
    49  
    50  func (u *ColorizeUi) colorize(message string, color string) string {
    51  	if color == "" {
    52  		return message
    53  	}
    54  
    55  	return u.Colorize.Color(fmt.Sprintf("%s%s[reset]", color, message))
    56  }