Skip to content

Commit fe5ed03

Browse files
committed
cli: only check auth if needed
1 parent 86265df commit fe5ed03

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/cmd/cli/main.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ var (
4646
const autoConnect = "auto-connect" // annotation to indicate that a command needs to connect to the cluster
4747
var autoConnectAnnotation = map[string]string{autoConnect: ""}
4848

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+
4952
var rootCmd = &cobra.Command{
5053
SilenceUsage: true,
5154
SilenceErrors: true,
@@ -83,6 +86,11 @@ var rootCmd = &cobra.Command{
8386

8487
client, tenantId = cli.Connect(server)
8588

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+
8694
if err := cli.CheckLogin(cmd.Context(), client); err != nil && !nonInteractive {
8795
// Login now; only do this for authorization-related errors
8896
if connect.CodeOf(err) != connect.CodeUnauthenticated {
@@ -134,7 +142,7 @@ var whoamiCmd = &cobra.Command{
134142

135143
var generateCmd = &cobra.Command{
136144
Use: "generate",
137-
Annotations: autoConnectAnnotation,
145+
Annotations: authNeededAnnotation,
138146
Args: cobra.NoArgs,
139147
Aliases: []string{"gen", "new", "init"},
140148
Short: "Generate a sample Defang project in the current folder",
@@ -143,11 +151,6 @@ var generateCmd = &cobra.Command{
143151
return errors.New("cannot run in non-interactive mode")
144152
}
145153

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-
151154
var qs = []*survey.Question{
152155
{
153156
Name: "language",
@@ -230,7 +233,7 @@ Generate will write files in the current folder. You can edit them and then depl
230233

231234
var getServicesCmd = &cobra.Command{
232235
Use: "services",
233-
Annotations: autoConnectAnnotation,
236+
Annotations: authNeededAnnotation,
234237
Args: cobra.NoArgs,
235238
Aliases: []string{"getServices", "ls", "list"},
236239
Short: "Get list of services on the cluster",
@@ -262,7 +265,7 @@ var getVersionCmd = &cobra.Command{
262265

263266
var tailCmd = &cobra.Command{
264267
Use: "tail",
265-
Annotations: autoConnectAnnotation,
268+
Annotations: authNeededAnnotation,
266269
Args: cobra.NoArgs,
267270
Short: "Tail logs from one or more services",
268271
RunE: func(cmd *cobra.Command, args []string) error {
@@ -291,7 +294,7 @@ var secretsCmd = &cobra.Command{
291294

292295
var secretsSetCmd = &cobra.Command{
293296
Use: "set",
294-
Annotations: autoConnectAnnotation,
297+
Annotations: authNeededAnnotation,
295298
Args: cobra.NoArgs,
296299
Aliases: []string{"add", "put"},
297300
Short: "Adds or updates a secret",
@@ -335,7 +338,7 @@ var secretsSetCmd = &cobra.Command{
335338

336339
var secretsDeleteCmd = &cobra.Command{
337340
Use: "delete",
338-
Annotations: autoConnectAnnotation,
341+
Annotations: authNeededAnnotation,
339342
Args: cobra.NoArgs,
340343
Aliases: []string{"del", "rm", "remove"},
341344
Short: "Deletes a secret",
@@ -359,7 +362,7 @@ var secretsDeleteCmd = &cobra.Command{
359362

360363
var secretsListCmd = &cobra.Command{
361364
Use: "ls",
362-
Annotations: autoConnectAnnotation,
365+
Annotations: authNeededAnnotation,
363366
Args: cobra.NoArgs,
364367
Aliases: []string{"list"},
365368
Short: "List secrets",
@@ -393,7 +396,7 @@ func printEndpoints(serviceInfos []*defangv1.ServiceInfo) {
393396

394397
var composeUpCmd = &cobra.Command{
395398
Use: "up",
396-
Annotations: autoConnectAnnotation,
399+
Annotations: authNeededAnnotation,
397400
Args: cobra.NoArgs,
398401
Short: "Like 'start' but immediately tracks the progress of the deployment",
399402
RunE: func(cmd *cobra.Command, args []string) error {
@@ -422,7 +425,7 @@ var composeUpCmd = &cobra.Command{
422425
var composeStartCmd = &cobra.Command{
423426
Use: "start",
424427
Aliases: []string{"deploy"},
425-
Annotations: autoConnectAnnotation,
428+
Annotations: authNeededAnnotation,
426429
Args: cobra.NoArgs,
427430
Short: "Reads a docker-compose.yml file and deploys services to the cluster",
428431
RunE: func(cmd *cobra.Command, args []string) error {
@@ -448,7 +451,7 @@ var composeStartCmd = &cobra.Command{
448451
var composeDownCmd = &cobra.Command{
449452
Use: "down",
450453
Aliases: []string{"stop", "rm"},
451-
Annotations: autoConnectAnnotation,
454+
Annotations: authNeededAnnotation,
452455
Args: cobra.NoArgs,
453456
Short: "Reads a docker-compose.yml file and deletes services from the cluster",
454457
RunE: func(cmd *cobra.Command, args []string) error {
@@ -493,7 +496,7 @@ var composeConfigCmd = &cobra.Command{
493496

494497
var deleteCmd = &cobra.Command{
495498
Use: "delete",
496-
Annotations: autoConnectAnnotation,
499+
Annotations: authNeededAnnotation,
497500
Args: cobra.NoArgs,
498501
Aliases: []string{"del", "rm", "remove"},
499502
Short: "Delete a service from the cluster",
@@ -527,7 +530,7 @@ var deleteCmd = &cobra.Command{
527530
var sendCmd = &cobra.Command{
528531
Use: "send",
529532
Hidden: true, // not available in private beta
530-
Annotations: autoConnectAnnotation,
533+
Annotations: authNeededAnnotation,
531534
Args: cobra.NoArgs,
532535
Aliases: []string{"msg", "message", "publish", "pub"},
533536
Short: "Send a message to a service",
@@ -544,7 +547,7 @@ var sendCmd = &cobra.Command{
544547

545548
var tokenCmd = &cobra.Command{
546549
Use: "token",
547-
Annotations: autoConnectAnnotation,
550+
Annotations: authNeededAnnotation,
548551
Args: cobra.NoArgs,
549552
Short: "Manage personal access tokens",
550553
RunE: func(cmd *cobra.Command, args []string) error {

0 commit comments

Comments
 (0)