Golang Prompts

Prompt interface overview

The following Prompts are supported by the Golang SDK:

  • Checkbox
  • Confirm
  • Datetime
  • Editor
  • Input
  • List
  • Number
  • Password
  • Secret

To create a prompt in your Op, you need to create a new Prompt object (whose methods are the above listed supported Prompts):

func NewPrompt() *Prompt

Alternatively, the Prompt object is included in the Client object:

client := NewClient()
client.Prompt.Checkbox(...)

func (*Prompt) Checkbox

func (*Prompt) Checkbox(name, msg string, choices []string, options ...CheckboxOption) ([]string, error)

Checkbox presents a list of options to the user, who can select multiple items in the interface (i.e. terminal or Slack).

choices is the list of string options that can be selected.

Example:

p := ctoai.NewPrompt()
resp, err := p.PromptCheckbox("tools", "Which interpreters would you like to have included in your OS image?", []string{"Lua", "Node.js", "Perl", "Python 2", "Python 3", "Raku", "Ruby"}, OptCheckboxDefaultValues([]string{"Lua"}), OptCheckboxFlag("C")) // user selects Lua
if err != nil {
    panic(err)
}

fmt.Println(resp)

Output:

Lua

Type:

type CheckboxOption func(*daemon.CheckboxPromptBody)

func OptCheckboxDefaultIndex

func OptCheckboxDefaultIndex(defaultIndexes []int) CheckboxOption

func OptCheckboxDefaultValues

func OptCheckboxDefaultValues(defaultValues []string) CheckboxOption

func OptCheckboxFlag

func OptCheckboxFlag(flag string) CheckboxOption

OptCheckboxFlag sets the flag value for the checkbox prompt. The flag value is used to match the command line arguments to prompts.

func (*Prompt) Confirm

func (*Prompt) Confirm(name, msg string, options ...ConfirmOption) (bool, error)

Confirm presents a yes/no question to the user in the interface (i.e. terminal or Slack).

Example:

p := ctoai.NewPrompt()
resp, err := p.PromptConfirm("verbose", "Do you want to run in verbose mode?", OptConfirmFlag("c"), OptConfirmDefault(false)) // user responds with y
if err != nil {
    panic(err)
}

fmt.Println(resp)

Output:

true

Type:

type ConfirmOption func(*daemon.ConfirmPromptBody)

func OptConfirmDefault

func OptConfirmDefault(defaultValue bool) ConfirmOption

func OptConfirmFlag

func OptConfirmFlag(flag string) ConfirmOption

OptConfirmFlag sets the flag value for the confirm prompt. The flag value is used to match command line arguments to prompts.

func (*Prompt) Datetime

func (*Prompt) Datetime(name, msg string, options ...DatetimeOption) (time.Time, error)

Datetime presents a date picker to the user that allows them to select a date and/or time. The method returns the user's response as a time.Time type.

Example:

import "time"

p := ctoai.NewPrompt()
resp, err := p.Datetime("nextRun", "When do you want to run the code next?", OptDatetimeVariant(DATETIME), OptDatetimeFlag("T"), OptDatetimeDefault(time.Now()), OptDatetimeMaximum(time.Now().Add(time.Hour * 2)), OptDatetimeMinimum(time.Now())) // user selects default
if err != nil {
    panic(err)
}

fmt.Println(resp)

Output:

[the output will equal time.Now() in 2006-01-02 15:04:05 format]

Type:

type DatetimeOption func(*daemon.DatetimePromptBody)

func OptDatetimeDefault

func OptDatetimeDefault(defaultValue time.Time) DatetimeOption

func OptDatetimeFlag

func OptDatetimeFlag(flag string) DatetimeOption

OptDatetimeFlag sets the flag value for the datetime prompt. The flag value is used to match the command line arguments to prompts.

func OptDatetimeMaximum

func OptDatetimeMaximum(maximumValue time.Time) DatetimeOption

func OptDatetimeMinimum

func OptDatetimeMinimum(minimumValue time.Time) DatetimeOption

func OptDatetimeVariant

func OptDatetimeVariant(variant string) DatetimeOption

func (*Prompt) Editor

func (*Prompt) Editor(name, msg string, options ...EditorOption) (string, error)

Editor presets a prompt requesting a multi-line response from the user. If used in a terminal interface, the nano editor will be presented.

Example:

p := ctoai.NewPrompt()

template := `Features:

Fixes:

Chores:
`
resp, err := p.Editor("notes", "Please enter your release notes", OptEditorDefault(template), OptEditorFlag("e"))
if err != nil {
    panic(err)
}

Output:

[Nano will be brought up with the template in the editor]

Type:

type EditorOption func(*daemon.EditorPromptBody)

func OptEditorDefault

func OptEditorDefault(defaultValue string) EditorOption

func OptEditorFlag

func OptEditorFlag(flag string) EditorOption

OptEditorFlag sets the flag value for the editor prompt. The flag value is used to match command line arguments to prompts.

func (*Prompt) Input

func (*Prompt) Input(name, msg string, options ...InputOption) (string, error)

Input presents an input (single-line text) prompt on the interface (i.e. terminal or Slack).

The method returns the user's response as string.

Example:

