Skip to content

Commit

Permalink
fix: fix cli context misc (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent authored Nov 6, 2023
2 parents c9d2ce0 + d0441bb commit 6aa2d0d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
6 changes: 3 additions & 3 deletions exp/cli/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ func TestCommand(t *testing.T) {
{
Name: "sub-sub-cmd",
Func: func(ctx context.Context, remainingArgs []string) error {
cmd, ok := FromContext(ctx)
if !ok {
return errorz.Errorf("failed to get command from context")
cmd, err := FromContext(ctx)
if err != nil {
return errorz.Errorf("FromContext: %w", err)
}
if cmd.Name != "sub-sub-cmd" {
return errorz.Errorf("unexpected command name: %s", cmd.Name)
Expand Down
8 changes: 6 additions & 2 deletions exp/cli/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ func WithContext(ctx context.Context, cmd *Command) context.Context {
return context.WithValue(ctx, contextKeyCommand{}, cmd)
}

func FromContext(ctx context.Context) (cmd *Command, ok bool) {
func FromContext(ctx context.Context) (*Command, error) {
c, ok := ctx.Value(contextKeyCommand{}).(*Command)
return c, ok
if !ok {
return nil, ErrCommandNotSetInContext
}

return c, nil
}
21 changes: 21 additions & 0 deletions exp/cli/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cliz_test

import (
"context"
"errors"
"testing"

cliz "github.com/kunitsucom/util.go/exp/cli"
)

func TestFromContext(t *testing.T) {
t.Parallel()

t.Run("failure,ErrCommandNotSetInContext", func(t *testing.T) {
t.Parallel()

if _, err := cliz.FromContext(context.Background()); !errors.Is(err, cliz.ErrCommandNotSetInContext) {
t.Errorf("❌: err != cliz.ErrCommandNotSetInContext: %+v", err)
}
})
}
17 changes: 9 additions & 8 deletions exp/cli/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package cliz
import "errors"

var (
ErrHelp = errors.New("help requested")
ErrCommandFuncNotSet = errors.New("command func not set")
ErrMissingOptionValue = errors.New("missing option value")
ErrOptionRequired = errors.New("option required")
ErrUnknownOption = errors.New("unknown option")
ErrInvalidOptionType = errors.New("invalid option type")
ErrDuplicateOptionName = errors.New("duplicate option name")
ErrDuplicateSubCommand = errors.New("duplicate sub command")
ErrHelp = errors.New("help requested")
ErrCommandNotSetInContext = errors.New("command not set in context")
ErrCommandFuncNotSet = errors.New("command func not set")
ErrMissingOptionValue = errors.New("missing option value")
ErrOptionRequired = errors.New("option required")
ErrUnknownOption = errors.New("unknown option")
ErrInvalidOptionType = errors.New("invalid option type")
ErrDuplicateOptionName = errors.New("duplicate option name")
ErrDuplicateSubCommand = errors.New("duplicate sub command")
)

0 comments on commit 6aa2d0d

Please sign in to comment.