From 277f90165d8a0bfef0387877267ba3ae0597bf29 Mon Sep 17 00:00:00 2001
From: Eric Liu <eric.liu@defang.io>
Date: Fri, 20 Dec 2024 13:55:30 -0800
Subject: [PATCH] update code review

---
 src/cmd/cli/command/cd.go       | 11 +---------
 src/cmd/cli/command/commands.go | 36 +++++++--------------------------
 2 files changed, 8 insertions(+), 39 deletions(-)

diff --git a/src/cmd/cli/command/cd.go b/src/cmd/cli/command/cd.go
index 2fadb4e3..9e014e96 100644
--- a/src/cmd/cli/command/cd.go
+++ b/src/cmd/cli/command/cd.go
@@ -137,20 +137,11 @@ var cdListCmd = &cobra.Command{
 		}
 
 		if remote {
-			loader := configureLoader(cmd)
-
-			projectName, err := cliClient.LoadProjectNameWithFallback(cmd.Context(), loader, provider)
+			provider, err = canIUseProvider(cmd.Context(), provider, "")
 			if err != nil {
 				return err
 			}
 
-			provider, err = canIUseProvider(cmd.Context(), provider, projectName)
-			if err != nil {
-				return err
-			}
-		}
-
-		if remote {
 			// FIXME: this needs auth because it spawns the CD task
 			return cli.BootstrapCommand(cmd.Context(), "", client, provider, "list")
 		}
diff --git a/src/cmd/cli/command/commands.go b/src/cmd/cli/command/commands.go
index 5c5ce57e..8ca36341 100644
--- a/src/cmd/cli/command/commands.go
+++ b/src/cmd/cli/command/commands.go
@@ -77,21 +77,6 @@ func prettyError(err error) error {
 	return err
 }
 
-// TODO: Make call to provider to get project cd version and pass to CanIUse API call
-func allowToUseProvider(ctx context.Context, providerID cliClient.ProviderID, projectName string) (string, error) {
-	canUseReq := defangv1.CanIUseRequest{
-		Project:  projectName,
-		Provider: providerID.EnumValue(),
-	}
-
-	resp, err := client.CanIUse(ctx, &canUseReq)
-	if err != nil {
-		return "", err
-	}
-
-	return resp.CdImage, nil
-}
-
 func Execute(ctx context.Context) error {
 	if term.StdoutCanColor() { // TODO: should use DoColor(…) instead
 		restore := term.EnableANSI()
@@ -1136,24 +1121,17 @@ func getProvider(ctx context.Context, loader cliClient.Loader) (cliClient.Provid
 }
 
 func canIUseProvider(ctx context.Context, provider cliClient.Provider, projectName string) (cliClient.Provider, error) {
-	cdImage, err := allowToUseProvider(ctx, providerID, projectName)
-	if err != nil {
-		return nil, err
+	canUseReq := defangv1.CanIUseRequest{
+		Project:  projectName,
+		Provider: providerID.EnumValue(),
 	}
 
-	// provide sane defaults for the CD image
-	if cdImage == "" {
-		switch providerID {
-		case cliClient.ProviderAWS:
-			cdImage = "public.ecr.aws/defang-io/cd:public-beta"
-		case cliClient.ProviderDO:
-			cdImage = "docker.io/defangio/cd:public-beta"
-		case cliClient.ProviderGCP:
-			cdImage = "docker.io/defangio/cd:pubilc-gcp-beta"
-		}
+	resp, err := client.CanIUse(ctx, &canUseReq)
+	if err != nil {
+		return nil, err
 	}
 	// Allow local override of the CD image
-	cdImage = pkg.Getenv("DEFANG_CD_IMAGE", cdImage)
+	cdImage := pkg.Getenv("DEFANG_CD_IMAGE", resp.CdImage)
 	provider.SetCDImage(cdImage)
 
 	return provider, nil