File: options.go

package info (click to toggle)
golang-github-smallstep-cli 0.15.16%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,404 kB
  • sloc: sh: 512; makefile: 99
file content (163 lines) | stat: -rw-r--r-- 3,922 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package ui

import (
	"fmt"
	"regexp"

	"github.com/manifoldco/promptui"
)

type options struct {
	mask            rune
	defaultValue    string
	value           string
	allowEdit       bool
	printTemplate   string
	promptTemplates *promptui.PromptTemplates
	selectTemplates *promptui.SelectTemplates
	validateFunc    promptui.ValidateFunc
}

// apply applies the given options.
func (o *options) apply(opts []Option) *options {
	for _, fn := range opts {
		fn(o)
	}
	return o
}

// valid returns true if the validate function passes on the value.
func (o *options) valid() bool {
	if o.validateFunc == nil {
		return true
	}
	return o.validateFunc(o.value) == nil
}

// getValue validates the value and returns it.
func (o *options) getValue() (string, error) {
	if o.validateFunc == nil {
		return o.value, nil
	}
	if err := o.validateFunc(o.value); err != nil {
		return "", err
	}
	return o.value, nil
}

// getValueBytes validates the value and returns it as a byte slice.
func (o *options) getValueBytes() ([]byte, error) {
	if o.validateFunc == nil {
		return []byte(o.value), nil
	}
	if err := o.validateFunc(o.value); err != nil {
		return nil, err
	}
	return []byte(o.value), nil
}

// Option is the type of the functions that modify the prompt options.
type Option func(*options)

func extractOptions(args []interface{}) (opts []Option, rest []interface{}) {
	rest = args[:0]
	for _, arg := range args {
		if o, ok := arg.(Option); ok {
			opts = append(opts, o)
		} else {
			rest = append(rest, arg)
		}
	}
	return
}

// WithMask adds a mask to a prompt.
func WithMask(r rune) Option {
	return func(o *options) {
		o.mask = r
	}
}

// WithDefaultValue adds a custom string as the default value.
func WithDefaultValue(s string) Option {
	return func(o *options) {
		o.defaultValue = s
	}
}

// WithValue sets a custom string as the result of a prompt. If value is set,
// the prompt won't be displayed.
func WithValue(value string) Option {
	return func(o *options) {
		o.value = value
	}
}

// WithAllowEdit if true, let's the user edit the default value set.
func WithAllowEdit(b bool) Option {
	return func(o *options) {
		o.allowEdit = b
	}
}

// WithPrintTemplate sets the template to use on the print methods.
func WithPrintTemplate(template string) Option {
	return func(o *options) {
		o.printTemplate = template
	}
}

// WithPromptTemplates adds a custom template to a prompt.
func WithPromptTemplates(t *promptui.PromptTemplates) Option {
	return func(o *options) {
		o.promptTemplates = t
	}
}

// WithSelectTemplates adds a custom template to a select.
func WithSelectTemplates(t *promptui.SelectTemplates) Option {
	return func(o *options) {
		o.selectTemplates = t
	}
}

// WithValidateFunc adds a custom validation function to a prompt.
func WithValidateFunc(fn func(string) error) Option {
	return func(o *options) {
		o.validateFunc = fn
	}
}

// WithValidateNotEmpty adds a custom validation function to a prompt that
// checks that the propted string is not empty.
func WithValidateNotEmpty() Option {
	return WithValidateFunc(NotEmpty())
}

// WithValidateYesNo adds a custom validation function to a prompt for a Yes/No
// prompt.
func WithValidateYesNo() Option {
	return WithValidateFunc(YesNo())
}

// WithRichPrompt add the template option with rich templates.
func WithRichPrompt() Option {
	return WithPromptTemplates(PromptTemplates())
}

// WithSimplePrompt add the template option with simple templates.
func WithSimplePrompt() Option {
	return WithPromptTemplates(SimplePromptTemplates())
}

// WithValidateRegexp checks a prompt answer with a regular expression. If the
// regular expression is not a valid one, the option will panic.
func WithValidateRegexp(re string) Option {
	rx := regexp.MustCompile(re)
	return WithValidateFunc(func(s string) error {
		if rx.MatchString(s) {
			return nil
		}
		return fmt.Errorf("%s does not match the regular expresion %s", s, re)
	})
}