github.com/sunriselayer/sunrise-da@v0.13.1-sr3/cmd/env.go (about)

     1  package cmd
     2  
     3  import (
     4  	"context"
     5  
     6  	logging "github.com/ipfs/go-log/v2"
     7  	"go.uber.org/fx"
     8  
     9  	"github.com/sunriselayer/sunrise-da/nodebuilder"
    10  	"github.com/sunriselayer/sunrise-da/nodebuilder/node"
    11  	"github.com/sunriselayer/sunrise-da/nodebuilder/p2p"
    12  )
    13  
    14  var log = logging.Logger("cmd")
    15  
    16  // NodeType reads the node type from the context.
    17  func NodeType(ctx context.Context) node.Type {
    18  	return ctx.Value(nodeTypeKey{}).(node.Type)
    19  }
    20  
    21  // Network reads the node type from the context.
    22  func Network(ctx context.Context) p2p.Network {
    23  	return ctx.Value(networkKey{}).(p2p.Network)
    24  }
    25  
    26  // StorePath reads the store path from the context.
    27  func StorePath(ctx context.Context) string {
    28  	return ctx.Value(storePathKey{}).(string)
    29  }
    30  
    31  // NodeConfig reads the node config from the context.
    32  func NodeConfig(ctx context.Context) nodebuilder.Config {
    33  	cfg, ok := ctx.Value(configKey{}).(nodebuilder.Config)
    34  	if !ok {
    35  		nodeType := NodeType(ctx)
    36  		cfg = *nodebuilder.DefaultConfig(nodeType)
    37  	}
    38  	return cfg
    39  }
    40  
    41  // WithNodeType sets the node type in the given context.
    42  func WithNodeType(ctx context.Context, tp node.Type) context.Context {
    43  	return context.WithValue(ctx, nodeTypeKey{}, tp)
    44  }
    45  
    46  // WithNetwork sets the network in the given context.
    47  func WithNetwork(ctx context.Context, network p2p.Network) context.Context {
    48  	return context.WithValue(ctx, networkKey{}, network)
    49  }
    50  
    51  // WithStorePath sets Store Path in the given context.
    52  func WithStorePath(ctx context.Context, storePath string) context.Context {
    53  	return context.WithValue(ctx, storePathKey{}, storePath)
    54  }
    55  
    56  // NodeOptions returns config options parsed from Environment(Flags, ENV vars, etc)
    57  func NodeOptions(ctx context.Context) []fx.Option {
    58  	options, ok := ctx.Value(optionsKey{}).([]fx.Option)
    59  	if !ok {
    60  		return []fx.Option{}
    61  	}
    62  	return options
    63  }
    64  
    65  // WithNodeOptions add new options to Env.
    66  func WithNodeOptions(ctx context.Context, opts ...fx.Option) context.Context {
    67  	options := NodeOptions(ctx)
    68  	return context.WithValue(ctx, optionsKey{}, append(options, opts...))
    69  }
    70  
    71  // WithNodeConfig sets the node config in the Env.
    72  func WithNodeConfig(ctx context.Context, config *nodebuilder.Config) context.Context {
    73  	return context.WithValue(ctx, configKey{}, *config)
    74  }
    75  
    76  type (
    77  	optionsKey   struct{}
    78  	configKey    struct{}
    79  	storePathKey struct{}
    80  	nodeTypeKey  struct{}
    81  	networkKey   struct{}
    82  )