99 "fmt"
1010 "io"
1111 "net/http"
12+ "net/url"
1213 "time"
1314
1415 "github.com/kusaridev/kusari-cli/pkg/auth"
@@ -27,15 +28,23 @@ func Login(ctx context.Context, clientId, clientSecret, redirectUrl, authEndpoin
2728 fmt .Println ()
2829 }
2930
30- token , err := auth .Authenticate (ctx , clientId , clientSecret , redirectUrl , authEndpoint , redirectPort , consoleUrl )
31+ // Check if there's a previously stored workspace for this platform and auth endpoint
32+ // This allows us to include the workspace in the redirect URL
33+ workspaceId := ""
34+ previousWorkspace , _ := auth .LoadWorkspace (platformUrl , currentAuthEndpoint )
35+ if previousWorkspace != nil {
36+ workspaceId = previousWorkspace .ID
37+ fmt .Printf ("\n Using stored workspace: %s\n " , previousWorkspace .Description )
38+ }
39+
40+ token , err := auth .Authenticate (ctx , clientId , clientSecret , redirectUrl , authEndpoint , redirectPort , consoleUrl , workspaceId )
3141 if err != nil {
3242 return err
3343 }
3444
3545 fmt .Println ("Successfully logged in!" )
3646
37- // Check if there's a previously stored workspace for this platform and auth endpoint
38- previousWorkspace , _ := auth .LoadWorkspace (platformUrl , currentAuthEndpoint )
47+ // If we already had a workspace, we're done
3948 if previousWorkspace != nil {
4049 fmt .Printf ("\n Your current workspace is: %s\n " , previousWorkspace .Description )
4150 fmt .Println ("To change workspaces, run: kusari auth select-workspace" )
@@ -82,6 +91,23 @@ func Login(ctx context.Context, clientId, clientSecret, redirectUrl, authEndpoin
8291 fmt .Printf ("\n Workspace '%s' has been set as your active workspace.\n " , selectedWorkspace .Description )
8392 fmt .Println ("To change workspaces later, run: kusari auth select-workspace" )
8493
94+ // Now that we have a workspace, redirect to the console with the workspace parameter
95+ baseURL , err := urlBuilder .Build (consoleUrl , "/analysis" )
96+ if err == nil && baseURL != nil {
97+ // Parse the URL and add workspace as query parameter
98+ parsedURL , parseErr := url .Parse (* baseURL )
99+ if parseErr == nil {
100+ query := parsedURL .Query ()
101+ query .Set ("workspaceId" , selectedWorkspace .ID )
102+ parsedURL .RawQuery = query .Encode ()
103+
104+ fmt .Println ("\n Opening console in your browser..." )
105+ if err := auth .OpenBrowser (parsedURL .String ()); err != nil {
106+ fmt .Printf ("Failed to open browser automatically. Please visit: %s\n " , parsedURL .String ())
107+ }
108+ }
109+ }
110+
85111 // ANSI escape codes:
86112 // \033[1m = bold
87113 // \033[34m = blue
@@ -149,5 +175,11 @@ func FetchWorkspaces(platformUrl string, accessToken string) ([]Workspace, error
149175 return nil , fmt .Errorf ("no workspaces found for this user" )
150176 }
151177
178+ for i := range result .Workspaces {
179+ if result .Workspaces [i ].Description == "" {
180+ result .Workspaces [i ].Description = "My Workspace"
181+ }
182+ }
183+
152184 return result .Workspaces , nil
153185}
0 commit comments