github.com/ipni/storetheindex@v0.8.30/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"os/signal"
     8  	"syscall"
     9  
    10  	"github.com/ipni/storetheindex/command"
    11  	"github.com/urfave/cli/v2"
    12  )
    13  
    14  func main() {
    15  	// Set up a context that is canceled when the command is interrupted
    16  	ctx, cancel := context.WithCancel(context.Background())
    17  	defer cancel()
    18  
    19  	// Set up a signal handler to cancel the context
    20  	go func() {
    21  		interrupt := make(chan os.Signal, 1)
    22  		signal.Notify(interrupt, syscall.SIGTERM, syscall.SIGINT)
    23  		select {
    24  		case <-interrupt:
    25  			cancel()
    26  			fmt.Println("Received interrupt signal, shutting down...")
    27  			fmt.Println("(Hit ctrl-c again to force-shutdown the daemon.)")
    28  		case <-ctx.Done():
    29  		}
    30  		// Allow any further SIGTERM or SIGINT to kill process
    31  		signal.Stop(interrupt)
    32  	}()
    33  
    34  	app := &cli.App{
    35  		Name:    "storetheindex",
    36  		Usage:   "IPNI indexer implementation",
    37  		Version: version,
    38  		Commands: []*cli.Command{
    39  			command.AdminCmd,
    40  			command.AssignerCmd,
    41  			command.DaemonCmd,
    42  			command.GCCmd,
    43  			command.InitCmd,
    44  			command.LoadtestCmd,
    45  			command.LogCmd,
    46  			command.UpdateMirrorCmd,
    47  		},
    48  	}
    49  
    50  	if err := app.RunContext(ctx, os.Args); err != nil {
    51  		fmt.Fprintln(os.Stderr, err)
    52  		os.Exit(1)
    53  	}
    54  }