Skip to content

Commit

Permalink
chore: add GetBusFromCmd() CLI helper
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Jul 12, 2023
1 parent 023c4ac commit b8904da
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 3 additions & 3 deletions app/client/cli/helpers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ var (

// fetchPeerstore retrieves the providers from the CLI context and uses them to retrieve the address book for the current height
func FetchPeerstore(cmd *cobra.Command) (types.Peerstore, error) {
bus, ok := GetValueFromCLIContext[modules.Bus](cmd, BusCLICtxKey)
if !ok || bus == nil {
return nil, errors.New("retrieving bus from CLI context")
bus, err := GetBusFromCmd(cmd)
if err != nil {
return nil, err
}
// TECHDEBT(#810, #811): use `bus.GetPeerstoreProvider()` after peerstore provider
// is retrievable as a proper submodule
Expand Down
14 changes: 14 additions & 0 deletions app/client/cli/helpers/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package helpers

import (
"context"
"fmt"

"github.com/spf13/cobra"

"github.com/pokt-network/pocket/shared/modules"
)

const BusCLICtxKey cliContextKey = "bus"

var ErrCxtFromBus = fmt.Errorf("could not get context from bus")

// NOTE: this is required by the linter, otherwise a simple string constant would have been enough
type cliContextKey string

Expand All @@ -19,3 +24,12 @@ func GetValueFromCLIContext[T any](cmd *cobra.Command, key cliContextKey) (T, bo
value, ok := cmd.Context().Value(key).(T)
return value, ok
}

func GetBusFromCmd(cmd *cobra.Command) (modules.Bus, error) {
bus, ok := GetValueFromCLIContext[modules.Bus](cmd, BusCLICtxKey)
if !ok {
return nil, ErrCxtFromBus
}

return bus, nil
}

0 comments on commit b8904da

Please sign in to comment.