Skip to content

Commit 1681c99

Browse files
committed
feat: add required flag to cli options
1 parent 9572e90 commit 1681c99

File tree

4 files changed

+36
-19
lines changed

4 files changed

+36
-19
lines changed

cli/clibase/cmd.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,18 @@ func (inv *Invocation) run(state *runState) error {
311311
return xerrors.Errorf("setting defaults: %w", err)
312312
}
313313

314+
// All options should be set. Check all required options have sources,
315+
// meaning they were set by the user in some way (env, flag, etc).
316+
var missing []string
317+
for _, opt := range inv.Command.Options {
318+
if opt.Required && opt.ValueSource == ValueSourceNone {
319+
missing = append(missing, opt.Flag)
320+
}
321+
}
322+
if len(missing) > 0 {
323+
return xerrors.Errorf("Missing values for the required flags: %s", strings.Join(missing, ", "))
324+
}
325+
314326
// Run child command if found (next child only)
315327
// We must do subcommand detection after flag parsing so we don't mistake flag
316328
// values for subcommand names.

cli/clibase/option.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ const (
2323
type Option struct {
2424
Name string `json:"name,omitempty"`
2525
Description string `json:"description,omitempty"`
26+
// Required means this value must be set by some means. It requires
27+
// `ValueSource != ValueSourceNone`
28+
// If `Default` is set, then `Required` is ignored.
29+
Required bool `json:"required,omitempty"`
2630

2731
// Flag is the long name of the flag used to configure this option. If unset,
2832
// flag configuring is disabled.

cli/clibase/values.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,23 @@ 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+
// 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
2727
}
2828

2929
func Validate[T pflag.Value](opt T) *Validator[T] {
3030
return &Validator[T]{Value: opt}
3131
}
3232

3333
func (i *Validator[T]) Before(fn func(input string) error) *Validator[T] {
34-
i.ValidateBefore = fn
34+
i.validateBefore = fn
3535
return i
3636
}
3737

3838
func (i *Validator[T]) After(fn func(value T) error) *Validator[T] {
39-
i.ValidateAfter = fn
39+
i.validateAfter = fn
4040
return i
4141
}
4242

@@ -45,8 +45,8 @@ func (i *Validator[T]) String() string {
4545
}
4646

4747
func (i *Validator[T]) Set(input string) error {
48-
if i.ValidateBefore != nil {
49-
err := i.ValidateBefore(input)
48+
if i.validateBefore != nil {
49+
err := i.validateBefore(input)
5050
if err != nil {
5151
return err
5252
}
@@ -56,8 +56,8 @@ func (i *Validator[T]) Set(input string) error {
5656
if err != nil {
5757
return err
5858
}
59-
if i.ValidateAfter != nil {
60-
err = i.ValidateAfter(i.Value)
59+
if i.validateAfter != nil {
60+
err = i.validateAfter(i.Value)
6161
if err != nil {
6262
return err
6363
}

enterprise/cli/proxyserver.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (*RootCmd) proxyServer() *clibase.Cmd {
6565
Flag: "proxy-session-token",
6666
Env: "CODER_PROXY_SESSION_TOKEN",
6767
YAML: "proxySessionToken",
68-
Default: "",
68+
Required: true,
6969
Value: &proxySessionToken,
7070
Group: &externalProxyOptionGroup,
7171
Hidden: false,
@@ -77,10 +77,15 @@ func (*RootCmd) proxyServer() *clibase.Cmd {
7777
Flag: "primary-access-url",
7878
Env: "CODER_PRIMARY_ACCESS_URL",
7979
YAML: "primaryAccessURL",
80-
Default: "",
81-
Value: &primaryAccessURL,
82-
Group: &externalProxyOptionGroup,
83-
Hidden: false,
80+
Required: true,
81+
Value: clibase.Validate(&primaryAccessURL).After(func(value *clibase.URL) error {
82+
if !(value.Scheme == "http" || value.Scheme == "https") {
83+
return xerrors.Errorf("'--primary-access-url' value must be http or https: url=%s", primaryAccessURL.String())
84+
}
85+
return nil
86+
}),
87+
Group: &externalProxyOptionGroup,
88+
Hidden: false,
8489
},
8590
)
8691

@@ -94,10 +99,6 @@ func (*RootCmd) proxyServer() *clibase.Cmd {
9499
clibase.RequireNArgs(0),
95100
),
96101
Handler: func(inv *clibase.Invocation) error {
97-
if !(primaryAccessURL.Scheme == "http" || primaryAccessURL.Scheme == "https") {
98-
return xerrors.Errorf("'--primary-access-url' value must be http or https: url=%s", primaryAccessURL.String())
99-
}
100-
101102
var closers closers
102103
// Main command context for managing cancellation of running
103104
// services.

0 commit comments

Comments
 (0)