summaryrefslogtreecommitdiff
path: root/internal/archive/archive.go
blob: 8ea7e7a5d3da7c1497cd483dadd6804b5a539683 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package archive

import (
	"fmt"
	"os"
	"path/filepath"
	"time"
)

// Archiver moves the cards directory into a timestamped archive folder under
// the parent state directory. Implementations are typically injected at
// composition roots (cmd, GUI) so callers depend on this abstraction rather
// than package-level functions alone.
type Archiver interface {
	ArchiveCards(cardsDir string) error
}

// DefaultArchiver implements Archiver using the local filesystem.
type DefaultArchiver struct{}

// ArchiveCards implements Archiver.
func (DefaultArchiver) ArchiveCards(cardsDir string) error {
	return archiveCards(cardsDir)
}

// archiveCards moves the cards directory to an archive with timestamp.
func archiveCards(cardsDir string) error {
	// Check if cards directory exists
	if _, err := os.Stat(cardsDir); os.IsNotExist(err) {
		return fmt.Errorf("cards directory does not exist: %s", cardsDir)
	}

	// Get parent directory and create archive path
	parentDir := filepath.Dir(cardsDir)
	archiveDir := filepath.Join(parentDir, "archive")

	// Create archive directory if it doesn't exist
	if err := os.MkdirAll(archiveDir, 0755); err != nil {
		return fmt.Errorf("failed to create archive directory: %w", err)
	}

	// Generate timestamp
	timestamp := time.Now().Format("20060102-150405")
	archiveName := fmt.Sprintf("cards-%s", timestamp)
	archivePath := filepath.Join(archiveDir, archiveName)

	// Check if archive already exists (unlikely but possible)
	if _, err := os.Stat(archivePath); err == nil {
		// Add microseconds to make it unique
		timestamp = time.Now().Format("20060102-150405.000000")
		archiveName = fmt.Sprintf("cards-%s", timestamp)
		archivePath = filepath.Join(archiveDir, archiveName)
	}

	// Rename cards directory to archive
	if err := os.Rename(cardsDir, archivePath); err != nil {
		return fmt.Errorf("failed to archive cards directory: %w", err)
	}

	fmt.Printf("Cards directory archived to: %s\n", archivePath)
	return nil
}

// ArchiveCards archives cards using DefaultArchiver. It exists for call sites
// that do not use dependency injection (e.g. tests and legacy scripts).
func ArchiveCards(cardsDir string) error {
	return (DefaultArchiver{}).ArchiveCards(cardsDir)
}