-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
update.go
74 lines (56 loc) · 1.69 KB
/
update.go
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
69
70
71
72
73
74
// Copyright (c) 2021 - 2024, Ludvig Lundgren and the autobrr contributors.
// SPDX-License-Identifier: GPL-2.0-or-later
package update
import (
"context"
"sync"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/internal/logger"
"github.com/autobrr/autobrr/pkg/version"
"github.com/rs/zerolog"
)
type Service struct {
log zerolog.Logger
config *domain.Config
m sync.RWMutex
releaseChecker *version.Checker
latestRelease *version.Release
}
func NewUpdate(log logger.Logger, config *domain.Config) *Service {
return &Service{
log: log.With().Str("module", "update").Logger(),
config: config,
releaseChecker: version.NewChecker("autobrr", "autobrr", config.Version),
}
}
func (s *Service) GetLatestRelease(ctx context.Context) *version.Release {
s.m.RLock()
defer s.m.RUnlock()
return s.latestRelease
}
func (s *Service) CheckUpdates(ctx context.Context) {
if _, err := s.CheckUpdateAvailable(ctx); err != nil {
s.log.Error().Err(err).Msg("error checking new release")
return
}
return
}
func (s *Service) CheckUpdateAvailable(ctx context.Context) (*version.Release, error) {
s.log.Trace().Msg("checking for updates...")
newAvailable, newVersion, err := s.releaseChecker.CheckNewVersion(ctx, s.config.Version)
if err != nil {
s.log.Error().Err(err).Msg("could not check for new release")
return nil, nil
}
if newAvailable {
s.log.Info().Msgf("autobrr outdated, found newer release: %s", newVersion.TagName)
s.m.Lock()
defer s.m.Unlock()
if s.latestRelease != nil && s.latestRelease.TagName == newVersion.TagName {
return nil, nil
}
s.latestRelease = newVersion
return newVersion, nil
}
return nil, nil
}