ui

package
v1.0.0-rc07 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ui provides user interface utilities for the opencenter CLI.

Confirmation Prompts

The package provides a testable confirmation prompt system through the ConfirmationPrompter interface. This allows commands to prompt users for confirmation while remaining testable.

Interactive Mode

In interactive mode, the InteractivePrompter reads from stdin and writes to stdout, prompting the user for yes/no confirmation:

prompter := ui.NewInteractivePrompter(os.Stdin, os.Stdout)
confirmed, err := prompter.Confirm(ctx, "Are you sure?")
if err != nil {
    return err
}
if !confirmed {
    fmt.Println("Operation cancelled")
    return nil
}

Test Mode

In test mode, the TestPrompter returns a predetermined response without prompting the user. This is useful for automated testing:

// Create a prompter that always confirms
prompter := ui.NewTestPrompter(true)
confirmed, err := prompter.Confirm(ctx, "Are you sure?")
// confirmed will always be true

// Create a prompter that always denies
prompter := ui.NewTestPrompter(false)
confirmed, err := prompter.Confirm(ctx, "Are you sure?")
// confirmed will always be false

Automatic Mode Selection

The GetPrompter function automatically selects the appropriate prompter based on the environment:

testMode := os.Getenv("OPENCENTER_TEST_MODE") != ""
prompter := ui.GetPrompter(os.Stdin, os.Stdout, testMode)
confirmed, err := prompter.Confirm(ctx, "Are you sure?")

Context Support

All prompters support context cancellation and timeouts:

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

prompter := ui.NewInteractivePrompter(os.Stdin, os.Stdout)
confirmed, err := prompter.Confirm(ctx, "Are you sure?")
if err == context.DeadlineExceeded {
    fmt.Println("Confirmation timed out")
    return err
}

Usage in Commands

Commands should use the prompter interface to allow for testing:

func runDestroyCommand(cmd *cobra.Command, args []string) error {
    if !force {
        testMode := os.Getenv("OPENCENTER_TEST_MODE") != ""
        prompter := ui.GetPrompter(os.Stdin, cmd.OutOrStdout(), testMode)

        confirmed, err := prompter.Confirm(ctx, "Destroy cluster?")
        if err != nil {
            return err
        }
        if !confirmed {
            fmt.Fprintln(cmd.OutOrStdout(), "Operation cancelled")
            return nil
        }
    }
    // Proceed with destroy operation
    return nil
}

Error Formatting

The package also provides error formatting utilities for consistent error messages across the CLI. See error_formatter.go for details.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetGuidedPromptRunner

func GetGuidedPromptRunner(input io.Reader, output io.Writer, errorOutput io.Writer, testMode bool) orchestration.PromptRunner

Types

type ConfirmationPrompter

type ConfirmationPrompter interface {
	// Confirm prompts the user with a message and returns true if they confirm.
	// The context can be used to cancel the prompt operation.
	Confirm(ctx context.Context, message string) (bool, error)
}

ConfirmationPrompter provides an interface for prompting users for confirmation. This abstraction allows for different implementations in interactive and test modes.

func GetPrompter

func GetPrompter(input io.Reader, output io.Writer, testMode bool) ConfirmationPrompter

GetPrompter returns an appropriate ConfirmationPrompter based on the environment. In test mode (when OPENCENTER_TEST_MODE is set), it returns a TestPrompter. Otherwise, it returns an InteractivePrompter using the provided input and output.

type DefaultErrorFormatter

type DefaultErrorFormatter struct {
	// contains filtered or unexported fields
}

DefaultErrorFormatter implements ErrorFormatter interface Requirements: 15.1, 15.2, 15.3, 15.4, 15.5, 15.6, 15.7, 15.8

func NewDefaultErrorFormatter

func NewDefaultErrorFormatter() *DefaultErrorFormatter

NewDefaultErrorFormatter creates a new error formatter Requirements: 15.1, 15.2, 15.3

func (*DefaultErrorFormatter) Format

func (f *DefaultErrorFormatter) Format(err error) string

Format formats an error with credential masking Requirements: 3.5

func (*DefaultErrorFormatter) FormatMultiple

func (f *DefaultErrorFormatter) FormatMultiple(errs []error, maxErrors int) string

FormatMultiple formats multiple errors with credential masking Requirements: 3.5

func (*DefaultErrorFormatter) FormatMultipleWithLimit

func (f *DefaultErrorFormatter) FormatMultipleWithLimit(errs []error, maxErrors int, verbose bool) string

FormatMultipleWithLimit formats multiple errors with a limit Requirements: 15.5, 15.6

func (*DefaultErrorFormatter) FormatWithCode

func (f *DefaultErrorFormatter) FormatWithCode(err error, code string) string

FormatWithCode formats an error with an error code and credential masking Requirements: 3.5

