mirror of
https://github.com/netbirdio/netbird.git
synced 2025-06-24 11:41:35 +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 (
|
||||
defaultProfileName = "default"
|
||||
defaultProfileName = "default"
|
||||
profileStateFilename = "active_profile.txt"
|
||||
)
|
||||
|
||||
type Profile struct {
|
||||
@ -111,7 +112,7 @@ func (pm *ProfileManager) ListProfiles() ([]Profile, error) {
|
||||
|
||||
var profiles []Profile
|
||||
// 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 {
|
||||
profileName := strings.TrimSuffix(filepath.Base(file), ".json")
|
||||
var isActive bool
|
||||
@ -153,7 +154,7 @@ func (pm *ProfileManager) getActiveProfileState() string {
|
||||
return defaultProfileName
|
||||
}
|
||||
|
||||
statePath := filepath.Join(configDir, "active_profile.txt")
|
||||
statePath := filepath.Join(configDir, profileStateFilename)
|
||||
|
||||
prof, err := os.ReadFile(statePath)
|
||||
if err != nil {
|
||||
@ -170,7 +171,7 @@ func (pm *ProfileManager) getActiveProfileState() string {
|
||||
log.Warnf("active profile state is empty, using default profile: %s", 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)
|
||||
return defaultProfileName
|
||||
}
|
||||
@ -184,12 +185,14 @@ func (pm *ProfileManager) setActiveProfileState(profileName string) error {
|
||||
return fmt.Errorf("failed to get config directory: %w", err)
|
||||
}
|
||||
|
||||
profPath := filepath.Join(configDir, profileName+".json")
|
||||
if !fileExists(profPath) {
|
||||
return fmt.Errorf("profile %s does not exist", profileName)
|
||||
if profileName != defaultProfileName {
|
||||
profPath := filepath.Join(configDir, profileName+".json")
|
||||
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)
|
||||
if err != nil {
|
||||
|
@ -192,23 +192,6 @@ func (s *serviceClient) showProfilesUI() {
|
||||
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 {
|
||||
err := s.profileManager.AddProfile(profilemanager.Profile{
|
||||
Name: profileName,
|
||||
@ -334,7 +317,6 @@ func (p *profileMenu) refresh() {
|
||||
}
|
||||
}()
|
||||
|
||||
// TODO(hakan): handle switch profile
|
||||
}
|
||||
p.profileMenuItem.AddSeparator()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
Loading…
x
Reference in New Issue
Block a user