Skip to content

Commit d6849e5

Browse files
authored
feat: support --mode for cd preview (#1151)
1 parent c8edb98 commit d6849e5

File tree

6 files changed

+18
-10
lines changed

6 files changed

+18
-10
lines changed

src/cmd/cli/command/cd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,6 @@ var cdPreviewCmd = &cobra.Command{
198198
return err
199199
}
200200

201-
return cli.Preview(cmd.Context(), project, client, provider) // TODO: support --mode #1022
201+
return cli.Preview(cmd.Context(), project, client, provider, mode.Value())
202202
},
203203
}

src/cmd/cli/command/commands.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var (
4646
gitHubClientId = pkg.Getenv("DEFANG_CLIENT_ID", "7b41848ca116eac4b125") // GitHub OAuth app
4747
hasTty = term.IsTerminal() && !pkg.GetenvBool("CI")
4848
hideUpdate = pkg.GetenvBool("DEFANG_HIDE_UPDATE")
49+
mode = Mode(defangv1.DeploymentMode_MODE_UNSPECIFIED)
4950
modelId = os.Getenv("DEFANG_MODEL_ID") // for Pro users only
5051
nonInteractive = !hasTty
5152
org string
@@ -177,6 +178,7 @@ func SetupCommands(ctx context.Context, version string) {
177178
cdListCmd.Flags().Bool("remote", false, "invoke the command on the remote cluster")
178179
cdCmd.AddCommand(cdListCmd)
179180
cdCmd.AddCommand(cdCancelCmd)
181+
cdPreviewCmd.Flags().VarP(&mode, "mode", "m", fmt.Sprintf("deployment mode; one of %v", allModes()))
180182
cdCmd.AddCommand(cdPreviewCmd)
181183

182184
// Eula command

src/cmd/cli/command/compose.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"github.com/DefangLabs/defang/src/pkg/track"
1919
"github.com/DefangLabs/defang/src/pkg/types"
2020
defangv1 "github.com/DefangLabs/defang/src/protos/io/defang/v1"
21-
2221
"github.com/bufbuild/connect-go"
2322
"github.com/spf13/cobra"
2423
)
@@ -49,7 +48,6 @@ func createProjectForDebug(loader *compose.Loader) (*compose.Project, error) {
4948
}
5049

5150
func makeComposeUpCmd() *cobra.Command {
52-
mode := Mode(defangv1.DeploymentMode_DEVELOPMENT)
5351
composeUpCmd := &cobra.Command{
5452
Use: "up",
5553
Annotations: authNeededAnnotation,
@@ -199,7 +197,7 @@ func makeComposeUpCmd() *cobra.Command {
199197
composeUpCmd.Flags().Bool("utc", false, "show logs in UTC timezone (ie. TZ=UTC)")
200198
composeUpCmd.Flags().Bool("tail", false, "tail the service logs after updating") // obsolete, but keep for backwards compatibility
201199
_ = composeUpCmd.Flags().MarkHidden("tail")
202-
composeUpCmd.Flags().VarP(&mode, "mode", "m", "deployment mode, possible values: "+strings.Join(allModes(), ", "))
200+
composeUpCmd.Flags().VarP(&mode, "mode", "m", fmt.Sprintf("deployment mode; one of %v", allModes()))
203201
composeUpCmd.Flags().Bool("build", true, "build the image before starting the service") // docker-compose compatibility
204202
_ = composeUpCmd.Flags().MarkHidden("build")
205203
composeUpCmd.Flags().Bool("wait", true, "wait for services to be running|healthy") // docker-compose compatibility
@@ -511,7 +509,7 @@ func makeComposeLogsCmd() *cobra.Command {
511509
logsCmd.Flags().String("since", "", "show logs since duration/time")
512510
logsCmd.Flags().String("until", "", "show logs until duration/time")
513511
logsCmd.Flags().Bool("utc", false, "show logs in UTC timezone (ie. TZ=UTC)")
514-
logsCmd.Flags().Var(&logType, "type", fmt.Sprintf(`show logs of type; one of %v`, logs.AllLogTypes))
512+
logsCmd.Flags().Var(&logType, "type", fmt.Sprintf("show logs of type; one of %v", logs.AllLogTypes))
515513
logsCmd.Flags().String("filter", "", "only show logs containing given text; case-insensitive")
516514
return logsCmd
517515
}

src/cmd/cli/command/mode.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package command
22

33
import (
44
"fmt"
5+
"slices"
56
"strings"
67

78
defangv1 "github.com/DefangLabs/defang/src/protos/io/defang/v1"
@@ -10,16 +11,21 @@ import (
1011
type Mode defangv1.DeploymentMode
1112

1213
func (b Mode) String() string {
14+
if b == 0 {
15+
return ""
16+
}
1317
return strings.ToLower(defangv1.DeploymentMode_name[int32(b)])
1418
}
19+
1520
func (b *Mode) Set(s string) error {
1621
mode, ok := defangv1.DeploymentMode_value[strings.ToUpper(s)]
1722
if !ok {
18-
return fmt.Errorf("invalid mode: %s, valid values are: %v", s, strings.Join(allModes(), ", "))
23+
return fmt.Errorf("invalid mode: %s, not one of %v", s, allModes())
1924
}
2025
*b = Mode(mode)
2126
return nil
2227
}
28+
2329
func (b Mode) Type() string {
2430
return "mode"
2531
}
@@ -36,5 +42,6 @@ func allModes() []string {
3642
}
3743
modes = append(modes, strings.ToLower(mode))
3844
}
45+
slices.Sort(modes) // TODO: sort by enum value instead of string
3946
return modes
4047
}

src/pkg/cli/preview.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
defangv1 "github.com/DefangLabs/defang/src/protos/io/defang/v1"
1010
)
1111

12-
func Preview(ctx context.Context, project *compose.Project, fabric cliClient.FabricClient, provider cliClient.Provider) error {
13-
resp, project, err := ComposeUp(ctx, project, fabric, provider, compose.UploadModePreview, defangv1.DeploymentMode_MODE_UNSPECIFIED)
12+
func Preview(ctx context.Context, project *compose.Project, fabric cliClient.FabricClient, provider cliClient.Provider, mode defangv1.DeploymentMode) error {
13+
resp, project, err := ComposeUp(ctx, project, fabric, provider, compose.UploadModePreview, mode)
1414
if err != nil {
1515
return err
1616
}

src/pkg/cli/preview_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/DefangLabs/defang/src/pkg/cli/client"
1111
"github.com/DefangLabs/defang/src/pkg/cli/compose"
1212
"github.com/DefangLabs/defang/src/pkg/clouds/aws/ecs"
13+
defangv1 "github.com/DefangLabs/defang/src/protos/io/defang/v1"
1314
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
1415
)
1516

@@ -45,7 +46,7 @@ func TestPreviewStops(t *testing.T) {
4546
deploymentStatus: tt.err,
4647
}
4748

48-
err := Preview(context.Background(), project, fabric, provider)
49+
err := Preview(context.Background(), project, fabric, provider, defangv1.DeploymentMode_MODE_UNSPECIFIED)
4950
if err != nil {
5051
if err.Error() != tt.wantError {
5152
t.Errorf("got error: %v, want: %v", err, tt.wantError)
@@ -67,7 +68,7 @@ func TestPreviewStops(t *testing.T) {
6768

6869
provider := &mockDeployProvider{}
6970

70-
err := Preview(ctx, project, fabric, provider)
71+
err := Preview(ctx, project, fabric, provider, defangv1.DeploymentMode_MODE_UNSPECIFIED)
7172
if err != nil {
7273
t.Errorf("got error: %v, want nil", err)
7374
}

0 commit comments

Comments
 (0)