func (*DefaultErrorFormatter) FormatWithContext

func (f *DefaultErrorFormatter) FormatWithContext(err error, context map[string]interface{}) string

FormatWithContext formats an error with additional context and credential masking Requirements: 3.5

func (*DefaultErrorFormatter) FormatWithErrorInfo

func (f *DefaultErrorFormatter) FormatWithErrorInfo(err error, info ErrorInfo) string

FormatWithErrorInfo formats an error with complete error information Requirements: 15.1, 15.2, 15.4, 15.8

func (*DefaultErrorFormatter) FormatWithFix

func (f *DefaultErrorFormatter) FormatWithFix(err error, fix string) string

FormatWithFix formats an error with a fix suggestion and credential masking Requirements: 3.5

func (*DefaultErrorFormatter) GetErrorInfo

func (f *DefaultErrorFormatter) GetErrorInfo(code string) (ErrorInfo, bool)

GetErrorInfo retrieves error information by code Requirements: 15.2, 15.3

func (*DefaultErrorFormatter) MaskError

func (f *DefaultErrorFormatter) MaskError(err error) error

MaskError masks credentials in an error message Requirements: 3.5

type ErrorFormatter

type ErrorFormatter interface {
	Format(err error) string
	FormatWithFix(err error, fix string) string
	FormatWithCode(err error, code string) string
	FormatWithErrorInfo(err error, info ErrorInfo) string
	GetErrorInfo(code string) (ErrorInfo, bool)
}

ErrorFormatter formats errors with credential masking Requirements: 3.5, 15.1, 15.2, 15.4, 15.8

type ErrorInfo

type ErrorInfo struct {
	Code        string
	Title       string
	Description string
	Fix         string
	FixCommand  string
	Hint        string
	DocsURL     string
	Severity    Severity
}

ErrorInfo contains metadata about an error code Requirements: 15.2, 15.3

type InteractiveGuidedPromptRunner

type InteractiveGuidedPromptRunner struct {
	// contains filtered or unexported fields
}

func NewInteractiveGuidedPromptRunner

func NewInteractiveGuidedPromptRunner(input io.Reader, output io.Writer, errorOutput io.Writer) *InteractiveGuidedPromptRunner

func (*InteractiveGuidedPromptRunner) Message

func (p *InteractiveGuidedPromptRunner) Message(message string)

func (*InteractiveGuidedPromptRunner) Prompt

func (*InteractiveGuidedPromptRunner) Review

func (*InteractiveGuidedPromptRunner) Warning

func (p *InteractiveGuidedPromptRunner) Warning(message string)

type InteractivePrompter

type InteractivePrompter struct {
	// contains filtered or unexported fields
}

InteractivePrompter provides interactive confirmation prompts for users. It reads from stdin and writes to the provided output writer.

func NewInteractivePrompter

func NewInteractivePrompter(input io.Reader, output io.Writer) *InteractivePrompter

NewInteractivePrompter creates a new interactive prompter. If input or output are nil, they default to os.Stdin and os.Stdout.

func (*InteractivePrompter) Confirm

func (p *InteractivePrompter) Confirm(ctx context.Context, message string) (bool, error)

Confirm prompts the user with a message and waits for yes/no input. Returns true if the user confirms (y/yes), false otherwise. The prompt is case-insensitive and accepts y/yes for confirmation.

type ScriptedGuidedPromptRunner

type ScriptedGuidedPromptRunner struct {
	// contains filtered or unexported fields
}

func NewScriptedGuidedPromptRunnerFromJSON

func NewScriptedGuidedPromptRunnerFromJSON(raw string, output io.Writer) (*ScriptedGuidedPromptRunner, error)

func (*ScriptedGuidedPromptRunner) Message

func (p *ScriptedGuidedPromptRunner) Message(message string)

func (*ScriptedGuidedPromptRunner) Prompt

func (*ScriptedGuidedPromptRunner) Review

func (*ScriptedGuidedPromptRunner) Warning

func (p *ScriptedGuidedPromptRunner) Warning(message string)

type Severity

type Severity int

Severity represents the severity level of an error

const (
	SeverityInfo Severity = iota
	SeverityWarning
	SeverityCritical
)

type TestPrompter

type TestPrompter struct {
	// contains filtered or unexported fields
}

TestPrompter provides a non-interactive prompter for testing. It returns a predetermined response without prompting the user.

func NewTestPrompter

func NewTestPrompter(response bool) *TestPrompter

NewTestPrompter creates a new test prompter with a predetermined response. If response is true, Confirm() will always return true. If response is false, Confirm() will always return false.

func (*TestPrompter) Confirm

func (p *TestPrompter) Confirm(ctx context.Context, message string) (bool, error)

Confirm returns the predetermined response without prompting. This is useful for testing code that requires user confirmation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL