backblaze-backup/cmd/sync.go

47 lines
1.1 KiB
Go

package cmd
import (
"context"
"log"
"os"
"gitea.urkob.com/urko/backblaze-backup/internal/services"
"gitea.urkob.com/urko/backblaze-backup/kit/config"
"github.com/spf13/cobra"
)
var Sync = &cobra.Command{
Use: "sync",
Short: "Sync files or directories to Backblaze",
Long: `A tool to backup files and directories to Backblaze.`,
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithCancel(signalContext(cmd.Context()))
defer cancel()
log.SetFlags(log.Lmicroseconds)
filePath, err := cmd.Flags().GetString("file")
if err != nil {
log.Fatalln("file %w", err)
}
dir, err := cmd.Flags().GetString("dir")
if err != nil {
log.Fatalln("dir %w", err)
}
bucketName, err := cmd.Flags().GetString("bucket")
if err != nil {
log.Fatalln("bucket %w", err)
}
envFile := ""
if os.Getenv("BACKBLAZE_ENV") == "dev" {
envFile = ".env"
}
cfg := config.NewConfig(envFile)
bbService := services.NewBackBlaze(cfg.BbId, cfg.BbKey).WithBucket(bucketName).WithDir(dir).WithFile(filePath)
if err := bbService.Sync(ctx); err != nil {
log.Fatalln("bbService.Sync()", err)
}
},
}