backblaze-backup/cmd/duplicate_versions.go

53 lines
1.1 KiB
Go
Raw Normal View History

package cmd
import (
"context"
"log"
"os"
"os/signal"
"syscall"
"gitea.urkob.com/urko/backblaze-backup/internal/services"
"gitea.urkob.com/urko/backblaze-backup/kit/config"
"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.Lmicroseconds)
ctx, cancel := context.WithCancel(signalContext(cmd.Context()))
defer cancel()
envFile := ""
if os.Getenv("BACKBLAZE_ENV") == "dev" {
envFile = ".env"
}
cfg := config.NewConfig(envFile)
bbService := services.NewBackBlaze(cfg.BbId, cfg.BbKey)
if err := bbService.ListDuplicateVersions(ctx); err != nil {
log.Fatalln("bbService.ListDuplicateVersions()", 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
}