github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/cmd/br/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"os/signal"
     8  	"syscall"
     9  
    10  	"github.com/pingcap/log"
    11  	"github.com/spf13/cobra"
    12  	"go.uber.org/zap"
    13  )
    14  
    15  func main() {
    16  	gCtx := context.Background()
    17  	ctx, cancel := context.WithCancel(gCtx)
    18  	defer cancel()
    19  
    20  	sc := make(chan os.Signal, 1)
    21  	signal.Notify(sc,
    22  		syscall.SIGHUP,
    23  		syscall.SIGINT,
    24  		syscall.SIGTERM,
    25  		syscall.SIGQUIT)
    26  
    27  	go func() {
    28  		sig := <-sc
    29  		fmt.Printf("\nGot signal [%v] to exit.\n", sig)
    30  		log.Warn("received signal to exit", zap.Stringer("signal", sig))
    31  		cancel()
    32  		fmt.Fprintln(os.Stderr, "gracefully shuting down, press ^C again to force exit")
    33  		<-sc
    34  		// Even user use SIGTERM to exit, there isn't any checkpoint for resuming,
    35  		// hence returning fail exit code.
    36  		os.Exit(1)
    37  	}()
    38  
    39  	rootCmd := &cobra.Command{
    40  		Use:              "br",
    41  		Short:            "br is a TiDB/TiKV cluster backup restore tool.",
    42  		TraverseChildren: true,
    43  		SilenceUsage:     true,
    44  	}
    45  	AddFlags(rootCmd)
    46  	SetDefaultContext(ctx)
    47  	rootCmd.AddCommand(
    48  		NewDebugCommand(),
    49  		NewBackupCommand(),
    50  		NewRestoreCommand(),
    51  	)
    52  	// Ouputs cmd.Print to stdout.
    53  	rootCmd.SetOut(os.Stdout)
    54  
    55  	rootCmd.SetArgs(os.Args[1:])
    56  	if err := rootCmd.Execute(); err != nil {
    57  		cancel()
    58  		log.Error("br failed", zap.Error(err))
    59  		os.Exit(1) // nolint:gocritic
    60  	}
    61  }