From 9ae48a062a590d88d785d9896784cb4b22dd5a01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Papp?= Date: Mon, 13 Oct 2025 18:25:29 +0200 Subject: [PATCH] Remove unused codes and remove unnecessary variables --- client/internal/updatemanager/manager.go | 100 ++++++++---------- .../internal/updatemanager/update_darwin.go | 2 +- .../internal/updatemanager/update_freebsd.go | 2 +- client/internal/updatemanager/update_js.go | 2 +- client/internal/updatemanager/update_linux.go | 2 +- .../internal/updatemanager/update_windows.go | 5 +- 6 files changed, 50 insertions(+), 63 deletions(-) diff --git a/client/internal/updatemanager/manager.go b/client/internal/updatemanager/manager.go index c8b5baf1f..2c80e6968 100644 --- a/client/internal/updatemanager/manager.go +++ b/client/internal/updatemanager/manager.go @@ -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 { diff --git a/client/internal/updatemanager/update_darwin.go b/client/internal/updatemanager/update_darwin.go index fc91b9cdf..95f4bf9a6 100644 --- a/client/internal/updatemanager/update_darwin.go +++ b/client/internal/updatemanager/update_darwin.go @@ -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 { diff --git a/client/internal/updatemanager/update_freebsd.go b/client/internal/updatemanager/update_freebsd.go index 18b3dea24..f987897f1 100644 --- a/client/internal/updatemanager/update_freebsd.go +++ b/client/internal/updatemanager/update_freebsd.go @@ -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 } diff --git a/client/internal/updatemanager/update_js.go b/client/internal/updatemanager/update_js.go index b5085e11c..cec6c9a1c 100644 --- a/client/internal/updatemanager/update_js.go +++ b/client/internal/updatemanager/update_js.go @@ -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 } diff --git a/client/internal/updatemanager/update_linux.go b/client/internal/updatemanager/update_linux.go index 7ff116a16..64caa0f77 100644 --- a/client/internal/updatemanager/update_linux.go +++ b/client/internal/updatemanager/update_linux.go @@ -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 } diff --git a/client/internal/updatemanager/update_windows.go b/client/internal/updatemanager/update_windows.go index 599fe9e6a..39eb3b89f 100644 --- a/client/internal/updatemanager/update_windows.go +++ b/client/internal/updatemanager/update_windows.go @@ -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)