package cmd import ( "context" "fmt" "log" "os" "os/signal" "syscall" "gitea.urkob.com/urko/backblaze-backup/internal/services/backblaze" "gitea.urkob.com/urko/backblaze-backup/kit" "gitea.urkob.com/urko/backblaze-backup/kit/config" "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) var Versions = &cobra.Command{ Use: "versions", Short: "Handle versions of files in Backblaze", Run: func(cmd *cobra.Command, args []string) { log.SetFlags(log.Ldate | log.Lmicroseconds) ctx, cancel := context.WithCancel(signalContext(cmd.Context())) defer cancel() envFile := "" if os.Getenv("BACKBLAZE_ENV") == "dev" { envFile = kit.RootDir() + "/.env" } cfg := config.NewConfig(envFile) logger := logrus.New() logger.SetLevel(logrus.Level(cfg.LogLevel)) bbService, err := backblaze.NewBackBlaze(ctx, logger, cfg.BbId, cfg.BbKey) if err != nil { panic(fmt.Errorf("NewBackBlaze %w", err)) } if err := bbService.ListDuplicateVersions(ctx, cancel); err != nil { panic(fmt.Errorf("bbService.ListDuplicateVersions %w", err)) } }, } func signalContext(ctx context.Context) context.Context { ctx, cancel := context.WithCancel(ctx) sigs := make(chan os.Signal, 1) signal.Notify(sigs, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) go func() { log.Println("listening for shutdown signal") <-sigs log.Println("shutdown signal received") signal.Stop(sigs) close(sigs) cancel() }() return ctx }