cosmossdk.io/client/v2@v2.0.0-beta.1/autocli/builder.go (about)

     1  package autocli
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/spf13/cobra"
     7  	"google.golang.org/grpc"
     8  
     9  	"cosmossdk.io/client/v2/autocli/flag"
    10  	"cosmossdk.io/client/v2/autocli/keyring"
    11  
    12  	"github.com/cosmos/cosmos-sdk/client"
    13  	authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
    14  )
    15  
    16  // Builder manages options for building CLI commands.
    17  type Builder struct {
    18  	// flag.Builder embeds the flag builder and its options.
    19  	flag.Builder
    20  
    21  	// GetClientConn specifies how CLI commands will resolve a grpc.ClientConnInterface
    22  	// from a given context.
    23  	GetClientConn func(*cobra.Command) (grpc.ClientConnInterface, error)
    24  
    25  	// ClientCtx contains the necessary information needed to execute the commands.
    26  	ClientCtx client.Context
    27  
    28  	// TxConfigOptions is required to support sign mode textual
    29  	TxConfigOpts authtx.ConfigOptions
    30  
    31  	// AddQueryConnFlags and AddTxConnFlags are functions that add flags to query and transaction commands
    32  	AddQueryConnFlags func(*cobra.Command)
    33  	AddTxConnFlags    func(*cobra.Command)
    34  }
    35  
    36  // ValidateAndComplete the builder fields.
    37  // It returns an error if any of the required fields are missing.
    38  // If the Logger is nil, it will be set to a nop logger.
    39  // If the keyring is nil, it will be set to a no keyring.
    40  func (b *Builder) ValidateAndComplete() error {
    41  	if b.Builder.AddressCodec == nil {
    42  		return errors.New("address codec is required in flag builder")
    43  	}
    44  
    45  	if b.Builder.ValidatorAddressCodec == nil {
    46  		return errors.New("validator address codec is required in flag builder")
    47  	}
    48  
    49  	if b.Builder.ConsensusAddressCodec == nil {
    50  		return errors.New("consensus address codec is required in flag builder")
    51  	}
    52  
    53  	if b.Builder.Keyring == nil {
    54  		b.Keyring = keyring.NoKeyring{}
    55  	}
    56  
    57  	if b.Builder.TypeResolver == nil {
    58  		return errors.New("type resolver is required in flag builder")
    59  	}
    60  
    61  	if b.Builder.FileResolver == nil {
    62  		return errors.New("file resolver is required in flag builder")
    63  	}
    64  
    65  	return nil
    66  }