updater.go RawHistoryFolder
1// Package port defines interfaces for external dependencies.
2package port
3
4import (
5 "context"
6
7 "github.com/bnema/dumber/internal/domain/entity"
8)
9
10// UpdateChecker checks for available updates from a remote source.
11type UpdateChecker interface {
12 // CheckForUpdate compares the current version with the latest available release.
13 // Returns update info including whether a newer version is available.
14 CheckForUpdate(ctx context.Context, currentVersion string) (*entity.UpdateInfo, error)
15}
16
17// UpdateDownloader downloads and extracts update archives.
18type UpdateDownloader interface {
19 // Download fetches the update archive to the specified destination directory.
20 // Returns the path to the downloaded archive file.
21 Download(ctx context.Context, downloadURL string, destDir string) (archivePath string, err error)
22
23 // Extract extracts the binary from the downloaded archive.
24 // Returns the path to the extracted binary.
25 Extract(ctx context.Context, archivePath string, destDir string) (binaryPath string, err error)
26}
27
28// SelfUpdateBlockedReason indicates why self-update is not available.
29type SelfUpdateBlockedReason string
30
31const (
32 // SelfUpdateAllowed means self-update is possible.
33 SelfUpdateAllowed SelfUpdateBlockedReason = ""
34 // SelfUpdateBlockedFlatpak means updates are managed by Flatpak.
35 SelfUpdateBlockedFlatpak SelfUpdateBlockedReason = "flatpak"
36 // SelfUpdateBlockedPacman means updates are managed by pacman/AUR.
37 SelfUpdateBlockedPacman SelfUpdateBlockedReason = "pacman"
38 // SelfUpdateBlockedNotWritable means the binary is not writable.
39 SelfUpdateBlockedNotWritable SelfUpdateBlockedReason = "not_writable"
40)
41
42// UpdateApplier stages and applies updates.
43type UpdateApplier interface {
44 // CanSelfUpdate checks if the current binary is writable by the current user.
45 // Returns true if auto-update is possible, false if user lacks write permission.
46 CanSelfUpdate(ctx context.Context) bool
47
48 // SelfUpdateBlockedReason returns why self-update is blocked, or empty if allowed.
49 SelfUpdateBlockedReason(ctx context.Context) SelfUpdateBlockedReason
50
51 // GetBinaryPath returns the path to the currently running binary.
52 GetBinaryPath() (string, error)
53
54 // StageUpdate prepares the new binary to be applied on exit.
55 // The staged update will be applied when ApplyOnExit is called.
56 StageUpdate(ctx context.Context, newBinaryPath string) error
57
58 // HasStagedUpdate checks if there is an update staged and ready to apply.
59 HasStagedUpdate(ctx context.Context) bool
60
61 // ApplyOnExit replaces the current binary with the staged update.
62 // This should be called during graceful shutdown.
63 // Returns the path to the backup of the old binary.
64 ApplyOnExit(ctx context.Context) (backupPath string, err error)
65
66 // ClearStagedUpdate removes any staged update without applying it.
67 ClearStagedUpdate(ctx context.Context) error
68}