ioutils

package
v2.56.8 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2024 License: Apache-2.0 Imports: 19 Imported by: 5

Documentation

Index

Constants

View Source
const (
	PressTabMsg      = " (press Tab for options):"
	InvalidAnswerMsg = "Invalid answer. Please select value from the suggestions list."
	VariableUseMsg   = " You may use dynamic variable in the form of ${key}."
	EmptyValueMsg    = "The value cannot be empty. Please enter a valid value."
	OptionalKey      = "OptionalKey"
	SaveAndExit      = ":x"

	// Boolean answers
	True  = "true"
	False = "false"

	CommaSeparatedListMsg = "The value should be a comma separated list"
)
View Source
const (
	DummyDefaultAnswer = "-"
)

Variables

View Source
var FreeStringQuestionInfo = QuestionInfo{
	Options:   nil,
	AllowVars: false,
	Writer:    WriteStringAnswer,
}

Common questions

View Source
var VarPattern = regexp.MustCompile(`^\$\{\w+}+$`)

Var can be inserted in the form of ${key}

Functions

func AskFromList added in v2.47.10

func AskFromList(msg, promptPrefix string, allowVars bool, options []prompt.Suggest, defaultValue string) string

Ask question with list of possible answers. If answer is empty and defaultValue isn't, return defaultValue. Otherwise, the answer must be chosen from the list, but can be a variable if allowVars set to true.

func AskFromListWithMismatchConfirmation added in v2.47.10

func AskFromListWithMismatchConfirmation(promptPrefix, misMatchMsg string, options []prompt.Suggest) string

Ask question with list of possible answers. If the provided answer does not appear in list, confirm the choice.

func AskString added in v2.47.10

func AskString(msg, promptPrefix string, allowEmpty bool, allowVars bool) string

Ask question with free string answer, allow an empty string as an answer

func AskStringWithDefault added in v2.47.10

func AskStringWithDefault(msg, promptPrefix, defaultValue string) string

Ask question with free string answer. If answer is empty and defaultValue isn't, return defaultValue. Otherwise, answer cannot be empty. Variable aren't checked and can be part of the answer.

func BackupFile added in v2.47.10

func BackupFile(filePath, backupFileName string) (restore func() error, err error)

BackupFile creates a backup of the file in filePath. The backup will be found at backupPath. The returned restore function can be called to restore the file's state - the file in filePath will be replaced by the backup in backupPath. If there is no file at filePath, a backup file won't be created, and the restore function will delete the file at filePath.

func ConvertToSuggests added in v2.47.10

func ConvertToSuggests(options []string) []prompt.Suggest

func DoubleWinPathSeparator

func DoubleWinPathSeparator(filePath string) string

func GetBoolSuggests added in v2.47.10

func GetBoolSuggests() []prompt.Suggest

func GetSuggestsFromKeys added in v2.47.10

func GetSuggestsFromKeys(keys []string, suggestionMap map[string]prompt.Suggest) []prompt.Suggest

func OptionalKeyCallback added in v2.47.10

func OptionalKeyCallback(iq *InteractiveQuestionnaire, key string) (value string, err error)

After an optional value was chosen we'll ask for its value.

func PromptStrings

func PromptStrings(items []PromptItem, label string, onSelect func(PromptItem)) error

Prompt strings by selecting from list until "Save and continue" is selected. Usage example: 🐸 Save and continue JFrog Artifactory URL (http://localhost:8080/artifactory/) JFrog Distribution URL () JFrog Xray URL () JFrog Mission Control URL () JFrog Pipelines URL ()

func ReadCredentialsFromConsole

func ReadCredentialsFromConsole(details, savedDetails coreutils.Credentials, disallowUsingSavedPassword bool) error

disallowUsingSavedPassword - Prevent changing username or url without changing the password. False if the user changed the username or the url.

func ScanFromConsole

func ScanFromConsole(caption string, scanInto *string, defaultValue string)

func ScanJFrogPasswordFromConsole added in v2.14.0

func ScanJFrogPasswordFromConsole() (string, error)

func ScanPasswordFromConsole added in v2.2.0

func ScanPasswordFromConsole(message string) (string, error)

func SelectString

func SelectString(items []PromptItem, label string, needSearch bool, onSelect func(PromptItem)) error

func UnixToWinPathSeparator

func UnixToWinPathSeparator(filePath string) string

func WinToUnixPathSeparator

func WinToUnixPathSeparator(filePath string) string

func WriteBoolAnswer added in v2.47.10

func WriteBoolAnswer(resultMap *map[string]interface{}, key, value string) error

func WriteIntAnswer added in v2.47.10

func WriteIntAnswer(resultMap *map[string]interface{}, key, value string) error

func WriteStringAnswer added in v2.47.10

func WriteStringAnswer(resultMap *map[string]interface{}, key, value string) error

Common writers

func WriteStringArrayAnswer added in v2.47.10

func WriteStringArrayAnswer(resultMap *map[string]interface{}, key, value string) error

Types

type AnswerWriter added in v2.47.10

type AnswerWriter func(resultMap *map[string]interface{}, key, value string) error

Each question can have the following properties:

  • Msg - will be printed in separate line
  • PromptPrefix - will be printed before the input cursor in the answer line
  • Options - In case the answer must be selected from a predefined list
  • AllowVars - a flag indicates whether a variable (in form of ${var}) is an acceptable answer despite the predefined list
  • Writer - how to write the answer to the final config map
  • MapKey - the key under which the answer will be written to the configMap
  • Callback - optional function can be executed after the answer was inserted. Can be used to implement some dependencies between questions.

type InteractiveQuestionnaire added in v2.47.10

type InteractiveQuestionnaire struct {
	QuestionsMap           map[string]QuestionInfo
	MandatoryQuestionsKeys []string
	OptionalKeysSuggests   []prompt.Suggest
	AnswersMap             map[string]interface{}
}

The interactive questionnaire works as follows:

We have to provide a map of QuestionInfo which include all possible questions may be asked.
1. Mandatory Questions:
	* We will ask all the questions in MandatoryQuestionsKeys list one after the other.
2. Optional questions:
	* We have to provide a slice of prompt.Suggest, in which each suggest.Text is a key of a question in the map.
	* After a suggestion was chosen from the list, the corresponding question from the map will be asked.
	* Each answer is written to the configMap using its writer, under the MapKey specified in the questionInfo.
	* We will execute the previous step until the SaveAndExit string was inserted.

func (*InteractiveQuestionnaire) AskQuestion added in v2.47.10

func (iq *InteractiveQuestionnaire) AskQuestion(question QuestionInfo) (value string, err error)

Ask question steps:

  1. Ask for string/from list
  2. Write the answer to answersMap (if writer provided)
  3. Run callback (if provided)

func (*InteractiveQuestionnaire) Perform added in v2.47.10

func (iq *InteractiveQuestionnaire) Perform() error

The main function to perform the questionnaire

type PromptItem

type PromptItem struct {
	// The option string to show, i.e - JFrog Artifactory URL.
	Option string
	// The variable to set.
	TargetValue *string
	// Default value to show. If empty string is entered, use the default value.
	DefaultValue string
}

type QuestionInfo added in v2.47.10

type QuestionInfo struct {
	Msg          string
	PromptPrefix string
	Options      []prompt.Suggest
	AllowVars    bool
	Writer       AnswerWriter
	MapKey       string
	Callback     questionCallback
}

Jump to

Keyboard shortcuts

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