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 ¶
- func GetGuidedPromptRunner(input io.Reader, output io.Writer, errorOutput io.Writer, testMode bool) orchestration.PromptRunner
- type ConfirmationPrompter
- type DefaultErrorFormatter
- func (f *DefaultErrorFormatter) Format(err error) string
- func (f *DefaultErrorFormatter) FormatMultiple(errs []error, maxErrors int) string
- func (f *DefaultErrorFormatter) FormatMultipleWithLimit(errs []error, maxErrors int, verbose bool) string
- func (f *DefaultErrorFormatter) FormatWithCode(err error, code string) string
- func (f *DefaultErrorFormatter) FormatWithContext(err error, context map[string]interface{}) string
- func (f *DefaultErrorFormatter) FormatWithErrorInfo(err error, info ErrorInfo) string
- func (f *DefaultErrorFormatter) FormatWithFix(err error, fix string) string
- func (f *DefaultErrorFormatter) GetErrorInfo(code string) (ErrorInfo, bool)
- func (f *DefaultErrorFormatter) MaskError(err error) error
- type ErrorFormatter
- type ErrorInfo
- type InteractiveGuidedPromptRunner
- func (p *InteractiveGuidedPromptRunner) Message(message string)
- func (p *InteractiveGuidedPromptRunner) Prompt(ctx context.Context, prompts []orchestration.PromptSpec) (orchestration.PromptAnswers, error)
- func (p *InteractiveGuidedPromptRunner) Review(ctx context.Context, review orchestration.ReviewSpec) (bool, error)
- func (p *InteractiveGuidedPromptRunner) Warning(message string)
- type InteractivePrompter
- type ScriptedGuidedPromptRunner
- func (p *ScriptedGuidedPromptRunner) Message(message string)
- func (p *ScriptedGuidedPromptRunner) Prompt(_ context.Context, prompts []orchestration.PromptSpec) (orchestration.PromptAnswers, error)
- func (p *ScriptedGuidedPromptRunner) Review(_ context.Context, _ orchestration.ReviewSpec) (bool, error)
- func (p *ScriptedGuidedPromptRunner) Warning(message string)
- type Severity
- type TestPrompter
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 ¶
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 (*InteractiveGuidedPromptRunner) Message ¶
func (p *InteractiveGuidedPromptRunner) Message(message string)
func (*InteractiveGuidedPromptRunner) Prompt ¶
func (p *InteractiveGuidedPromptRunner) Prompt(ctx context.Context, prompts []orchestration.PromptSpec) (orchestration.PromptAnswers, error)
func (*InteractiveGuidedPromptRunner) Review ¶
func (p *InteractiveGuidedPromptRunner) Review(ctx context.Context, review orchestration.ReviewSpec) (bool, error)
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.
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 (p *ScriptedGuidedPromptRunner) Prompt(_ context.Context, prompts []orchestration.PromptSpec) (orchestration.PromptAnswers, error)
func (*ScriptedGuidedPromptRunner) Review ¶
func (p *ScriptedGuidedPromptRunner) Review(_ context.Context, _ orchestration.ReviewSpec) (bool, error)
func (*ScriptedGuidedPromptRunner) Warning ¶
func (p *ScriptedGuidedPromptRunner) Warning(message string)
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.