46
46
const autoConnect = "auto-connect" // annotation to indicate that a command needs to connect to the cluster
47
47
var autoConnectAnnotation = map [string ]string {autoConnect : "" }
48
48
49
+ const authNeeded = "auth-needed" // annotation to indicate that a command needs authorization
50
+ var authNeededAnnotation = map [string ]string {authNeeded : "" , autoConnect : "" } // auth implies auto-connect
51
+
49
52
var rootCmd = & cobra.Command {
50
53
SilenceUsage : true ,
51
54
SilenceErrors : true ,
@@ -83,6 +86,11 @@ var rootCmd = &cobra.Command{
83
86
84
87
client , tenantId = cli .Connect (server )
85
88
89
+ // Check if we are correctly logged in, but only if the command needs authorization
90
+ if _ , ok := cmd .Annotations [authNeeded ]; ! ok {
91
+ return nil
92
+ }
93
+
86
94
if err := cli .CheckLogin (cmd .Context (), client ); err != nil && ! nonInteractive {
87
95
// Login now; only do this for authorization-related errors
88
96
if connect .CodeOf (err ) != connect .CodeUnauthenticated {
@@ -134,7 +142,7 @@ var whoamiCmd = &cobra.Command{
134
142
135
143
var generateCmd = & cobra.Command {
136
144
Use : "generate" ,
137
- Annotations : autoConnectAnnotation ,
145
+ Annotations : authNeededAnnotation ,
138
146
Args : cobra .NoArgs ,
139
147
Aliases : []string {"gen" , "new" , "init" },
140
148
Short : "Generate a sample Defang project in the current folder" ,
@@ -143,11 +151,6 @@ var generateCmd = &cobra.Command{
143
151
return errors .New ("cannot run in non-interactive mode" )
144
152
}
145
153
146
- // check if we are properly connected / authenticated before asking the questions
147
- if err := cli .CheckLogin (cmd .Context (), client ); err != nil {
148
- return err
149
- }
150
-
151
154
var qs = []* survey.Question {
152
155
{
153
156
Name : "language" ,
@@ -230,7 +233,7 @@ Generate will write files in the current folder. You can edit them and then depl
230
233
231
234
var getServicesCmd = & cobra.Command {
232
235
Use : "services" ,
233
- Annotations : autoConnectAnnotation ,
236
+ Annotations : authNeededAnnotation ,
234
237
Args : cobra .NoArgs ,
235
238
Aliases : []string {"getServices" , "ls" , "list" },
236
239
Short : "Get list of services on the cluster" ,
@@ -262,7 +265,7 @@ var getVersionCmd = &cobra.Command{
262
265
263
266
var tailCmd = & cobra.Command {
264
267
Use : "tail" ,
265
- Annotations : autoConnectAnnotation ,
268
+ Annotations : authNeededAnnotation ,
266
269
Args : cobra .NoArgs ,
267
270
Short : "Tail logs from one or more services" ,
268
271
RunE : func (cmd * cobra.Command , args []string ) error {
@@ -291,7 +294,7 @@ var secretsCmd = &cobra.Command{
291
294
292
295
var secretsSetCmd = & cobra.Command {
293
296
Use : "set" ,
294
- Annotations : autoConnectAnnotation ,
297
+ Annotations : authNeededAnnotation ,
295
298
Args : cobra .NoArgs ,
296
299
Aliases : []string {"add" , "put" },
297
300
Short : "Adds or updates a secret" ,
@@ -335,7 +338,7 @@ var secretsSetCmd = &cobra.Command{
335
338
336
339
var secretsDeleteCmd = & cobra.Command {
337
340
Use : "delete" ,
338
- Annotations : autoConnectAnnotation ,
341
+ Annotations : authNeededAnnotation ,
339
342
Args : cobra .NoArgs ,
340
343
Aliases : []string {"del" , "rm" , "remove" },
341
344
Short : "Deletes a secret" ,
@@ -359,7 +362,7 @@ var secretsDeleteCmd = &cobra.Command{
359
362
360
363
var secretsListCmd = & cobra.Command {
361
364
Use : "ls" ,
362
- Annotations : autoConnectAnnotation ,
365
+ Annotations : authNeededAnnotation ,
363
366
Args : cobra .NoArgs ,
364
367
Aliases : []string {"list" },
365
368
Short : "List secrets" ,
@@ -393,7 +396,7 @@ func printEndpoints(serviceInfos []*defangv1.ServiceInfo) {
393
396
394
397
var composeUpCmd = & cobra.Command {
395
398
Use : "up" ,
396
- Annotations : autoConnectAnnotation ,
399
+ Annotations : authNeededAnnotation ,
397
400
Args : cobra .NoArgs ,
398
401
Short : "Like 'start' but immediately tracks the progress of the deployment" ,
399
402
RunE : func (cmd * cobra.Command , args []string ) error {
@@ -422,7 +425,7 @@ var composeUpCmd = &cobra.Command{
422
425
var composeStartCmd = & cobra.Command {
423
426
Use : "start" ,
424
427
Aliases : []string {"deploy" },
425
- Annotations : autoConnectAnnotation ,
428
+ Annotations : authNeededAnnotation ,
426
429
Args : cobra .NoArgs ,
427
430
Short : "Reads a docker-compose.yml file and deploys services to the cluster" ,
428
431
RunE : func (cmd * cobra.Command , args []string ) error {
@@ -448,7 +451,7 @@ var composeStartCmd = &cobra.Command{
448
451
var composeDownCmd = & cobra.Command {
449
452
Use : "down" ,
450
453
Aliases : []string {"stop" , "rm" },
451
- Annotations : autoConnectAnnotation ,
454
+ Annotations : authNeededAnnotation ,
452
455
Args : cobra .NoArgs ,
453
456
Short : "Reads a docker-compose.yml file and deletes services from the cluster" ,
454
457
RunE : func (cmd * cobra.Command , args []string ) error {
@@ -493,7 +496,7 @@ var composeConfigCmd = &cobra.Command{
493
496
494
497
var deleteCmd = & cobra.Command {
495
498
Use : "delete" ,
496
- Annotations : autoConnectAnnotation ,
499
+ Annotations : authNeededAnnotation ,
497
500
Args : cobra .NoArgs ,
498
501
Aliases : []string {"del" , "rm" , "remove" },
499
502
Short : "Delete a service from the cluster" ,
@@ -527,7 +530,7 @@ var deleteCmd = &cobra.Command{
527
530
var sendCmd = & cobra.Command {
528
531
Use : "send" ,
529
532
Hidden : true , // not available in private beta
530
- Annotations : autoConnectAnnotation ,
533
+ Annotations : authNeededAnnotation ,
531
534
Args : cobra .NoArgs ,
532
535
Aliases : []string {"msg" , "message" , "publish" , "pub" },
533
536
Short : "Send a message to a service" ,
@@ -544,7 +547,7 @@ var sendCmd = &cobra.Command{
544
547
545
548
var tokenCmd = & cobra.Command {
546
549
Use : "token" ,
547
- Annotations : autoConnectAnnotation ,
550
+ Annotations : authNeededAnnotation ,
548
551
Args : cobra .NoArgs ,
549
552
Short : "Manage personal access tokens" ,
550
553
RunE : func (cmd * cobra.Command , args []string ) error {
0 commit comments