askit

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2021 License: ISC Imports: 5 Imported by: 0

README

askit

This package allows you to use the ask struct tag on your Go struct fields. You can then read the values for those fields using an Asker interface.

An Asker implementation can prompt user and read values from the standard input or any other source (cli provides an implementation for the Asker interface).

Quick Start

package main

import (
  "fmt"
  "os"

  "github.com/gardenbed/charm/askit"
  "github.com/mitchellh/cli"
)

func main() {
  asker := &cli.BasicUi{
    Reader:      os.Stdin,
    Writer:      os.Stdout,
    ErrorWriter: os.Stderr,
  }

  info := struct {
    Name  string `ask:"any, your full name"`
    Email string `ask:"email, your email address"`
    Token string `ask:"secret, your access token"`
  }{
    Name: "Jane Doe",
  }

  err := askit.Ask(&info, asker)
  if err != nil {
    panic(err)
  }

  fmt.Printf("%+v\n", info)
}

Examples

You can find more examples here.

Documentation

Supported Types
  • string, *string, []string
  • bool, *bool, []bool
  • int, int8, int16, int32, int64
  • *int, *int8, *int16, *int32, *int64
  • []int, []int8, []int16, []int32, []int64
  • uint, uint8, uint16, uint32, uint64
  • *uint, *uint8, *uint16, *uint32, *uint64
  • []uint, []uint8, []uint16, []uint32, []uint64
  • float32, float64
  • *float32, *float64
  • []float32, []float64
  • url.URL, *url.URL, []url.URL
  • regexp.Regexp, *regexp.Regexp, []regexp.Regexp
  • byte, *byte, []byte
  • rune, *rune, []rune
  • time.Duration, *time.Duration, []time.Duration

The supported syntax for Regexp is POSIX Regular Expressions. Nested structs are also supported.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ask

func Ask(s interface{}, asker Asker) error
Example
package main

import (
	"os"

	"github.com/gardenbed/charm/askit"
	"github.com/mitchellh/cli"
)

func main() {
	asker := &cli.BasicUi{
		Reader:      os.Stdin,
		Writer:      os.Stdout,
		ErrorWriter: os.Stderr,
	}

	info := struct {
		Name  string `ask:"any, your full name"`
		Email string `ask:"email, your email address"`
		Token string `ask:"secret, your access token"`
	}{}

	err := askit.Ask(&info, asker)
	if err != nil {
		panic(err)
	}
}
Output:

Types

type Asker

type Asker interface {
	Output(string)
	Ask(string) (string, error)
	AskSecret(string) (string, error)
}

Asker is the interface for getting inputs.

type Kind

type Kind string

Kind determines the kind of an input.

const (
	// KindAny denotes any kind of input.
	KindAny Kind = "any"
	// KindEmail denotes an email address input.
	KindEmail Kind = "email"
	// KindSecret denotes a secret input (password, token, etc.).
	KindSecret Kind = "secret"
)

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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