@@ -19,6 +19,7 @@ import (
19
19
"github.com/gitpod-io/local-app/pkg/auth"
20
20
"github.com/gitpod-io/local-app/pkg/config"
21
21
"github.com/gitpod-io/local-app/pkg/prettyprint"
22
+ "github.com/manifoldco/promptui"
22
23
"github.com/spf13/cobra"
23
24
)
24
25
@@ -27,6 +28,7 @@ var loginOpts struct {
27
28
Host string
28
29
ContextName string
29
30
OrganizationID string
31
+ NonInteractive bool
30
32
}
31
33
32
34
// loginCmd represents the login command
@@ -51,15 +53,19 @@ var loginCmd = &cobra.Command{
51
53
token = os .Getenv ("GITPOD_TOKEN" )
52
54
}
53
55
if token == "" {
54
- var err error
55
- token , err = auth .Login (context .Background (), auth.LoginOpts {
56
- GitpodURL : loginOpts .Host ,
57
- AuthTimeout : 5 * time .Minute ,
58
- // Request CLI scopes (extended compared to the local companion app)
59
- ExtendScopes : true ,
60
- })
61
- if err != nil {
62
- return err
56
+ if loginOpts .NonInteractive {
57
+ return fmt .Errorf ("no token provided" )
58
+ } else {
59
+ var err error
60
+ token , err = auth .Login (context .Background (), auth.LoginOpts {
61
+ GitpodURL : loginOpts .Host ,
62
+ AuthTimeout : 5 * time .Minute ,
63
+ // Request CLI scopes (extended compared to the local companion app)
64
+ ExtendScopes : true ,
65
+ })
66
+ if err != nil {
67
+ return err
68
+ }
63
69
}
64
70
}
65
71
@@ -91,6 +97,9 @@ var loginCmd = &cobra.Command{
91
97
if err != nil {
92
98
return fmt .Errorf ("cannot connect to Gitpod with this context: %w" , err )
93
99
}
100
+ if ! loginOpts .NonInteractive {
101
+ fmt .Println ("loading your organizations..." )
102
+ }
94
103
orgsList , err := clnt .Teams .ListTeams (cmd .Context (), connect .NewRequest (& v1.ListTeamsRequest {}))
95
104
if err != nil {
96
105
var (
@@ -117,15 +126,47 @@ var loginCmd = &cobra.Command{
117
126
}
118
127
}
119
128
129
+ orgs := orgsList .Msg .GetTeams ()
130
+ fmt .Print ("\033 [A\033 [K" )
131
+
132
+ resolutions := []string {
133
+ "pass an organization ID using --organization-id" ,
134
+ }
135
+
120
136
var orgID string
121
- switch len (orgsList . Msg . GetTeams () ) {
137
+ switch len (orgs ) {
122
138
case 0 :
123
- return fmt .Errorf ("no organizations found. Please pass an organization ID using --organization-id" )
139
+ return prettyprint . AddResolution ( fmt .Errorf ("no organizations found" ), resolutions ... )
124
140
case 1 :
125
- orgID = orgsList . Msg . GetTeams () [0 ].Id
141
+ orgID = orgs [0 ].Id
126
142
default :
127
- orgID = orgsList .Msg .GetTeams ()[0 ].Id
128
- slog .Info ("found more than one organization and choose the first one" , "org" , orgID )
143
+ if loginOpts .NonInteractive {
144
+ resolutions = append (resolutions ,
145
+ "omit --non-interactive and select an organization interactively" ,
146
+ )
147
+ return prettyprint .AddResolution (fmt .Errorf ("found more than one organization" ), resolutions ... )
148
+ }
149
+
150
+ var orgNames []string
151
+ for _ , org := range orgs {
152
+ orgNames = append (orgNames , org .Name )
153
+ }
154
+
155
+ prompt := promptui.Select {
156
+ Label : "What organization would you like to use?" ,
157
+ Items : orgNames ,
158
+ Templates : & promptui.SelectTemplates {
159
+ Selected : "Selected organization {{ . }}" ,
160
+ },
161
+ }
162
+ selectedIndex , selectedValue , err := prompt .Run ()
163
+ if selectedValue == "" {
164
+ return fmt .Errorf ("no organization selected" )
165
+ }
166
+ if err != nil {
167
+ return err
168
+ }
169
+ orgID = orgs [selectedIndex ].Id
129
170
}
130
171
cfg .Contexts [contextName ].OrganizationID = orgID
131
172
}
@@ -144,7 +185,7 @@ var loginCmd = &cobra.Command{
144
185
return err
145
186
}
146
187
147
- slog .Info ("Login succesfull " )
188
+ slog .Info ("login successful " )
148
189
fmt .Println ()
149
190
return WriteTabular (who , formatOpts {}, prettyprint .WriterFormatNarrow )
150
191
},
@@ -161,4 +202,5 @@ func init() {
161
202
loginCmd .Flags ().StringVar (& loginOpts .Token , "token" , "" , "The token to use for authentication (defaults to $GITPOD_TOKEN)" )
162
203
loginCmd .Flags ().StringVarP (& loginOpts .ContextName , "context-name" , "n" , "default" , "The name of the context to create" )
163
204
loginCmd .Flags ().StringVar (& loginOpts .OrganizationID , "org" , "" , "The organization ID to use for the context" )
205
+ loginCmd .Flags ().BoolVar (& loginOpts .NonInteractive , "non-interactive" , false , "Disable opening the browser and prompt to select an organization" )
164
206
}
0 commit comments