@@ -26,6 +26,7 @@ const (
2626)
2727
2828type LoginCmd struct {
29+ format.Writer
2930 API API `embed:"" prefix:"api-"`
3031 Organization string `help:"Name of your organization to use when providing an API client ID/secret." env:"NCTL_ORGANIZATION"`
3132 IssuerURL string `help:"OIDC issuer URL of the API." default:"${issuer_url}" hidden:""`
@@ -69,7 +70,7 @@ func (l *LoginCmd) Run(ctx context.Context) error {
6970 if err != nil {
7071 return err
7172 }
72- return login (cfg , loadingRules .GetDefaultFilename (), userInfo .User , "" , project (l .Organization ))
73+ return login (l . Writer , cfg , loadingRules .GetDefaultFilename (), userInfo .User , "" , project (l .Organization ))
7374 }
7475
7576 if l .API .ClientID != "" {
@@ -92,7 +93,7 @@ func (l *LoginCmd) Run(ctx context.Context) error {
9293 if userInfo .Project != "" {
9394 proj = userInfo .Project
9495 }
95- return login (cfg , loadingRules .GetDefaultFilename (), userInfo .User , "" , project (proj ))
96+ return login (l . Writer , cfg , loadingRules .GetDefaultFilename (), userInfo .User , "" , project (proj ))
9697 }
9798
9899 if ! l .ForceInteractiveEnvOverride && ! format .IsInteractiveEnvironment (os .Stdout ) {
@@ -117,17 +118,33 @@ func (l *LoginCmd) Run(ctx context.Context) error {
117118
118119 org := userInfo .Orgs [0 ]
119120 if len (userInfo .Orgs ) > 1 {
120- fmt .Printf ("Multiple organizations found for the account %q.\n " , userInfo .User )
121- fmt .Printf ("Defaulting to %q\n " , org )
122- printAvailableOrgsString (org , userInfo .Orgs )
121+ l .Printf ("Multiple organizations found for the account %q.\n " , userInfo .User )
122+ l .Printf ("Defaulting to %q\n " , org )
123+ l . printAvailableOrgsString (org , userInfo .Orgs )
123124 }
124125
125126 cfg , err := newAPIConfig (apiURL , issuerURL , command , l .ClientID , withOrganization (org ))
126127 if err != nil {
127128 return err
128129 }
129130
130- return login (cfg , loadingRules .GetDefaultFilename (), userInfo .User , "" , project (org ))
131+ return login (l .Writer , cfg , loadingRules .GetDefaultFilename (), userInfo .User , "" , project (org ))
132+ }
133+
134+ func (l * LoginCmd ) printAvailableOrgsString (currentorg string , orgs []string ) {
135+ l .Println ("\n Available Organizations:" )
136+
137+ for _ , org := range orgs {
138+ activeMarker := ""
139+ if currentorg == org {
140+ activeMarker = "*"
141+ }
142+
143+ l .Printf ("%s\t %s\n " , activeMarker , org )
144+ }
145+
146+ l .Printf ("\n To switch the organization use the following command:\n " )
147+ l .Printf ("$ nctl auth set-org <org-name>\n " )
131148}
132149
133150func (l * LoginCmd ) tokenGetter () api.TokenGetter {
@@ -254,7 +271,7 @@ func switchCurrentContext() loginOption {
254271 }
255272}
256273
257- func login (newConfig * clientcmdapi.Config , kubeconfigPath , userName string , toOrg string , opts ... loginOption ) error {
274+ func login (w format. Writer , newConfig * clientcmdapi.Config , kubeconfigPath , userName string , toOrg string , opts ... loginOption ) error {
258275 loginConfig := & loginConfig {}
259276 for _ , opt := range opts {
260277 opt (loginConfig )
@@ -284,15 +301,15 @@ func login(newConfig *clientcmdapi.Config, kubeconfigPath, userName string, toOr
284301 }
285302
286303 if toOrg != "" {
287- format . PrintSuccessf ("🏢" , "switched to the organization %q" , toOrg )
304+ w . Successf ("🏢" , "switched to the organization %q\n " , toOrg )
288305 }
289- format . PrintSuccessf ("📋" , "added %s to kubeconfig" , newConfig .CurrentContext )
306+ w . Successf ("📋" , "added %s to kubeconfig\n " , newConfig .CurrentContext )
290307
291308 loginMessage := fmt .Sprintf ("logged into cluster %s" , newConfig .CurrentContext )
292309 if strings .TrimSpace (userName ) != "" {
293310 loginMessage = fmt .Sprintf ("logged into cluster %s as %s" , newConfig .CurrentContext , userName )
294311 }
295- format . PrintSuccess ("🚀" , loginMessage )
312+ w . Success ("🚀" , loginMessage + " \n " )
296313
297314 return nil
298315}
0 commit comments