diff --git a/acceptance/bundle/generate/dashboard_existing_path_nominal/out/dashboard/custom_key.lvdash.json b/acceptance/bundle/generate/dashboard_existing_path_nominal/out/dashboard/custom_key.lvdash.json new file mode 100644 index 00000000000..ea901f555c7 --- /dev/null +++ b/acceptance/bundle/generate/dashboard_existing_path_nominal/out/dashboard/custom_key.lvdash.json @@ -0,0 +1,9 @@ +{ + "pages": [ + { + "displayName": "New Page", + "layout": [], + "name": "[NUMID]" + } + ] +} diff --git a/acceptance/bundle/generate/dashboard_existing_path_nominal/out/resource/custom_key.dashboard.yml b/acceptance/bundle/generate/dashboard_existing_path_nominal/out/resource/custom_key.dashboard.yml new file mode 100644 index 00000000000..43eec1b9664 --- /dev/null +++ b/acceptance/bundle/generate/dashboard_existing_path_nominal/out/resource/custom_key.dashboard.yml @@ -0,0 +1,6 @@ +resources: + dashboards: + custom_key: + display_name: "This is a test dashboard" + warehouse_id: w4r3h0us3 + file_path: ../dashboard/custom_key.lvdash.json diff --git a/acceptance/bundle/generate/dashboard_existing_path_nominal/output.txt b/acceptance/bundle/generate/dashboard_existing_path_nominal/output.txt index de3519416c9..dcef0efdf55 100644 --- a/acceptance/bundle/generate/dashboard_existing_path_nominal/output.txt +++ b/acceptance/bundle/generate/dashboard_existing_path_nominal/output.txt @@ -2,3 +2,7 @@ >>> [CLI] bundle generate dashboard --existing-path /path/to/dashboard --dashboard-dir out/dashboard --resource-dir out/resource Writing dashboard to out/dashboard/this_is_a_test_dashboard.lvdash.json Writing configuration to out/resource/this_is_a_test_dashboard.dashboard.yml + +>>> [CLI] bundle generate dashboard --existing-path /path/to/dashboard --key custom_key --dashboard-dir out/dashboard --resource-dir out/resource +Writing dashboard to out/dashboard/custom_key.lvdash.json +Writing configuration to out/resource/custom_key.dashboard.yml diff --git a/acceptance/bundle/generate/dashboard_existing_path_nominal/script b/acceptance/bundle/generate/dashboard_existing_path_nominal/script index 60f26d10841..814370542d9 100644 --- a/acceptance/bundle/generate/dashboard_existing_path_nominal/script +++ b/acceptance/bundle/generate/dashboard_existing_path_nominal/script @@ -1,2 +1,4 @@ # Test generating dashboard from existing path trace $CLI bundle generate dashboard --existing-path /path/to/dashboard --dashboard-dir out/dashboard --resource-dir out/resource + +trace $CLI bundle generate dashboard --existing-path /path/to/dashboard --key custom_key --dashboard-dir out/dashboard --resource-dir out/resource diff --git a/cmd/bundle/generate/dashboard.go b/cmd/bundle/generate/dashboard.go index b6b01e8c528..47f7f180ce3 100644 --- a/cmd/bundle/generate/dashboard.go +++ b/cmd/bundle/generate/dashboard.go @@ -344,7 +344,11 @@ func (d *dashboard) generateForExisting(ctx context.Context, b *bundle.Bundle, d return } - key := textutil.NormalizeString(dashboard.DisplayName) + // The "key" flag is a persistent flag on the parent "generate" command. + key := d.cmd.Flag("key").Value.String() + if key == "" { + key = textutil.NormalizeString(dashboard.DisplayName) + } err = d.saveConfiguration(ctx, b, dashboard, key) if err != nil { logdiag.LogError(ctx, err) @@ -546,6 +550,11 @@ bundle files automatically, useful during active dashboard development.`, "existing-id", "resource", ) + cmd.MarkFlagsMutuallyExclusive( + "existing-path", + "existing-id", + "resource", + ) // Watch flag. This is relevant only in combination with the resource flag. cmd.Flags().BoolVar(&d.watch, "watch", false, `watch for changes to the dashboard and update the configuration`) diff --git a/cmd/bundle/generate/dashboard_test.go b/cmd/bundle/generate/dashboard_test.go index d7cb53ad46b..8c346250c8c 100644 --- a/cmd/bundle/generate/dashboard_test.go +++ b/cmd/bundle/generate/dashboard_test.go @@ -1,6 +1,7 @@ package generate import ( + "io" "testing" "github.com/databricks/cli/bundle" @@ -47,3 +48,20 @@ func TestDashboard_ErrorOnLegacyDashboard(t *testing.T) { require.Len(t, diags, 1) assert.Equal(t, "dashboard \"legacy dashboard\" is a legacy dashboard", diags[0].Summary) } + +func TestDashboard_LookupFlagsAreMutuallyExclusive(t *testing.T) { + tests := [][]string{ + {"--existing-path", "/path/to/dashboard", "--existing-id", "f00dcafe"}, + {"--existing-path", "/path/to/dashboard", "--resource", "test_dashboard"}, + {"--existing-id", "f00dcafe", "--resource", "test_dashboard"}, + } + + for _, args := range tests { + cmd := NewGenerateDashboardCommand() + cmd.SetArgs(args) + cmd.SetOut(io.Discard) + cmd.SetErr(io.Discard) + err := cmd.Execute() + assert.ErrorContains(t, err, "none of the others can be") + } +}