Refactor token management, client initialization, and project handling

- Consolidated `TokenStorage` struct into `internal/auth/models.go` for better organization.
- Updated `Client` to use `TokenStorage` for managing email and project ID.
- Simplified `SetupUser` method to ensure proper token and project assignment.
- Refactored API handlers to leverage new `GetEmail` and `GetProjectID` methods in `Client`.
- Cleanup: Removed unused structures and redundant code from `client.go` and `auth.go`.
- Adjusted CLI flow in `login.go` and `run.go` for streamlined user onboarding.
This commit is contained in:
Luis Pater
2025-07-04 17:08:58 +08:00
parent 57ead9a4bc
commit 582280f4c5
7 changed files with 116 additions and 130 deletions

View File

@@ -29,7 +29,7 @@ func DoLogin(cfg *config.Config, projectID string) {
// 3. Initialize CLI Client
cliClient := client.NewClient(httpClient, &ts, cfg)
projectID, err = cliClient.SetupUser(clientCtx, ts.Email, 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")
@@ -52,8 +52,7 @@ func DoLogin(cfg *config.Config, projectID string) {
log.Fatalf("failed to complete user setup: %v", err)
}
} else {
auto := ts.ProjectID == ""
cliClient.SetProjectID(projectID)
auto := projectID == ""
cliClient.SetIsAuto(auto)
if !cliClient.IsChecked() && !cliClient.IsAuto() {

View File

@@ -57,30 +57,7 @@ func StartService(cfg *config.Config) {
// 3. Initialize CLI Client
cliClient := client.NewClient(httpClient, &ts, cfg)
if _, err = cliClient.SetupUser(clientCtx, ts.Email, ts.ProjectID); err != nil {
if err.Error() == "failed to start user onboarding, need define a project id" {
log.Error("failed to start user onboarding")
project, errGetProjectList := cliClient.GetProjectList(clientCtx)
if errGetProjectList != nil {
log.Fatalf("failed to complete user setup: %v", err)
} else {
log.Infof("Your account %s needs specify a project id.", ts.Email)
log.Info("========================================================================")
for i := 0; i < len(project.Projects); i++ {
log.Infof("Project ID: %s", project.Projects[i].ProjectID)
log.Infof("Project Name: %s", project.Projects[i].Name)
log.Info("========================================================================")
}
log.Infof("Please run this command to login again:\n\n%s --login --project_id <project_id>\n", os.Args[0])
}
} else {
// Log as a warning because in some cases, the CLI might still be usable
// or the user might want to retry setup later.
log.Fatalf("failed to complete user setup: %v", err)
}
} else {
cliClients = append(cliClients, cliClient)
}
cliClients = append(cliClients, cliClient)
}
}
return nil