2023-07-18 15:00:12 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-27 21:30:19 +02:00
|
|
|
"fmt"
|
2023-07-18 15:00:12 +02:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
2023-08-27 21:30:19 +02:00
|
|
|
"gitea.urkob.com/urko/backblaze-backup/internal/services/backblaze"
|
|
|
|
"gitea.urkob.com/urko/backblaze-backup/kit"
|
2023-07-18 15:00:12 +02:00
|
|
|
"gitea.urkob.com/urko/backblaze-backup/kit/config"
|
2023-08-27 21:30:19 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2023-07-18 15:00:12 +02:00
|
|
|
"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) {
|
2023-07-26 17:59:14 +02:00
|
|
|
log.SetFlags(log.Ldate | log.Lmicroseconds)
|
2023-07-18 15:00:12 +02:00
|
|
|
ctx, cancel := context.WithCancel(signalContext(cmd.Context()))
|
|
|
|
defer cancel()
|
|
|
|
|
2023-08-12 09:52:04 +02:00
|
|
|
bucketName, err := cmd.Flags().GetString("bucket")
|
|
|
|
if err != nil {
|
2023-08-27 21:30:19 +02:00
|
|
|
panic(fmt.Errorf("bucket %w", err))
|
2023-08-12 09:52:04 +02:00
|
|
|
}
|
|
|
|
|
2023-07-18 15:00:12 +02:00
|
|
|
envFile := ""
|
|
|
|
if os.Getenv("BACKBLAZE_ENV") == "dev" {
|
2023-08-27 21:30:19 +02:00
|
|
|
envFile = kit.RootDir() + "/.env"
|
2023-07-18 15:00:12 +02:00
|
|
|
}
|
|
|
|
cfg := config.NewConfig(envFile)
|
2023-08-27 21:30:19 +02:00
|
|
|
logger := logrus.New()
|
|
|
|
logger.SetLevel(logrus.Level(cfg.LogLevel))
|
2023-07-18 15:00:12 +02:00
|
|
|
|
2023-08-27 21:30:19 +02:00
|
|
|
bbService, err := backblaze.NewBackBlaze(ctx, logger, cfg.BbId, cfg.BbKey)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("NewBackBlaze %w", err))
|
|
|
|
}
|
2023-08-12 09:52:04 +02:00
|
|
|
if err := bbService.CleanUp(ctx, cancel, bucketName); err != nil {
|
2023-08-27 21:30:19 +02:00
|
|
|
panic(fmt.Errorf("bbService.CleanUp %w", err))
|
2023-07-18 15:00:12 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|