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

     1  package cmd
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"os/signal"
     7  	"path/filepath"
     8  	"syscall"
     9  
    10  	"github.com/cosmos/cosmos-sdk/crypto/keyring"
    11  	"github.com/spf13/cobra"
    12  
    13  	"github.com/sunriselayer/sunrise/app"
    14  	"github.com/sunriselayer/sunrise/app/encoding"
    15  
    16  	"github.com/sunriselayer/sunrise-da/nodebuilder"
    17  )
    18  
    19  // Start constructs a CLI command to start Celestia Node daemon of any type with the given flags.
    20  func Start(options ...func(*cobra.Command)) *cobra.Command {
    21  	cmd := &cobra.Command{
    22  		Use: "start",
    23  		Short: `Starts Node daemon. First stopping signal gracefully stops the Node and second terminates it.
    24  Options passed on start override configuration options only on start and are not persisted in config.`,
    25  		Aliases:      []string{"run", "daemon"},
    26  		Args:         cobra.NoArgs,
    27  		SilenceUsage: true,
    28  		RunE: func(cmd *cobra.Command, args []string) (err error) {
    29  			ctx := cmd.Context()
    30  
    31  			// override config with all modifiers passed on start
    32  			cfg := NodeConfig(ctx)
    33  
    34  			storePath := StorePath(ctx)
    35  			keysPath := filepath.Join(storePath, "keys")
    36  
    37  			// construct ring
    38  			// TODO @renaynay: Include option for setting custom `userInput` parameter with
    39  			//  implementation of https://github.com/sunriselayer/sunrise-da/issues/415.
    40  			encConf := encoding.MakeConfig(app.ModuleEncodingRegisters...)
    41  			ring, err := keyring.New(app.Name, cfg.State.KeyringBackend, keysPath, os.Stdin, encConf.Codec)
    42  			if err != nil {
    43  				return err
    44  			}
    45  
    46  			store, err := nodebuilder.OpenStore(storePath, ring)
    47  			if err != nil {
    48  				return err
    49  			}
    50  			defer func() {
    51  				err = errors.Join(err, store.Close())
    52  			}()
    53  
    54  			nd, err := nodebuilder.NewWithConfig(NodeType(ctx), Network(ctx), store, &cfg, NodeOptions(ctx)...)
    55  			if err != nil {
    56  				return err
    57  			}
    58  
    59  			ctx, cancel := signal.NotifyContext(cmd.Context(), syscall.SIGINT, syscall.SIGTERM)
    60  			defer cancel()
    61  			err = nd.Start(ctx)
    62  			if err != nil {
    63  				return err
    64  			}
    65  
    66  			<-ctx.Done()
    67  			cancel() // ensure we stop reading more signals for start context
    68  
    69  			ctx, cancel = signal.NotifyContext(cmd.Context(), syscall.SIGINT, syscall.SIGTERM)
    70  			defer cancel()
    71  			return nd.Stop(ctx)
    72  		},
    73  	}
    74  	// Apply each passed option to the command
    75  	for _, option := range options {
    76  		option(cmd)
    77  	}
    78  	return cmd
    79  }