cli/cli
Prompter and survey
Active contributors: Sam Coe, vilmibm, Mislav
Purpose
internal/prompter/ is the only sanctioned place to ask the user a question. It hides the gritty details of survey/v2, Bubble Tea selectors, and confirmation flows behind a small interface that can be mocked with moq.
Directory layout
internal/prompter/
prompter.go # The Prompter interface
prompter_mock.go # Generated moq mock
prompter_test.go
hooks.go # Pre-prompt hooks (e.g. accessibility)Key abstractions
| Symbol | File | Role |
|---|---|---|
Prompter interface |
prompter.go |
The methods every command uses: Select, MultiSelect, Input, Password, Confirm, AuthToken, MarkdownEditor, ... |
Implementation |
prompter.go |
Real implementation backed by AlecAivazis/survey/v2 plus Bubble Tea selectors for the new menus. |
PrompterMock |
prompter_mock.go |
Generated mock used in tests. |
Hooks |
hooks.go |
Behaviour modifiers (e.g. screen-reader-friendly mode). |
How it works
A typical flow inside gh issue create:
title, err := opts.Prompter.Input("Title", "")
if err != nil { return err }
body, err := opts.Prompter.MarkdownEditor("Body", "", true /* edit empty? */)
if err != nil { return err }
submit, err := opts.Prompter.Confirm("Submit?", true)
if err != nil || !submit { return cmdutil.CancelError }Tests substitute the mock:
opts.Prompter = &prompter.PrompterMock{
InputFunc: func(prompt, defaultValue string) (string, error) {
return "title", nil
},
ConfirmFunc: func(prompt string, defaultValue bool) (bool, error) {
return true, nil
},
}PrompterMock.Calls() records every call so a test can assert that exactly the expected prompts ran.
Integration points
- The factory builds the prompter in
factory.newPrompter. It readsConfig.PromptDisabled()and falls back to a no-op prompter when the user has opted out. - Every interactive command takes a
Prompterfield on its options. The default for tests is the mock; the default for production is the real implementation. - The codepath for accessibility-friendly prompts respects the
accessibleconfig flag and thegh accessibilitysettings.
Entry points for modification
- New prompt type: add a method to the interface, implement it in
prompter.go, regenerate the mock withgo generate ./internal/prompter/.... - New hook: add it to
hooks.goand register it in the Implementation constructor. - Migration away from
survey/v2: replace one prompt at a time. The interface is intentionally narrow to permit this.
Key source files
| File | Purpose |
|---|---|
internal/prompter/prompter.go |
Interface + Implementation. |
internal/prompter/prompter_mock.go |
moq-generated mock. |
internal/prompter/hooks.go |
Pre-prompt hooks. |
Related pages
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.