2023-07-18 12:05:22 +02:00
|
|
|
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)
|
2023-07-18 14:23:33 +02:00
|
|
|
if err := bbService.ListDuplicateVersions(ctx, cancel); err != nil {
|
2023-07-18 12:05:22 +02:00
|
|
|
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
|
|
|
|
}
|