Some checks failed
FreeBSD / Client / Unit (push) Has been cancelled
Linux / Build Cache (push) Has been cancelled
Linux / Client / Unit (386) (push) Has been cancelled
Linux / Client / Unit (amd64) (push) Has been cancelled
Darwin / Client / Unit (push) Has been cancelled
Linux / Client (Docker) / Unit (push) Has been cancelled
Linux / Relay / Unit (386, ) (push) Has been cancelled
Linux / Relay / Unit (amd64, -race) (push) Has been cancelled
Linux / Proxy / Unit (386) (push) Has been cancelled
Linux / Proxy / Unit (amd64) (push) Has been cancelled
Linux / Signal / Unit (386) (push) Has been cancelled
Linux / Signal / Unit (amd64) (push) Has been cancelled
Linux / Management / Unit (amd64, mysql) (push) Has been cancelled
Linux / Management / Unit (amd64, postgres) (push) Has been cancelled
Test installation / test-install-script (false, macos-latest, false) (push) Has been cancelled
Linux / Management / Unit (amd64, sqlite) (push) Has been cancelled
Linux / Management / Benchmark (amd64, postgres) (push) Has been cancelled
Linux / Management / Benchmark (amd64, sqlite) (push) Has been cancelled
Linux / Management / Benchmark (API) (amd64, postgres) (push) Has been cancelled
Linux / Management / Benchmark (API) (amd64, sqlite) (push) Has been cancelled
Linux / Management / Integration (amd64, postgres) (push) Has been cancelled
Linux / Management / Integration (amd64, sqlite) (push) Has been cancelled
Windows / Client / Unit (push) Has been cancelled
Mobile / Android / Build (push) Has been cancelled
Mobile / iOS / Build (push) Has been cancelled
Release / release_ui_darwin (push) Has been cancelled
Release / Windows Installer / Build Test (amd64, amd64) (push) Has been cancelled
Test installation / test-install-script (false, macos-latest, true) (push) Has been cancelled
Test installation / test-install-script (false, ubuntu-latest, false) (push) Has been cancelled
Test installation / test-install-script (false, ubuntu-latest, true) (push) Has been cancelled
Test installation / test-install-script (true, macos-latest, false) (push) Has been cancelled
Test installation / test-install-script (true, macos-latest, true) (push) Has been cancelled
Release / Windows Installer / Build Test (arm64, arm64) (push) Has been cancelled
Release / Comment release artifacts (push) Has been cancelled
Test installation / test-install-script (true, ubuntu-latest, false) (push) Has been cancelled
Test installation / test-install-script (true, ubuntu-latest, true) (push) Has been cancelled
Release / FreeBSD Port / Build & Test (push) Has been cancelled
Release / release (push) Has been cancelled
Release / release_ui (push) Has been cancelled
Test Infrastructure files / test-docker-compose (mysql) (push) Has been cancelled
Test Infrastructure files / test-docker-compose (postgres) (push) Has been cancelled
Test Infrastructure files / test-docker-compose (sqlite) (push) Has been cancelled
Test Infrastructure files / test-getting-started-script (push) Has been cancelled
Release / trigger_signer (push) Has been cancelled
sync main / trigger_sync_main (push) Has been cancelled
Wasm / JS / Lint (push) Has been cancelled
Wasm / JS / Build (push) Has been cancelled
* Migrate to profile ids * Migrate android profile manager * Clean up * Fix review * Add ID type * Fix test and runes in ShortID() * Fix profile switch on up and android comments * Revert android profile to string id * Fix feedback * Fix UI feedback * Fix id assignment * Add renaming of profiles * Fix review * Remove ui binary * Fix getProfileConfigPath not validating id * Change resolve handle order and fix server merge problems * Fix mdm test
74 lines
1.9 KiB
Go
74 lines
1.9 KiB
Go
package profilemanager
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"path/filepath"
|
|
|
|
"github.com/netbirdio/netbird/util"
|
|
)
|
|
|
|
type ProfileState struct {
|
|
Email string `json:"email"`
|
|
}
|
|
|
|
// GetProfileState reads the per-profile state file keyed by profile ID.
|
|
// The state file lives in the user's config directory. Legacy state files
|
|
// keyed by the old profile name remain readable.
|
|
func (pm *ProfileManager) GetProfileState(id ID) (*ProfileState, error) {
|
|
configDir, err := getConfigDir()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get config directory: %w", err)
|
|
}
|
|
|
|
if id != defaultProfileName && !IsValidProfileFilenameStem(id) {
|
|
return nil, fmt.Errorf("invalid profile ID: %q", id)
|
|
}
|
|
|
|
stateFile := filepath.Join(configDir, id.String()+".state.json")
|
|
stateFileExists, err := fileExists(stateFile)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to check if profile state file exists: %w", err)
|
|
}
|
|
if !stateFileExists {
|
|
return nil, errors.New("profile state file does not exist")
|
|
}
|
|
|
|
var state ProfileState
|
|
_, err = util.ReadJson(stateFile, &state)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read profile state: %w", err)
|
|
}
|
|
|
|
return &state, nil
|
|
}
|
|
|
|
func (pm *ProfileManager) SetActiveProfileState(state *ProfileState) error {
|
|
configDir, err := getConfigDir()
|
|
if err != nil {
|
|
return fmt.Errorf("get config directory: %w", err)
|
|
}
|
|
|
|
activeProf, err := pm.GetActiveProfile()
|
|
if err != nil {
|
|
if errors.Is(err, ErrNoActiveProfile) {
|
|
return fmt.Errorf("no active profile set: %w", err)
|
|
}
|
|
return fmt.Errorf("get active profile: %w", err)
|
|
}
|
|
|
|
id := activeProf.ID
|
|
if id != defaultProfileName && !IsValidProfileFilenameStem(id) {
|
|
return fmt.Errorf("invalid active profile ID: %q", id)
|
|
}
|
|
|
|
stateFile := filepath.Join(configDir, id.String()+".state.json")
|
|
err = util.WriteJsonWithRestrictedPermission(context.Background(), stateFile, state)
|
|
if err != nil {
|
|
return fmt.Errorf("write profile state: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|