userutil

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2018 License: MIT Imports: 8 Imported by: 2

README

userutil

What is it?

A Go library for working with users on the command line.

API

User input

The library's API offers several helpful functions for working with user input. Prompting for user input can be configured using a PromptOptions struct. Passing an empty PromptOptions will use the default options.

For example, you can prompt a user to answer a question:

package main

import (
	"log"

	"github.com/stephen-fox/userutil"
)

func main() {
	result, err := userutil.GetUserInput("How is your day going?", userutil.PromptOptions{})
	if err != nil {
		log.Fatal(err.Error())
	}

	log.Println("You said: '" + result + "'")
}

If you wanted to hide user input, you can specify so using the PromptOptions:

package main

import (
	"log"

	"github.com/stephen-fox/userutil"
)

func main() {
	options := userutil.PromptOptions{
		ShouldHideInput: true,
	}
	result, err := userutil.GetUserInput("Say something nice", options)
	if err != nil {
		log.Fatal(err.Error())
	}

	log.Println("You said: '" + result + "'")
}

You can also prompt for a "yes or no" answer:

package main

import (
	"log"

	"github.com/stephen-fox/userutil"
)

func main() {
	answeredYes, err := userutil.GetYesOrNoUserInput("Is today a nice day?", userutil.PromptOptions{})
	if err != nil {
		log.Fatal(err.Error())
	}

	if answeredYes {
		log.Println("You said yes")
	} else {
		log.Println("You said no")
	}
}
Admin permissions

You can also check if the current user is root (or an Administrator):

package main

import (
	"log"

	"github.com/stephen-fox/userutil"
)

func main() {
	err := userutil.IsRoot()
	if err != nil {
		log.Fatal(err.Error())
	}

	log.Println("You are root")
}

Documentation

Overview

Package userutil provides several helpful functions for working with users.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetUserInput

func GetUserInput(promptMessage string, options PromptOptions) (string, error)

GetUserInput issues a prompt to the user and records the user's response.

func GetYesOrNoUserInput

func GetYesOrNoUserInput(promptMessage string, options PromptOptions) (bool, error)

GetYesOrNoUserInput issues a prompt and then checks if the user answered 'yes', 'y', 'no', or 'n'. Any capitalization of these inputs is valid. If the user did not provide any of these inputs then an error of type InputError is returned. If the user answered in the affirmative, then true is returned.

func IsRoot

func IsRoot() error

IsRoot returns a nil error if the current user is root.

Types

type InputError

type InputError struct {
	// contains filtered or unexported fields
}

func (InputError) Error

func (o InputError) Error() string

func (InputError) YesNoNotProvided

func (o InputError) YesNoNotProvided() bool

type PromptOptions

type PromptOptions struct {
	// Whether or not to hide the user's input.
	ShouldHideInput bool

	// The message appended to Y/N prompts.
	YesNoMessage string

	// The string to prefix to the user input message prompt.
	InputPrefix string

	// The string to append to the user input message prompt.
	InputSuffix string
}

PromptOptions configures how the input prompt should behave.

func (PromptOptions) InputPrefixFormat

func (o PromptOptions) InputPrefixFormat() string

func (PromptOptions) InputSuffixFormat

func (o PromptOptions) InputSuffixFormat() string

func (PromptOptions) YesNoSuffixFormat

func (o PromptOptions) YesNoSuffixFormat() string

type UserError

type UserError struct {
	// contains filtered or unexported fields
}

func (UserError) CheckFailed

func (o UserError) CheckFailed() bool

func (UserError) Error

func (o UserError) Error() string

func (UserError) NotRoot

func (o UserError) NotRoot() bool

Jump to

Keyboard shortcuts

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