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

     1  package cmd
     2  
     3  import (
     4  	"encoding/base64"
     5  	"encoding/hex"
     6  	"encoding/json"
     7  	"fmt"
     8  	"os"
     9  	"strings"
    10  
    11  	"github.com/spf13/cobra"
    12  	flag "github.com/spf13/pflag"
    13  
    14  	"github.com/sunriselayer/sunrise-da/nodebuilder/core"
    15  	"github.com/sunriselayer/sunrise-da/nodebuilder/gateway"
    16  	"github.com/sunriselayer/sunrise-da/nodebuilder/header"
    17  	"github.com/sunriselayer/sunrise-da/nodebuilder/node"
    18  	"github.com/sunriselayer/sunrise-da/nodebuilder/p2p"
    19  	rpc_cfg "github.com/sunriselayer/sunrise-da/nodebuilder/rpc"
    20  	"github.com/sunriselayer/sunrise-da/nodebuilder/state"
    21  	"github.com/sunriselayer/sunrise-da/share"
    22  )
    23  
    24  func PrintOutput(data interface{}, err error, formatData func(interface{}) interface{}) error {
    25  	switch {
    26  	case err != nil:
    27  		data = err.Error()
    28  	case formatData != nil:
    29  		data = formatData(data)
    30  	}
    31  
    32  	resp := struct {
    33  		Result interface{} `json:"result"`
    34  	}{
    35  		Result: data,
    36  	}
    37  
    38  	bytes, err := json.MarshalIndent(resp, "", "  ")
    39  	if err != nil {
    40  		return err
    41  	}
    42  	fmt.Fprintln(os.Stdout, string(bytes))
    43  	return nil
    44  }
    45  
    46  // ParseV0Namespace parses a namespace from a base64 or hex string. The param
    47  // is expected to be the user-specified portion of a v0 namespace ID (i.e. the
    48  // last 10 bytes).
    49  func ParseV0Namespace(param string) (share.Namespace, error) {
    50  	userBytes, err := DecodeToBytes(param)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	// if the namespace ID is <= 10 bytes, left pad it with 0s
    56  	return share.NewBlobNamespaceV0(userBytes)
    57  }
    58  
    59  // DecodeToBytes decodes a Base64 or hex input string into a byte slice.
    60  func DecodeToBytes(param string) ([]byte, error) {
    61  	if strings.HasPrefix(param, "0x") {
    62  		decoded, err := hex.DecodeString(param[2:])
    63  		if err != nil {
    64  			return nil, fmt.Errorf("error decoding namespace ID: %w", err)
    65  		}
    66  		return decoded, nil
    67  	}
    68  	// otherwise, it's just a base64 string
    69  	decoded, err := base64.StdEncoding.DecodeString(param)
    70  	if err != nil {
    71  		return nil, fmt.Errorf("error decoding namespace ID: %w", err)
    72  	}
    73  	return decoded, nil
    74  }
    75  
    76  func PersistentPreRunEnv(cmd *cobra.Command, nodeType node.Type, _ []string) error {
    77  	var (
    78  		ctx = cmd.Context()
    79  		err error
    80  	)
    81  
    82  	ctx = WithNodeType(ctx, nodeType)
    83  
    84  	parsedNetwork, err := p2p.ParseNetwork(cmd)
    85  	if err != nil {
    86  		return err
    87  	}
    88  	ctx = WithNetwork(ctx, parsedNetwork)
    89  
    90  	// loads existing config into the environment
    91  	ctx, err = ParseNodeFlags(ctx, cmd, Network(ctx))
    92  	if err != nil {
    93  		return err
    94  	}
    95  
    96  	cfg := NodeConfig(ctx)
    97  
    98  	err = p2p.ParseFlags(cmd, &cfg.P2P)
    99  	if err != nil {
   100  		return err
   101  	}
   102  
   103  	err = core.ParseFlags(cmd, &cfg.Core)
   104  	if err != nil {
   105  		return err
   106  	}
   107  
   108  	if nodeType != node.Bridge {
   109  		err = header.ParseFlags(cmd, &cfg.Header)
   110  		if err != nil {
   111  			return err
   112  		}
   113  	}
   114  
   115  	ctx, err = ParseMiscFlags(ctx, cmd)
   116  	if err != nil {
   117  		return err
   118  	}
   119  
   120  	rpc_cfg.ParseFlags(cmd, &cfg.RPC)
   121  	gateway.ParseFlags(cmd, &cfg.Gateway)
   122  	state.ParseFlags(cmd, &cfg.State)
   123  
   124  	// set config
   125  	ctx = WithNodeConfig(ctx, &cfg)
   126  	cmd.SetContext(ctx)
   127  	return nil
   128  }
   129  
   130  // WithFlagSet adds the given flagset to the command.
   131  func WithFlagSet(fset []*flag.FlagSet) func(*cobra.Command) {
   132  	return func(c *cobra.Command) {
   133  		for _, set := range fset {
   134  			c.Flags().AddFlagSet(set)
   135  		}
   136  	}
   137  }