Remove unused codes and remove unnecessary variables

This commit is contained in:
Zoltán Papp
2025-10-13 18:25:29 +02:00
parent 582ff1ff8c
commit 9ae48a062a
6 changed files with 50 additions and 63 deletions

View File

@@ -43,17 +43,17 @@ func (u UpdateState) Name() string {
}
type UpdateManager struct {
lastTrigger time.Time
statusRecorder *peer.Status
mgmUpdateChan chan struct{}
updateChannel chan struct{}
wg sync.WaitGroup
currentVersion string
updateFunc func(ctx context.Context, targetVersion string) error
stateManager *statemanager.Manager
lastTrigger time.Time
mgmUpdateChan chan struct{}
updateChannel chan struct{}
currentVersion string
update UpdateInterface
wg sync.WaitGroup
cancel context.CancelFunc
update UpdateInterface
expectedVersion *v.Version
updateToLatestVersion bool
@@ -63,46 +63,24 @@ type UpdateManager struct {
func NewUpdateManager(statusRecorder *peer.Status, stateManager *statemanager.Manager) *UpdateManager {
manager := &UpdateManager{
statusRecorder: statusRecorder,
stateManager: stateManager,
mgmUpdateChan: make(chan struct{}, 1),
updateChannel: make(chan struct{}, 1),
currentVersion: version.NetbirdVersion(),
updateFunc: triggerUpdate,
update: version.NewUpdate("nb/client"),
stateManager: stateManager,
}
return manager
}
func (u *UpdateManager) StartWithTimeout(ctx context.Context, timeout time.Duration) {
if u.cancel != nil {
log.Errorf("UpdateManager already started")
return
}
u.startInit(ctx)
ctx, cancel := context.WithTimeout(ctx, timeout)
u.cancel = cancel
u.wg.Add(1)
go u.updateLoop(ctx)
}
func (u *UpdateManager) Start(ctx context.Context) {
if u.cancel != nil {
log.Errorf("UpdateManager already started")
return
}
u.startInit(ctx)
ctx, cancel := context.WithCancel(ctx)
u.cancel = cancel
u.updateStateManager(ctx)
u.wg.Add(1)
go u.updateLoop(ctx)
}
func (u *UpdateManager) startInit(ctx context.Context) {
u.update.SetDaemonVersion(u.currentVersion)
u.update.SetOnUpdateListener(func() {
select {
@@ -112,31 +90,11 @@ func (u *UpdateManager) startInit(ctx context.Context) {
})
go u.update.StartFetcher()
u.stateManager.RegisterState(&UpdateState{})
if err := u.stateManager.LoadState(&UpdateState{}); err != nil {
log.Warnf("failed to load state: %v", err)
return
}
if u.stateManager.GetState(&UpdateState{}) == nil {
return
}
updateState := u.stateManager.GetState(&UpdateState{}).(*UpdateState)
log.Warnf("autoUpdate state loaded, %v", *updateState)
if updateState.TargetVersion == u.currentVersion {
log.Warnf("published notification event")
u.statusRecorder.PublishEvent(
cProto.SystemEvent_INFO,
cProto.SystemEvent_SYSTEM,
"Auto-update completed",
fmt.Sprintf("Your NetBird Client was auto-updated to version %s", u.currentVersion),
nil,
)
}
if err := u.stateManager.DeleteState(updateState); err != nil {
log.Warnf("failed to delete state: %v", err)
} else if err = u.stateManager.PersistState(ctx); err != nil {
log.Warnf("failed to persist state: %v", err)
}
ctx, cancel := context.WithCancel(ctx)
u.cancel = cancel
u.wg.Add(1)
go u.updateLoop(ctx)
}
func (u *UpdateManager) SetVersion(expectedVersion string) {
@@ -281,7 +239,7 @@ func (u *UpdateManager) handleUpdate(ctx context.Context) {
}
}
err = u.updateFunc(ctx, updateVersion.String())
err = u.triggerUpdate(ctx, updateVersion.String())
if err != nil {
log.Errorf("Error triggering auto-update: %v", err)
@@ -302,6 +260,34 @@ func (u *UpdateManager) handleUpdate(ctx context.Context) {
}
}
func (u *UpdateManager) updateStateManager(ctx context.Context) {
u.stateManager.RegisterState(&UpdateState{})
if err := u.stateManager.LoadState(&UpdateState{}); err != nil {
log.Errorf("failed to load state: %v", err)
return
}
if u.stateManager.GetState(&UpdateState{}) == nil {
return
}
updateState := u.stateManager.GetState(&UpdateState{}).(*UpdateState)
log.Debugf("autoUpdate state loaded, %v", *updateState)
if updateState.TargetVersion == u.currentVersion {
log.Infof("published notification event")
u.statusRecorder.PublishEvent(
cProto.SystemEvent_INFO,
cProto.SystemEvent_SYSTEM,
"Auto-update completed",
fmt.Sprintf("Your NetBird Client was auto-updated to version %s", u.currentVersion),
nil,
)
}
if err := u.stateManager.DeleteState(updateState); err != nil {
log.Errorf("failed to delete state: %v", err)
} else if err = u.stateManager.PersistState(ctx); err != nil {
log.Errorf("failed to persist state: %v", err)
}
}
func (u *UpdateManager) shouldUpdate(updateVersion *v.Version) bool {
currentVersion, err := v.NewVersion(u.currentVersion)
if err != nil {

View File

@@ -16,7 +16,7 @@ const (
pkgDownloadURL = "https://github.com/netbirdio/netbird/releases/download/v%version/netbird_%version_darwin_%arch.pkg"
)
func triggerUpdate(ctx context.Context, targetVersion string) error {
func (u *UpdateManager) triggerUpdate(ctx context.Context, targetVersion string) error {
cmd := exec.CommandContext(ctx, "pkgutil", "--pkg-info", "io.netbird.client")
outBytes, err := cmd.Output()
if err != nil && cmd.ProcessState.ExitCode() == 1 {

View File

@@ -4,7 +4,7 @@ package updatemanager
import "context"
func triggerUpdate(ctx context.Context, targetVersion string) error {
func (u *UpdateManager) triggerUpdate(ctx context.Context, targetVersion string) error {
// TODO: Implement
return nil
}

View File

@@ -4,7 +4,7 @@ package updatemanager
import "context"
func triggerUpdate(ctx context.Context, targetVersion string) error {
func (u *UpdateManager) triggerUpdate(ctx context.Context, targetVersion string) error {
// TODO: Implement
return nil
}

View File

@@ -4,7 +4,7 @@ package updatemanager
import "context"
func triggerUpdate(ctx context.Context, targetVersion string) error {
func (u *UpdateManager) triggerUpdate(ctx context.Context, targetVersion string) error {
// TODO: Implement
return nil
}

View File

@@ -5,9 +5,10 @@ package updatemanager
import (
"context"
"fmt"
"golang.org/x/sys/windows/registry"
"os/exec"
"golang.org/x/sys/windows/registry"
log "github.com/sirupsen/logrus"
)
@@ -18,7 +19,7 @@ const (
uninstallKeyPath32 = `SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Netbird`
)
func installationMethod() string {
func (u *UpdateManager) installationMethod() string {
k, err := registry.OpenKey(registry.LOCAL_MACHINE, uninstallKeyPath64, registry.QUERY_VALUE)
if err != nil {
k, err = registry.OpenKey(registry.LOCAL_MACHINE, uninstallKeyPath32, registry.QUERY_VALUE)