mirror of
https://github.com/netbirdio/netbird.git
synced 2025-06-24 19:51:33 +02:00
feat: introduce profile state management with active profile tracking
This commit is contained in:
parent
4b68a2a665
commit
618009e27f
@ -15,7 +15,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultProfileName = "default"
|
defaultProfileName = "default"
|
||||||
|
profileStateFilename = "active_profile.txt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Profile struct {
|
type Profile struct {
|
||||||
@ -111,7 +112,7 @@ func (pm *ProfileManager) ListProfiles() ([]Profile, error) {
|
|||||||
|
|
||||||
var profiles []Profile
|
var profiles []Profile
|
||||||
// add default profile always
|
// add default profile always
|
||||||
profiles = append(profiles, Profile{Name: "default", IsActive: activeProfName == "" || activeProfName == "default"})
|
profiles = append(profiles, Profile{Name: defaultProfileName, IsActive: activeProfName == "" || activeProfName == defaultProfileName})
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
profileName := strings.TrimSuffix(filepath.Base(file), ".json")
|
profileName := strings.TrimSuffix(filepath.Base(file), ".json")
|
||||||
var isActive bool
|
var isActive bool
|
||||||
@ -153,7 +154,7 @@ func (pm *ProfileManager) getActiveProfileState() string {
|
|||||||
return defaultProfileName
|
return defaultProfileName
|
||||||
}
|
}
|
||||||
|
|
||||||
statePath := filepath.Join(configDir, "active_profile.txt")
|
statePath := filepath.Join(configDir, profileStateFilename)
|
||||||
|
|
||||||
prof, err := os.ReadFile(statePath)
|
prof, err := os.ReadFile(statePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -170,7 +171,7 @@ func (pm *ProfileManager) getActiveProfileState() string {
|
|||||||
log.Warnf("active profile state is empty, using default profile: %s", defaultProfileName)
|
log.Warnf("active profile state is empty, using default profile: %s", defaultProfileName)
|
||||||
return defaultProfileName
|
return defaultProfileName
|
||||||
}
|
}
|
||||||
if !fileExists(filepath.Join(configDir, profileName+".json")) {
|
if profileName != defaultProfileName && !fileExists(filepath.Join(configDir, profileName+".json")) {
|
||||||
log.Warnf("active profile %s does not exist, using default profile: %s", profileName, defaultProfileName)
|
log.Warnf("active profile %s does not exist, using default profile: %s", profileName, defaultProfileName)
|
||||||
return defaultProfileName
|
return defaultProfileName
|
||||||
}
|
}
|
||||||
@ -184,12 +185,14 @@ func (pm *ProfileManager) setActiveProfileState(profileName string) error {
|
|||||||
return fmt.Errorf("failed to get config directory: %w", err)
|
return fmt.Errorf("failed to get config directory: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
profPath := filepath.Join(configDir, profileName+".json")
|
if profileName != defaultProfileName {
|
||||||
if !fileExists(profPath) {
|
profPath := filepath.Join(configDir, profileName+".json")
|
||||||
return fmt.Errorf("profile %s does not exist", profileName)
|
if !fileExists(profPath) {
|
||||||
|
return fmt.Errorf("profile %s does not exist", profileName)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
statePath := filepath.Join(configDir, "active_profile.txt")
|
statePath := filepath.Join(configDir, profileStateFilename)
|
||||||
|
|
||||||
err = os.WriteFile(statePath, []byte(profileName), 0644)
|
err = os.WriteFile(statePath, []byte(profileName), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -192,23 +192,6 @@ func (s *serviceClient) showProfilesUI() {
|
|||||||
s.wProfiles.Show()
|
s.wProfiles.Show()
|
||||||
}
|
}
|
||||||
|
|
||||||
// func (p *profileMenu) updateProfiles(ctx context.Context, conn proto.DaemonServiceClient) {
|
|
||||||
// profiles, err := p.getProfiles(ctx, conn)
|
|
||||||
// if err != nil {
|
|
||||||
// log.Errorf("get profiles: %v", err)
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Clear existing profiles
|
|
||||||
// p.clearProfiles()
|
|
||||||
|
|
||||||
// p.mtx.Lock()
|
|
||||||
// defer p.mtx.Unlock()
|
|
||||||
// // Add new profiles
|
|
||||||
// p.profiles = append(p.profiles, profiles...)
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
func (s *serviceClient) addProfile(profileName string) error {
|
func (s *serviceClient) addProfile(profileName string) error {
|
||||||
err := s.profileManager.AddProfile(profilemanager.Profile{
|
err := s.profileManager.AddProfile(profilemanager.Profile{
|
||||||
Name: profileName,
|
Name: profileName,
|
||||||
@ -334,7 +317,6 @@ func (p *profileMenu) refresh() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// TODO(hakan): handle switch profile
|
|
||||||
}
|
}
|
||||||
p.profileMenuItem.AddSeparator()
|
p.profileMenuItem.AddSeparator()
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user