Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add validation errors
  • Loading branch information
Emyrk committed Sep 29, 2023
commit 1af63f9c13d043974db1eb396bd4e505a442a00b
26 changes: 26 additions & 0 deletions cli/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"fmt"
"net/http"
"net/http/httptest"
"os"
Expand All @@ -19,6 +20,8 @@ func (RootCmd) errorExample() *clibase.Cmd {
},
}
}

// Make an api error
recorder := httptest.NewRecorder()
recorder.WriteHeader(http.StatusBadRequest)
resp := recorder.Result()
Expand All @@ -36,6 +39,9 @@ func (RootCmd) errorExample() *clibase.Cmd {
}
apiError.(*codersdk.Error).Helper = "Have you tried turning it off and on again?"

// Some flags
var magicWord clibase.String

cmd := &clibase.Cmd{
Use: "example-error",
Short: "Shows what different error messages look like",
Expand Down Expand Up @@ -65,6 +71,26 @@ func (RootCmd) errorExample() *clibase.Cmd {
return xerrors.Errorf("some error: %w", errorWithStackTrace())
},
},

{
Use: "validation",
Options: clibase.OptionSet{
clibase.Option{
Name: "magic-word",
Description: "Take a good guess.",
Required: true,
Flag: "magic-word",
Default: "",
Value: clibase.Validate(&magicWord, func(value *clibase.String) error {
return xerrors.Errorf("magic word is incorrect")
}),
},
},
Handler: func(i *clibase.Invocation) error {
_, _ = fmt.Fprint(i.Stdout, "Try setting the --magic-word flag\n")
return nil
},
},
},
}

Expand Down