Skip to content

Commit 5ba5b40

Browse files
committed
Resolve copilot comments
1 parent 6bb62b9 commit 5ba5b40

File tree

4 files changed

+22
-25
lines changed

4 files changed

+22
-25
lines changed

client/internal/updatemanager/manager.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http"
1010
"os"
1111
"path/filepath"
12+
"runtime"
1213
"strings"
1314
"time"
1415

@@ -50,7 +51,7 @@ func NewUpdateManager(statusRecorder *peer.Status) *UpdateManager {
5051

5152
func (u *UpdateManager) SetVersion(v string) {
5253
if u.version != v {
53-
log.Tracef("Auto-update verstion set to %s", v)
54+
log.Tracef("Auto-update version set to %s", v)
5455
u.version = v
5556
go u.CheckForUpdates()
5657
}
@@ -145,3 +146,9 @@ func downloadFileToTemporaryDir(ctx context.Context, fileURL string) (string, er
145146

146147
return out.Name(), nil
147148
}
149+
150+
func urlWithVersionArch(url, version string) string { //nolint:unused
151+
url = strings.ReplaceAll(url, "%version", version)
152+
url = strings.ReplaceAll(url, "%arch", runtime.GOARCH)
153+
return url
154+
}

client/internal/updatemanager/update_darwin.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"os"
88
"os/exec"
99
"os/user"
10-
"runtime"
1110
"strings"
1211
"syscall"
1312
)
@@ -25,9 +24,7 @@ func (u *UpdateManager) triggerUpdate(targetVersion string) error {
2524
return u.updateHomeBrew()
2625
}
2726
// Installed using pkg file
28-
url := strings.ReplaceAll(pkgDownloadURL, "%version", targetVersion)
29-
url = strings.ReplaceAll(url, "%arch", runtime.GOARCH)
30-
path, err := downloadFileToTemporaryDir(u.ctx, url)
27+
path, err := downloadFileToTemporaryDir(u.ctx, urlWithVersionArch(pkgDownloadURL, targetVersion))
3128
if err != nil {
3229
return fmt.Errorf("error downloading update file: %w", err)
3330
}
@@ -99,7 +96,7 @@ func (u *UpdateManager) updateHomeBrew() error {
9996
// Restart netbird service after the fact
10097
// This is a workaround since attempting to restart using launchctl will kill the service and die before starting
10198
// the service again as it's a child process
102-
// using SigTerm should ensure a clean shutdown
99+
// using SIGTERM should ensure a clean shutdown
103100
process, err := os.FindProcess(currentPID)
104101
if err != nil {
105102
return fmt.Errorf("error finding current process: %w", err)

client/internal/updatemanager/update_windows.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ package updatemanager
44

55
import (
66
"fmt"
7-
"os/exec"
8-
"runtime"
9-
"strings"
10-
117
log "github.com/sirupsen/logrus"
8+
"os/exec"
129

1310
"golang.org/x/sys/windows/registry"
1411
)
@@ -42,9 +39,7 @@ func installationMethod() string {
4239
}
4340

4441
func (u *UpdateManager) updateMSI(targetVersion string) error {
45-
url := strings.ReplaceAll(msiDownloadURL, "%version", targetVersion)
46-
url = strings.ReplaceAll(url, "%arch", runtime.GOARCH)
47-
path, err := downloadFileToTemporaryDir(u.ctx, url)
42+
path, err := downloadFileToTemporaryDir(u.ctx, urlWithVersionArch(msiDownloadURL, targetVersion))
4843
if err != nil {
4944
return err
5045
}
@@ -54,9 +49,7 @@ func (u *UpdateManager) updateMSI(targetVersion string) error {
5449
}
5550

5651
func (u *UpdateManager) updateEXE(targetVersion string) error {
57-
url := strings.ReplaceAll(exeDownloadURL, "%version", targetVersion)
58-
url = strings.ReplaceAll(url, "%arch", runtime.GOARCH)
59-
path, err := downloadFileToTemporaryDir(u.ctx, url)
52+
path, err := downloadFileToTemporaryDir(u.ctx, urlWithVersionArch(exeDownloadURL, targetVersion))
6053
if err != nil {
6154
return err
6255
}

management/server/http/handlers/accounts/accounts_handler.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package accounts
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"net/http"
78
"net/netip"
89
"time"
@@ -12,11 +13,11 @@ import (
1213
goversion "github.com/hashicorp/go-version"
1314
"github.com/netbirdio/netbird/management/server/account"
1415
nbcontext "github.com/netbirdio/netbird/management/server/context"
16+
"github.com/netbirdio/netbird/management/server/settings"
17+
"github.com/netbirdio/netbird/management/server/types"
1518
"github.com/netbirdio/netbird/shared/management/http/api"
1619
"github.com/netbirdio/netbird/shared/management/http/util"
17-
"github.com/netbirdio/netbird/management/server/settings"
1820
"github.com/netbirdio/netbird/shared/management/status"
19-
"github.com/netbirdio/netbird/management/server/types"
2021
)
2122

2223
const (
@@ -163,7 +164,7 @@ func (h *handler) getAllAccounts(w http.ResponseWriter, r *http.Request) {
163164
util.WriteJSONObject(r.Context(), w, []*api.Account{resp})
164165
}
165166

166-
func (h *handler) updateAccountRequestSettings(req api.PutApiAccountsAccountIdJSONRequestBody, w http.ResponseWriter) *types.Settings {
167+
func (h *handler) updateAccountRequestSettings(req api.PutApiAccountsAccountIdJSONRequestBody) (*types.Settings, error) {
167168
returnSettings := &types.Settings{
168169
PeerLoginExpirationEnabled: req.Settings.PeerLoginExpirationEnabled,
169170
PeerLoginExpiration: time.Duration(float64(time.Second.Nanoseconds()) * float64(req.Settings.PeerLoginExpiration)),
@@ -209,12 +210,11 @@ func (h *handler) updateAccountRequestSettings(req api.PutApiAccountsAccountIdJS
209210
err == nil {
210211
returnSettings.AutoUpdateVersion = *req.Settings.AutoUpdateVersion
211212
} else if *req.Settings.AutoUpdateVersion != "" {
212-
util.WriteErrorResponse("Invalid AutoUpdateVersion", http.StatusBadRequest, w)
213-
return nil
213+
return nil, fmt.Errorf("invalid AutoUpdateVersion")
214214
}
215215
}
216216

217-
return returnSettings
217+
return returnSettings, nil
218218
}
219219

220220
// updateAccount is HTTP PUT handler that updates the provided account. Updates only account settings (server.Settings)
@@ -241,9 +241,9 @@ func (h *handler) updateAccount(w http.ResponseWriter, r *http.Request) {
241241
return
242242
}
243243

244-
settings := h.updateAccountRequestSettings(req, w)
245-
if settings == nil {
246-
return
244+
settings, err := h.updateAccountRequestSettings(req)
245+
if err != nil {
246+
util.WriteError(r.Context(), err, w)
247247
}
248248
if req.Settings.NetworkRange != nil && *req.Settings.NetworkRange != "" {
249249
prefix, err := netip.ParsePrefix(*req.Settings.NetworkRange)

0 commit comments

Comments
 (0)