Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add GetBusFromCmd() CLI helper
Browse files Browse the repository at this point in the history
bryanchriswhite committed Jul 11, 2023
1 parent 9bb2e02 commit 4518327
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
@@ -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
14 changes: 14 additions & 0 deletions app/client/cli/helpers/context.go
Original file line number Diff line number Diff line change
@@ -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

@@ -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 4518327

Please sign in to comment.