Refactor user onboarding and token management
goreleaser / goreleaser (push) Has been cancelled

- Enhanced the `Client` initialization to include `TokenStorage` and configuration parameters.
- Replaced `SaveTokenToFile` with a `Client` method for better encapsulation.
- Improved onboarding flow with project ID verification and API enablement checks.
- Refactored token saving logic to ensure proper handling of directory creation and JSON encoding.
- Removed unused file-related code in `auth.go` for improved maintainability.
This commit is contained in:
Luis Pater
2025-07-04 07:53:07 +08:00
parent 79acea5976
commit 57ead9a4bc
4 changed files with 155 additions and 74 deletions
+20 -4
View File
@@ -28,8 +28,8 @@ func DoLogin(cfg *config.Config, projectID string) {
log.Info("Authentication successful.")
// 3. Initialize CLI Client
cliClient := client.NewClient(httpClient)
projectID, err = cliClient.SetupUser(clientCtx, ts.Email, projectID, ts.Auto)
cliClient := client.NewClient(httpClient, &ts, cfg)
projectID, err = cliClient.SetupUser(clientCtx, ts.Email, projectID)
if err != nil {
if err.Error() == "failed to start user onboarding, need define a project id" {
log.Error("failed to start user onboarding")
@@ -53,10 +53,26 @@ func DoLogin(cfg *config.Config, projectID string) {
}
} else {
auto := ts.ProjectID == ""
ts.ProjectID = projectID
err = auth.SaveTokenToFile(&ts, cfg, auto)
cliClient.SetProjectID(projectID)
cliClient.SetIsAuto(auto)
if !cliClient.IsChecked() && !cliClient.IsAuto() {
isChecked, checkErr := cliClient.CheckCloudAPIIsEnabled()
if checkErr != nil {
log.Fatalf("failed to check cloud api is enabled: %v", checkErr)
return
}
cliClient.SetIsChecked(isChecked)
}
if !cliClient.IsChecked() && !cliClient.IsAuto() {
return
}
err = cliClient.SaveTokenToFile()
if err != nil {
log.Fatal(err)
return
}
}
}