confirmer

package module
v0.0.0-...-22724a4 Latest Latest
Warning

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

Go to latest
Published: May 2, 2021 License: MIT Imports: 4 Imported by: 0

README

github.com/eculver/go-play/pkg/confirmer

A simple confirmation library for blocking on user confirmation prompts.

Installation

go get -u github.com/eculver/go-play/pkg/confirmer

Usage

The confirmer.Confirm method provides a default configuration to cover the most common use-case:

import (
    "os"
    "github.com/eculver/go-play/pkg/confirmer"
)

if !confirmer.Confirm("Continue?", os.Stdin) {
    // not confirmed
    os.Exit(1)
}
// confirmed

This will prompt the user up to three times until a valid "accept" or "deny" input is given. If after three times no valid inputs have been given, it will exit and return false:

evan.culver@evan ➜  go-play (confirmer) go run cmd/confirmer/main.go
Continue? [Y/n]: not valid

Continue? [Y/n]: really?

Continue? [Y/n]: yes

not confirmed!
exit status 1
evan.culver@evan ➜  go-play (confirmer) go run cmd/confirmer/main.go
Continue? [Y/n]: Y
confirmed!
evan.culver@evan ➜  go-play (confirmer) go run cmd/confirmer/main.go
Continue? [Y/n]: N
Continue? [Y/n]: n
not confirmed!
exit status 1

A complete working example can be found in ./cmd/confirmer/main.go of this repository.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultAccept = []string{"Y"}
View Source
var DefaultConfirmer = New(DefaultAccept, DefaultDeny, 3)
View Source
var DefaultDeny = []string{"n"}

Functions

func Confirm

func Confirm(msg string, reader io.Reader) bool

Types

type Confirmer

type Confirmer interface {
	Confirm(msg string, reader io.Reader) bool
}

Confirmer confirms user input

func New

func New(accept []string, deny []string, tries int) Confirmer

New returns a new Confirmer for reading acceptable or deniable input for the given number of tries before giving up.

Jump to

Keyboard shortcuts

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