mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: add cli first class validation (#8374)
* feat: add cli first class validation * feat: add required flag to cli options * Add unit test to catch invalid and missing flag
This commit is contained in:
@@ -65,7 +65,7 @@ func (*RootCmd) proxyServer() *clibase.Cmd {
|
||||
Flag: "proxy-session-token",
|
||||
Env: "CODER_PROXY_SESSION_TOKEN",
|
||||
YAML: "proxySessionToken",
|
||||
Default: "",
|
||||
Required: true,
|
||||
Value: &proxySessionToken,
|
||||
Group: &externalProxyOptionGroup,
|
||||
Hidden: false,
|
||||
@@ -77,10 +77,15 @@ func (*RootCmd) proxyServer() *clibase.Cmd {
|
||||
Flag: "primary-access-url",
|
||||
Env: "CODER_PRIMARY_ACCESS_URL",
|
||||
YAML: "primaryAccessURL",
|
||||
Default: "",
|
||||
Value: &primaryAccessURL,
|
||||
Group: &externalProxyOptionGroup,
|
||||
Hidden: false,
|
||||
Required: true,
|
||||
Value: clibase.Validate(&primaryAccessURL, func(value *clibase.URL) error {
|
||||
if !(value.Scheme == "http" || value.Scheme == "https") {
|
||||
return xerrors.Errorf("'--primary-access-url' value must be http or https: url=%s", primaryAccessURL.String())
|
||||
}
|
||||
return nil
|
||||
}),
|
||||
Group: &externalProxyOptionGroup,
|
||||
Hidden: false,
|
||||
},
|
||||
)
|
||||
|
||||
@@ -94,10 +99,6 @@ func (*RootCmd) proxyServer() *clibase.Cmd {
|
||||
clibase.RequireNArgs(0),
|
||||
),
|
||||
Handler: func(inv *clibase.Invocation) error {
|
||||
if !(primaryAccessURL.Scheme == "http" || primaryAccessURL.Scheme == "https") {
|
||||
return xerrors.Errorf("'--primary-access-url' value must be http or https: url=%s", primaryAccessURL.String())
|
||||
}
|
||||
|
||||
var closers closers
|
||||
// Main command context for managing cancellation of running
|
||||
// services.
|
||||
|
||||
@@ -235,6 +235,12 @@ func (r *RootCmd) createProxy() *clibase.Cmd {
|
||||
noPrompts bool
|
||||
formatter = newUpdateProxyResponseFormatter()
|
||||
)
|
||||
validateIcon := func(s *clibase.String) error {
|
||||
if !(strings.HasPrefix(s.Value(), "/emojis/") || strings.HasPrefix(s.Value(), "http")) {
|
||||
return xerrors.New("icon must be a relative path to an emoji or a publicly hosted image URL")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
client := new(codersdk.Client)
|
||||
cmd := &clibase.Cmd{
|
||||
@@ -271,10 +277,7 @@ func (r *RootCmd) createProxy() *clibase.Cmd {
|
||||
Text: "Icon URL:",
|
||||
Default: "/emojis/1f5fa.png",
|
||||
Validate: func(s string) error {
|
||||
if !(strings.HasPrefix(s, "/emojis/") || strings.HasPrefix(s, "http")) {
|
||||
return xerrors.New("icon must be a relative path to an emoji or a publicly hosted image URL")
|
||||
}
|
||||
return nil
|
||||
return validateIcon(clibase.StringOf(&s))
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
@@ -319,7 +322,7 @@ func (r *RootCmd) createProxy() *clibase.Cmd {
|
||||
clibase.Option{
|
||||
Flag: "icon",
|
||||
Description: "Display icon of the proxy.",
|
||||
Value: clibase.StringOf(&proxyIcon),
|
||||
Value: clibase.Validate(clibase.StringOf(&proxyIcon), validateIcon),
|
||||
},
|
||||
clibase.Option{
|
||||
Flag: "no-prompt",
|
||||
|
||||
Reference in New Issue
Block a user