Open-Source Wikis

/

GitHub CLI

/

Systems

/

Prompter and survey

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 reads Config.PromptDisabled() and falls back to a no-op prompter when the user has opted out.
  • Every interactive command takes a Prompter field 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 accessible config flag and the gh accessibility settings.

Entry points for modification

  • New prompt type: add a method to the interface, implement it in prompter.go, regenerate the mock with go generate ./internal/prompter/....
  • New hook: add it to hooks.go and 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.

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

Prompter and survey – GitHub CLI wiki | Factory