Skip to content

Commit b1094fc

Browse files
committed
rename to just validate
1 parent 262ecf9 commit b1094fc

File tree

4 files changed

+18
-33
lines changed

4 files changed

+18
-33
lines changed

.golangci.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ linters-settings:
138138
- name: blank-imports
139139
- name: bool-literal-in-expr
140140
- name: call-to-gc
141-
- name: confusing-naming
141+
# This inconsistently marks struct methods as confusing if they have
142+
# the same name... which for interfaces they do.
143+
# - name: confusing-naming
142144
- name: confusing-results
143145
- name: constant-logical-expr
144146
- name: context-as-argument

cli/clibase/values.go

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,25 @@ import (
2020
// of the value after or before it has been set.
2121
type Validator[T pflag.Value] struct {
2222
Value T
23-
// validateBefore is called before the value is set.
24-
validateBefore func(input string) error
25-
// validateAfter is called after the value is set.
26-
validateAfter func(T) error
23+
// validate is called after the value is set.
24+
validate func(T) error
2725
}
2826

29-
func Validate[T pflag.Value](opt T) *Validator[T] {
30-
return &Validator[T]{Value: opt}
31-
}
32-
33-
func (i *Validator[T]) Before(fn func(input string) error) *Validator[T] {
34-
i.validateBefore = fn
35-
return i
36-
}
37-
38-
func (i *Validator[T]) After(fn func(value T) error) *Validator[T] {
39-
i.validateAfter = fn
40-
return i
27+
func Validate[T pflag.Value](opt T, validate func(value T) error) *Validator[T] {
28+
return &Validator[T]{Value: opt, validate: validate}
4129
}
4230

4331
func (i *Validator[T]) String() string {
4432
return i.Value.String()
4533
}
4634

4735
func (i *Validator[T]) Set(input string) error {
48-
if i.validateBefore != nil {
49-
err := i.validateBefore(input)
50-
if err != nil {
51-
return err
52-
}
53-
}
54-
5536
err := i.Value.Set(input)
5637
if err != nil {
5738
return err
5839
}
59-
if i.validateAfter != nil {
60-
err = i.validateAfter(i.Value)
40+
if i.validate != nil {
41+
err = i.validate(i.Value)
6142
if err != nil {
6243
return err
6344
}

enterprise/cli/proxyserver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (*RootCmd) proxyServer() *clibase.Cmd {
7878
Env: "CODER_PRIMARY_ACCESS_URL",
7979
YAML: "primaryAccessURL",
8080
Required: true,
81-
Value: clibase.Validate(&primaryAccessURL).After(func(value *clibase.URL) error {
81+
Value: clibase.Validate(&primaryAccessURL, func(value *clibase.URL) error {
8282
if !(value.Scheme == "http" || value.Scheme == "https") {
8383
return xerrors.Errorf("'--primary-access-url' value must be http or https: url=%s", primaryAccessURL.String())
8484
}

enterprise/cli/workspaceproxy.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ func (r *RootCmd) createProxy() *clibase.Cmd {
235235
noPrompts bool
236236
formatter = newUpdateProxyResponseFormatter()
237237
)
238-
validateIcon := func(s string) error {
239-
if !(strings.HasPrefix(s, "/emojis/") || strings.HasPrefix(s, "http")) {
238+
validateIcon := func(s *clibase.String) error {
239+
if !(strings.HasPrefix(s.Value(), "/emojis/") || strings.HasPrefix(s, "http")) {
240240
return xerrors.New("icon must be a relative path to an emoji or a publicly hosted image URL")
241241
}
242242
return nil
@@ -274,9 +274,11 @@ func (r *RootCmd) createProxy() *clibase.Cmd {
274274

275275
if proxyIcon == "" && !noPrompts {
276276
proxyIcon, err = cliui.Prompt(inv, cliui.PromptOptions{
277-
Text: "Icon URL:",
278-
Default: "/emojis/1f5fa.png",
279-
Validate: validateIcon,
277+
Text: "Icon URL:",
278+
Default: "/emojis/1f5fa.png",
279+
Validate: func(s string) error {
280+
return validateIcon(clibase.StringOf(&s))
281+
},
280282
})
281283
if err != nil {
282284
return err
@@ -320,7 +322,7 @@ func (r *RootCmd) createProxy() *clibase.Cmd {
320322
clibase.Option{
321323
Flag: "icon",
322324
Description: "Display icon of the proxy.",
323-
Value: clibase.Validate(clibase.StringOf(&proxyIcon)).Before(validateIcon),
325+
Value: clibase.Validate(clibase.StringOf(&proxyIcon), validateIcon),
324326
},
325327
clibase.Option{
326328
Flag: "no-prompt",

0 commit comments

Comments
 (0)