backblaze-backup/cmd/cleanup.go

46 lines
1.1 KiB
Go

package cmd
import (
"context"
"fmt"
"log"
"os"
"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 Cleanup = &cobra.Command{
Use: "cleanup",
Short: "Handle clenaup for multiple versions of file in Backblaze",
Run: func(cmd *cobra.Command, args []string) {
log.SetFlags(log.Ldate | log.Lmicroseconds)
ctx, cancel := context.WithCancel(signalContext(cmd.Context()))
defer cancel()
bucketName, err := cmd.Flags().GetString("bucket")
if err != nil {
panic(fmt.Errorf("bucket %w", err))
}
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.CleanUp(ctx, cancel, bucketName); err != nil {
panic(fmt.Errorf("bbService.CleanUp %w", err))
}
},
}