Skip to content

Commit

Permalink
Improve readability of the errIsValidTimeout logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dpritchett committed Oct 31, 2024
1 parent af3cbfb commit b479523
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions confirm/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ func (o Options) Run() error {
WithShowHelp(o.ShowHelp).
Run()

// Should the form time out it will return an error with this string:
const timeoutErrMsg = "timeout"
if err != nil {
allowErr := o.errIsValidTimeout(err)

if err != nil && err.Error() != timeoutErrMsg {
return fmt.Errorf("unable to run confirm: %w", err)
if !allowErr {
return fmt.Errorf("unable to run confirm: %w", err)
}
}

if !choice {
Expand All @@ -44,3 +45,11 @@ func (o Options) Run() error {

return nil
}

// errIsValidTimeout returns true iff an error is a timeout error AND the user specified a nonzero o.Timeout.
func (o Options) errIsValidTimeout(err error) bool {
errWasTimeout := err.Error() == huh.ErrTimeout.Error()
timeoutsExpected := o.Timeout > 0

return errWasTimeout && timeoutsExpected
}

0 comments on commit b479523

Please sign in to comment.