package cli import ( "context" "fmt" "os" "git.eeqj.de/sneak/vaultik/internal/config" "git.eeqj.de/sneak/vaultik/internal/database" "git.eeqj.de/sneak/vaultik/internal/globals" "github.com/spf13/cobra" "go.uber.org/fx" ) // BackupOptions contains options for the backup command type BackupOptions struct { ConfigPath string Daemon bool Cron bool Prune bool } // NewBackupCommand creates the backup command func NewBackupCommand() *cobra.Command { opts := &BackupOptions{} cmd := &cobra.Command{ Use: "backup", Short: "Perform incremental backup", Long: `Backup configured directories using incremental deduplication and encryption. Config is located at /etc/vaultik/config.yml, but can be overridden by specifying a path using --config or by setting VAULTIK_CONFIG to a path.`, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { // If --config not specified, check environment variable if opts.ConfigPath == "" { opts.ConfigPath = os.Getenv("VAULTIK_CONFIG") } // If still not specified, use default if opts.ConfigPath == "" { defaultConfig := "/etc/vaultik/config.yml" if _, err := os.Stat(defaultConfig); err == nil { opts.ConfigPath = defaultConfig } else { return fmt.Errorf("no config file specified, VAULTIK_CONFIG not set, and %s not found", defaultConfig) } } return runBackup(cmd.Context(), opts) }, } cmd.Flags().StringVar(&opts.ConfigPath, "config", "", "Path to config file") cmd.Flags().BoolVar(&opts.Daemon, "daemon", false, "Run in daemon mode with inotify monitoring") cmd.Flags().BoolVar(&opts.Cron, "cron", false, "Run in cron mode (silent unless error)") cmd.Flags().BoolVar(&opts.Prune, "prune", false, "Delete all previous snapshots and unreferenced blobs after backup") return cmd } func runBackup(ctx context.Context, opts *BackupOptions) error { return RunWithApp(ctx, AppOptions{ ConfigPath: opts.ConfigPath, Invokes: []fx.Option{ fx.Invoke(func(g *globals.Globals, cfg *config.Config, repos *database.Repositories) error { // TODO: Implement backup logic fmt.Printf("Running backup with config: %s\n", opts.ConfigPath) fmt.Printf("Version: %s, Commit: %s\n", g.Version, g.Commit) fmt.Printf("Index path: %s\n", cfg.IndexPath) if opts.Daemon { fmt.Println("Running in daemon mode") } if opts.Cron { fmt.Println("Running in cron mode") } if opts.Prune { fmt.Println("Pruning enabled - will delete old snapshots after backup") } return nil }), }, }) }