check_update.go RawHistoryFolder
1package usecase
2
3import (
4 "context"
5
6 "github.com/bnema/dumber/internal/application/port"
7 "github.com/bnema/dumber/internal/domain/build"
8 "github.com/bnema/dumber/internal/logging"
9)
10
11// CheckUpdateInput holds the input for the check update use case.
12type CheckUpdateInput struct{}
13
14// CheckUpdateOutput holds the result of the update check.
15type CheckUpdateOutput struct {
16 // UpdateAvailable is true if a newer version exists.
17 UpdateAvailable bool
18 // CanAutoUpdate is true if the binary is writable and auto-update is possible.
19 CanAutoUpdate bool
20 // CurrentVersion is the version of the running binary.
21 CurrentVersion string
22 // LatestVersion is the latest available version.
23 LatestVersion string
24 // ReleaseURL is the URL to the GitHub release page.
25 ReleaseURL string
26 // DownloadURL is the direct download URL for the archive.
27 DownloadURL string
28}
29
30// CheckUpdateUseCase checks for available updates.
31type CheckUpdateUseCase struct {
32 checker port.UpdateChecker
33 applier port.UpdateApplier
34 buildInfo build.Info
35}
36
37// NewCheckUpdateUseCase creates a new check update use case.
38func NewCheckUpdateUseCase(
39 checker port.UpdateChecker,
40 applier port.UpdateApplier,
41 buildInfo build.Info,
42) *CheckUpdateUseCase {
43 return &CheckUpdateUseCase{
44 checker: checker,
45 applier: applier,
46 buildInfo: buildInfo,
47 }
48}
49
50// Execute checks for available updates.
51func (uc *CheckUpdateUseCase) Execute(ctx context.Context, _ CheckUpdateInput) (*CheckUpdateOutput, error) {
52 log := logging.FromContext(ctx)
53
54 info, err := uc.checker.CheckForUpdate(ctx, uc.buildInfo.Version)
55 if err != nil {
56 log.Warn().Err(err).Msg("update check failed")
57 return nil, err
58 }
59
60 canAutoUpdate := false
61 if info.IsNewer {
62 canAutoUpdate = uc.applier.CanSelfUpdate(ctx)
63 }
64
65 log.Debug().
66 Str("current", uc.buildInfo.Version).
67 Str("latest", info.LatestVersion).
68 Bool("update_available", info.IsNewer).
69 Bool("can_auto_update", canAutoUpdate).
70 Msg("update check completed")
71
72 return &CheckUpdateOutput{
73 UpdateAvailable: info.IsNewer,
74 CanAutoUpdate: canAutoUpdate,
75 CurrentVersion: info.CurrentVersion,
76 LatestVersion: info.LatestVersion,
77 ReleaseURL: info.ReleaseURL,
78 DownloadURL: info.DownloadURL,
79 }, nil
80}