summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2022-05-09 14:18:23 +0200
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-05-09 14:18:23 +0200
commit1ef27cc47ad56a2c66aaa40e398a0063be2573d4 (patch)
treefff365577d82274f2b05878e702238b8a575c5c8 /internal
parentfd0753c5463b4c54d09712336301e174f05e05ab (diff)
FSM/State: Profile correctness and connect name change
Also add a force tcp flag
Diffstat (limited to 'internal')
-rw-r--r--internal/api.go8
-rw-r--r--internal/fsm.go10
-rw-r--r--internal/server.go22
3 files changed, 31 insertions, 9 deletions
diff --git a/internal/api.go b/internal/api.go
index a987f00..b615976 100644
--- a/internal/api.go
+++ b/internal/api.go
@@ -24,8 +24,12 @@ func apiAuthorized(server Server, method string, endpoint string, opts *HTTPOpti
url := base.Endpoints.API.V3.API + endpoint
// Ensure we have valid tokens
+ stateBefore := base.FSM.Current
oauthErr := EnsureTokens(server)
+ // we reset the state so that we go from the authorized state to the state we want
+ base.FSM.Current = stateBefore
+
if oauthErr != nil {
return nil, nil, oauthErr
}
@@ -83,7 +87,11 @@ func APIInfo(server Server) error {
if baseErr != nil {
return &APIInfoError{Err: baseErr}
}
+
+ // Store the profiles and make sure that the current profile is not overwritten
+ previousProfile := base.Profiles.Current
base.Profiles = structure
+ base.Profiles.Current = previousProfile
base.ProfilesRaw = string(body)
return nil
}
diff --git a/internal/fsm.go b/internal/fsm.go
index 0b9ad1e..b52b463 100644
--- a/internal/fsm.go
+++ b/internal/fsm.go
@@ -106,12 +106,12 @@ func (fsm *FSM) Init(name string, callback func(string, string, string), logger
DEREGISTERED: {{NO_SERVER, "Client registers"}},
NO_SERVER: {{CHOSEN_SERVER, "User chooses a server"}},
CHOSEN_SERVER: {{AUTHORIZED, "Found tokens in config"}, {OAUTH_STARTED, "No tokens found in config"}},
- OAUTH_STARTED: {{AUTHORIZED, "User authorizes with browser"}, {CHOSEN_SERVER, "Cancel OAuth"}},
+ OAUTH_STARTED: {{AUTHORIZED, "User authorizes with browser"}, {NO_SERVER, "Cancel or Error"}},
AUTHORIZED: {{OAUTH_STARTED, "Re-authorize with OAuth"}, {REQUEST_CONFIG, "Client requests a config"}},
- REQUEST_CONFIG: {{ASK_PROFILE, "Multiple profiles found"}, {HAS_CONFIG, "Success, only one profile"}},
- ASK_PROFILE: {{HAS_CONFIG, "User chooses profile and success"}},
- HAS_CONFIG: {{CONNECTED, "OS reports connected"}},
- CONNECTED: {{AUTHORIZED, "OS reports disconnected"}},
+ REQUEST_CONFIG: {{ASK_PROFILE, "Multiple profiles found and no profile chosen"}, {HAS_CONFIG, "Only one profile or profile already chosen"}, {NO_SERVER, "Cancel or Error"}, {OAUTH_STARTED, "Re-authorize"}},
+ ASK_PROFILE: {{HAS_CONFIG, "User chooses profile"}, {NO_SERVER, "Done but no profile selected"}},
+ HAS_CONFIG: {{CONNECTED, "OS reports connected"}, {REQUEST_CONFIG, "User chooses a new profile"}, {NO_SERVER, "User wants to choose a new server"}},
+ CONNECTED: {{HAS_CONFIG, "OS reports disconnected"}},
}
fsm.Current = DEREGISTERED
fsm.Name = name
diff --git a/internal/server.go b/internal/server.go
index 9d27907..4452c6d 100644
--- a/internal/server.go
+++ b/internal/server.go
@@ -340,15 +340,29 @@ func GetConfig(server Server) (string, error) {
if !base.FSM.InState(REQUEST_CONFIG) {
return "", &FSMWrongStateError{Got: base.FSM.Current, Want: REQUEST_CONFIG}
}
- infoErr := APIInfo(server)
+ // Get new profiles using the info call
+ // This does not override the current profile
+ infoErr := APIInfo(server)
if infoErr != nil {
return "", &ServerGetConfigError{Err: infoErr}
}
- // Set the current profile if there is only one profile
- if len(base.Profiles.Info.ProfileList) == 1 {
- base.Profiles.Current = base.Profiles.Info.ProfileList[0].ID
+ // If there was a profile chosen and it doesn't exist anymore, reset it
+ if base.Profiles.Current != "" {
+ _, existsProfileErr := getCurrentProfile(server)
+ if existsProfileErr != nil {
+ base.Logger.Log(LOG_INFO, fmt.Sprintf("Profile %s no longer exists, resetting the profile", base.Profiles.Current))
+ base.Profiles.Current = ""
+ }
+ }
+
+ // Set the current profile if there is only one profile or profile is already selected
+ if len(base.Profiles.Info.ProfileList) == 1 || base.Profiles.Current != "" {
+ // Set the first profile if none is selected
+ if base.Profiles.Current == "" {
+ base.Profiles.Current = base.Profiles.Info.ProfileList[0].ID
+ }
return getConfigWithProfile(server)
}