代码拉取完成,页面将自动刷新
/*
* Copyright 2018-2019 The NATS Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cliprompts
import (
"errors"
"fmt"
"io"
"net/mail"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/mitchellh/go-homedir"
)
type Logger func(args ...interface{})
type Validator func(string) error
var cli PromptLib
// set a Logger during a test (cli.LogFn = t.Log) to debug interactive prompts
var LogFn Logger
// lint:ignore U1000
var output io.Writer = os.Stdout
type PromptLib interface {
Prompt(label string, value string, edit bool, validator Validator) (string, error)
PromptWithHelp(label string, value string, edit bool, validator Validator, help string) (string, error)
PromptYN(m string, defaultValue bool) (bool, error)
PromptSecret(m string) (string, error)
PromptChoices(m string, value string, choices []string) (int, error)
PromptMultipleChoices(m string, choices []string) ([]int, error)
}
func init() {
ResetPromptLib()
LogFn = nil
}
func SetPromptLib(p PromptLib) {
cli = p
}
func ResetPromptLib() {
SetPromptLib(&SurveyUI{})
LogFn = nil
}
func SetOutput(out io.Writer) {
output = out
}
func GetOutput() io.Writer {
return output
}
func Underline(s string) string {
return fmt.Sprintf("\xff\033[4m\xff%s\xff\033[0m\xff", s)
}
func Bold(s string) string {
return fmt.Sprintf("\033[1m%s\033[0m", s)
}
func Italic(s string) string {
return fmt.Sprintf("\033[3m%s\033[0m", s)
}
func Prompt(label string, value string, edit bool, validator Validator) (string, error) {
return cli.Prompt(label, value, edit, validator)
}
func PromptWithHelp(label string, value string, edit bool, validator Validator, help string) (string, error) {
return cli.PromptWithHelp(label, value, edit, validator, help)
}
func PromptYN(m string) (bool, error) {
return cli.PromptYN(m, true)
}
func PromptBoolean(m string, defaultValue bool) (bool, error) {
return cli.PromptYN(m, defaultValue)
}
func PromptSecret(m string) (string, error) {
return cli.PromptSecret(m)
}
func PromptChoices(m string, value string, choices []string) (int, error) {
return cli.PromptChoices(m, value, choices)
}
func PromptMultipleChoices(m string, choices []string) ([]int, error) {
return cli.PromptMultipleChoices(m, choices)
}
func EmailValidator() Validator {
return func(input string) error {
if input != "" {
_, err := mail.ParseAddress(input)
return err
}
return nil
}
}
func LengthValidator(min int) Validator {
return func(input string) error {
if len(input) >= min {
return nil
}
return errors.New("value is too short")
}
}
func PathOrURLValidator() Validator {
return func(s string) error {
if u, err := url.Parse(s); err == nil && u.Scheme != "" {
return nil
}
v, err := homedir.Expand(s)
if err != nil {
return err
}
v, err = filepath.Abs(v)
if err != nil {
return err
}
info, err := os.Stat(v)
if err != nil {
return err
}
if !info.Mode().IsRegular() {
return errors.New("path is not a file")
}
return nil
}
}
func URLValidator(protocol ...string) Validator {
return func(s string) error {
s = strings.TrimSpace(s)
if s == "" {
return errors.New("url cannot be empty")
}
u, err := url.Parse(s)
if err != nil {
return err
}
scheme := strings.ToLower(u.Scheme)
ok := false
for _, v := range protocol {
if scheme == v {
ok = true
break
}
}
if !ok {
var protos []string
protos = append(protos, protocol...)
return fmt.Errorf("scheme %q is not supported (%v)", scheme, strings.Join(protos, ", "))
}
if u.Host == "" {
return fmt.Errorf("no host specified (%v)", s)
}
return nil
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。