p := ctoai.NewPrompt()
resp, err := p.Input("opinion" ,"What do you think of Go?", ctoai.OptInputDefault("good")) // user responds with "good"
if err != nil {
    panic(err) 
}

fmt.Println(resp)

Output:

good

Type:

type InputOption func(*daemon.InputPromptBody)

func OptInputAllowEmpty

func OptInputAllowEmpty(allowEmpty bool) InputOption

OptInputAllowEmpty sets whether the input prompt should accept an empty line. Has no effect if a default is set.

func OptInputDefault

func OptInputDefault(defaultValue string) InputOption

OptInputDefault sets the default value for the input prompt.

func OptInputFlag

func OptInputFlag(flag string) InputOption

OptInputFlag sets the flag value for the input prompt. The flag value is used to match command line arguments to prompts.

func (*Prompt) List

func (*Prompt) List(name, msg string, choices []string, options ...ListOption) (string, error)

List presents a list of options to the user to select one item from in the interface (i.e. terminal or Slack).

choices is the list of string options that can be selected.

Example:

p := ctoai.NewPrompt()
resp, err := p.List("platform", "What cloud platform would you like to deploy to?", []string{"AWS", "Google Cloud", "Azure"}, OptListDefault("AWS"), OptListFlag("L"), OptListAutocomplete(true)) // user selects Azure
if err != nil {
    panic(err)
}

fmt.Println(resp)

Output:

Azure

Type:

type ListOption func(*daemon.ListPromptBody)

func OptListAutocomplete

func OptListAutocomplete(autocomplete bool) ListOption

func OptListDefaultIndex

func OptListDefaultIndex(defaultIndex int) ListOption

func OptListDefaultValue

func OptListDefaultValue(defaultValue string) ListOption

func OptListFlag

func OptListFlag(flag string) ListOption

OptListFlag sets the flag value for the list prompt. The flag value is used to match command line arguments to prompts.

func (*Prompt) Number

func (*Prompt) Number(name, msg string, options ...NumberOption) (int, error)

Number presents a prompt for a numeric value to the interface (i.e. terminal or Slack).

The method returns the user's response as int.

Example:

p := ctoai.NewPrompt()
resp, err := p.Number("count", "How many hoorays do you want?", OptNumberFlag("n"), OptNumberDefault(10), OptNumberMinimum(0), OptNumberMaximum(10)) // user responds with 7
if err != nil {
    panic(err)
}

fmt.Println(resp)

Output:

7

Type:

type NumberOption func(*daemon.NumberPromptBody)

func OptNumberDefault

func OptNumberDefault(defaultValue int) NumberOption

func OptNumberFlag

func OptNumberFlag(flag string) NumberOption

OptNumberFlag sets the flag value for the number prompt. The flag value is used to match command line arguments to prompts.

func OptNumberMaximum

func OptNumberMaximum(maximumValue int) NumberOption

func OptNumberMinimum

func OptNumberMinimum(minimumValue int) NumberOption

func (*Prompt) Password

func (*Prompt) Password(name, msg string, options ...PasswordOption) (string, error)

Password presents an input prompt for passwords in the interface (i.e. terminal or Slack).

The password can be entered by the user or selected from their team's secret store. If the value is entered directly, it will be obscured on the screen.

For passwords, the name doubles as the default secret store key for the desired secret. If this key is present, it will be selected as the default option for the user.

The method returns the user's response as string.

Example:

p := ctoai.NewPrompt()
resp, err := p.Password("password", "What new password would you like to use?", OptPasswordFlag("p")) // user responds with 1234asdf
if err != nil {
    panic(err)
}

fmt.Println(resp)

Output:

1234asdf

Type:

type PasswordOption func(*daemon.PasswordPromptBody)

func OptPasswordConfirm

func OptPasswordConfirm(confirm bool) PasswordOption

func OptPasswordFlag

func OptPasswordFlag(flag string) PasswordOption

OptPasswordFlag sets the flag value for the password prompt. The flag value is used to match command line arguments to prompts.

func (*Prompt) Secret

func (*Prompt) Secret(name, msg string, options ...SecretOption) (string, error)

Secret presents an input prompt for secrets in the interface (i.e. terminal or Slack).

The secret can be entered by the user or selected from their team's secret store. The value will be displayed on the terminal/screen as it is entered for verification

For secrets, the name doubles as the default secret store key for the desired secret. If this key is present, it will be selected as the default option for the user.

The method returns the user's response as string.

Example:

p := ctoai.NewPrompt()
resp, err := p.Secret("SSH_KEY", "What SSH private key do you want to use?", OptSecretFlag("s")) // user responds with 1234asdf
if err != nil {
    panic(err)
}

fmt.Println(resp)

Output:

1234asdf

Type:

type SecretOption func(*daemon.SecretPromptBody)

func OptSecretFlag

func OptSecretFlag(flag string) SecretOption

OptSecretFlag sets the flag value for the secret prompt. The flag value is used to match command line arguments to prompts.

🚀 What's next?

  • If you want to explore the options offered by our SDKs, go to the Golang SDK page.
  • To learn more about the UX options available for you, please go to Golang UX page.
  • Add Secrets Management page to the Password and Secrets prompts.