From b8904da3c5ee6031a07757301fb498929c344195 Mon Sep 17 00:00:00 2001 From: Bryan White Date: Tue, 11 Jul 2023 09:28:28 +0200 Subject: [PATCH] chore: add `GetBusFromCmd()` CLI helper --- app/client/cli/helpers/common.go | 6 +++--- app/client/cli/helpers/context.go | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/client/cli/helpers/common.go b/app/client/cli/helpers/common.go index be9d75250..b89f79ba9 100644 --- a/app/client/cli/helpers/common.go +++ b/app/client/cli/helpers/common.go @@ -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 diff --git a/app/client/cli/helpers/context.go b/app/client/cli/helpers/context.go index f9f3f4549..f1494c5ac 100644 --- a/app/client/cli/helpers/context.go +++ b/app/client/cli/helpers/context.go @@ -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 +}