backblaze-backup/cmd/main.go

57 lines
1.4 KiB
Go
Raw Permalink Normal View History

2023-07-10 12:19:21 +02:00
package main
import (
"fmt"
"log"
"os"
"gitea.urkob.com/urko/backblaze-backup/internal/services"
2023-07-10 14:45:52 +02:00
"gitea.urkob.com/urko/backblaze-backup/kit/config"
2023-07-10 12:19:21 +02:00
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "backblaze-backup",
Short: "Backblaze backup tool",
Long: `A tool to backup files and directories to Backblaze.`,
Run: func(cmd *cobra.Command, args []string) {
log.SetFlags(log.Lmicroseconds)
2023-07-10 12:19:21 +02:00
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)
}
2023-07-10 14:45:52 +02:00
envFile := ""
if os.Getenv("BACKBLAZE_ENV") == "dev" {
envFile = ".env"
}
cfg := config.NewConfig(envFile)
2023-07-10 12:19:21 +02:00
2023-07-10 14:45:52 +02:00
bbService := services.NewBackBlaze(cfg.BbId, cfg.BbKey).WithBucket(bucketName).WithDir(dir).WithFile(filePath)
2023-07-10 12:19:21 +02:00
if err := bbService.Sync(); err != nil {
log.Fatalln("bbService.Sync()", err)
}
},
}
func init() {
rootCmd.PersistentFlags().String("file", "", "absolute path of the file you want to upload to backblaze")
rootCmd.PersistentFlags().String("dir", "", "absolute path of the directory you want to upload to backblaze")
rootCmd.PersistentFlags().String("bucket", "", "backblaze bucket name")
}
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}