refactor: remove unused profile-related messages and simplify profile switching logic

This commit is contained in:
Hakan Sariman 2025-06-22 09:38:58 +03:00
parent 63a8ccc13a
commit fee8051685
4 changed files with 187 additions and 873 deletions

View File

@ -126,6 +126,27 @@ func (pm *ProfileManager) ListProfiles() ([]Profile, error) {
return profiles, nil return profiles, nil
} }
// TODO(hakan): implement
func (pm *ProfileManager) SwitchProfile(profileName string) error {
pm.mu.Lock()
defer pm.mu.Unlock()
// Check if the profile exists
configDir, err := getConfigDir()
if err != nil {
return fmt.Errorf("failed to get config directory: %w", err)
}
profPath := filepath.Join(configDir, profileName+".json")
if !fileExists(profPath) {
return ErrProfileNotFound
}
// Set the active profile
pm.activeProfile = &Profile{Name: profileName}
return nil
}
// sanitazeUsername sanitizes the username by removing any invalid characters // sanitazeUsername sanitizes the username by removing any invalid characters
func sanitazeUsername(username string) string { func sanitazeUsername(username string) string {
// Remove invalid characters for a username in a file path // Remove invalid characters for a username in a file path

File diff suppressed because it is too large Load Diff

View File

@ -495,48 +495,3 @@ message GetEventsRequest {}
message GetEventsResponse { message GetEventsResponse {
repeated SystemEvent events = 1; repeated SystemEvent events = 1;
} }
// Profiles
message Profile {
string name = 1;
bool selected = 2;
}
message GetProfilesRequest {
}
message GetProfilesResponse {
repeated Profile profiles = 1;
}
message CreateProfileRequest {
string profile = 1;
}
message CreateProfileResponse {
bool success = 1;
string error = 2;
string profile = 3;
}
message SwitchProfileRequest {
string profile = 1;
}
message SwitchProfileResponse {
bool success = 1;
string error = 2;
string profile = 3;
}
message RemoveProfileRequest {
string profile = 1;
}
message RemoveProfileResponse {
bool success = 1;
string error = 2;
string profile = 3;
}

View File

@ -1,10 +1,8 @@
package main package main
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"sync"
"fyne.io/fyne/v2" "fyne.io/fyne/v2"
"fyne.io/fyne/v2/container" "fyne.io/fyne/v2/container"
@ -14,13 +12,11 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/netbirdio/netbird/client/internal/profilemanager" "github.com/netbirdio/netbird/client/internal/profilemanager"
"github.com/netbirdio/netbird/client/proto"
) )
// showProfilesUI creates and displays the Profiles window with a list of existing profiles, // showProfilesUI creates and displays the Profiles window with a list of existing profiles,
// a button to add new profiles, allows removal, and lets the user switch the active profile. // a button to add new profiles, allows removal, and lets the user switch the active profile.
func (s *serviceClient) showProfilesUI() { func (s *serviceClient) showProfilesUI() {
mProfiles := newProfileMenu()
profiles, err := s.getProfiles() profiles, err := s.getProfiles()
if err != nil { if err != nil {
@ -78,14 +74,8 @@ func (s *serviceClient) showProfilesUI() {
if !confirm { if !confirm {
return return
} }
conn, err := s.getSrvClient(defaultFailTimeout)
if err != nil {
log.Errorf("get client: %v", err)
return
}
// switch // switch
err = mProfiles.switchProfile(s.ctx, conn, profile.Name) err = s.switchProfile(profile.Name)
if err != nil { if err != nil {
dialog.ShowError(fmt.Errorf("failed to select profile: %w", err), s.wProfiles) dialog.ShowError(fmt.Errorf("failed to select profile: %w", err), s.wProfiles)
return return
@ -195,30 +185,11 @@ func (s *serviceClient) showProfilesUI() {
s.wProfiles.Show() s.wProfiles.Show()
} }
type profileMenu struct {
mtx sync.Mutex
profiles []*proto.Profile
}
type profile struct { type profile struct {
name string name string
selected bool selected bool
} }
func newProfileMenu() *profileMenu {
p := &profileMenu{
profiles: make([]*proto.Profile, 0),
}
return p
}
func (p *profileMenu) clearProfiles() {
p.mtx.Lock()
defer p.mtx.Unlock()
p.profiles = make([]*proto.Profile, 0)
}
// func (p *profileMenu) updateProfiles(ctx context.Context, conn proto.DaemonServiceClient) { // func (p *profileMenu) updateProfiles(ctx context.Context, conn proto.DaemonServiceClient) {
// profiles, err := p.getProfiles(ctx, conn) // profiles, err := p.getProfiles(ctx, conn)
// if err != nil { // if err != nil {
@ -248,18 +219,11 @@ func (s *serviceClient) addProfile(profileName string) error {
return nil return nil
} }
func (p *profileMenu) switchProfile(pCtx context.Context, conn proto.DaemonServiceClient, profileName string) error { func (s *serviceClient) switchProfile(profileName string) error {
ctx, cancel := context.WithTimeout(pCtx, defaultFailTimeout) err := s.profileManager.SwitchProfile(profileName)
defer cancel()
resp, err := conn.SwitchProfile(ctx, &proto.SwitchProfileRequest{Profile: profileName})
if err != nil { if err != nil {
return fmt.Errorf("switch profile: %v", err) return fmt.Errorf("switch profile: %w", err)
} }
if !resp.Success {
return fmt.Errorf("switch profile: %s", resp.Error)
}
return nil return nil
} }