|
| 1 | +// Copied from cmd/bundle/open.go and adapted for pipelines use. |
| 2 | +// Consider if changes made here should be made to the bundle counterpart as well. |
| 3 | +package pipelines |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + |
| 12 | + "github.com/databricks/cli/bundle" |
| 13 | + "github.com/databricks/cli/bundle/config/mutator" |
| 14 | + "github.com/databricks/cli/bundle/deploy/terraform" |
| 15 | + "github.com/databricks/cli/bundle/phases" |
| 16 | + "github.com/databricks/cli/bundle/resources" |
| 17 | + "github.com/databricks/cli/bundle/statemgmt" |
| 18 | + "github.com/databricks/cli/cmd/bundle/utils" |
| 19 | + "github.com/databricks/cli/cmd/root" |
| 20 | + "github.com/databricks/cli/libs/cmdio" |
| 21 | + "github.com/databricks/cli/libs/logdiag" |
| 22 | + "github.com/spf13/cobra" |
| 23 | + "golang.org/x/exp/maps" |
| 24 | + |
| 25 | + "github.com/pkg/browser" |
| 26 | +) |
| 27 | + |
| 28 | +func promptOpenArgument(ctx context.Context, b *bundle.Bundle) (string, error) { |
| 29 | + // Compute map of "Human readable name of resource" -> "resource key". |
| 30 | + inv := make(map[string]string) |
| 31 | + for k, ref := range resources.Completions(b) { |
| 32 | + title := fmt.Sprintf("%s: %s", ref.Description.SingularTitle, ref.Resource.GetName()) |
| 33 | + inv[title] = k |
| 34 | + } |
| 35 | + |
| 36 | + key, err := cmdio.Select(ctx, inv, "Pipeline to open") |
| 37 | + if err != nil { |
| 38 | + return "", err |
| 39 | + } |
| 40 | + |
| 41 | + return key, nil |
| 42 | +} |
| 43 | + |
| 44 | +func resolveOpenArgument(ctx context.Context, b *bundle.Bundle, args []string) (string, error) { |
| 45 | + // If no arguments are specified, prompt the user to select the resource to open. |
| 46 | + if len(args) == 0 && cmdio.IsPromptSupported(ctx) { |
| 47 | + return promptOpenArgument(ctx, b) |
| 48 | + } |
| 49 | + |
| 50 | + if len(args) < 1 { |
| 51 | + return "", errors.New("expected a KEY of the pipeline to open") |
| 52 | + } |
| 53 | + |
| 54 | + return args[0], nil |
| 55 | +} |
| 56 | + |
| 57 | +func openCommand() *cobra.Command { |
| 58 | + cmd := &cobra.Command{ |
| 59 | + Use: "open", |
| 60 | + Short: "Open a pipeline in the browser", |
| 61 | + Args: root.MaximumNArgs(1), |
| 62 | + } |
| 63 | + |
| 64 | + var forcePull bool |
| 65 | + cmd.Flags().BoolVar(&forcePull, "force-pull", false, "Skip local cache and load the state from the remote workspace") |
| 66 | + |
| 67 | + cmd.RunE = func(cmd *cobra.Command, args []string) error { |
| 68 | + ctx := logdiag.InitContext(cmd.Context()) |
| 69 | + cmd.SetContext(ctx) |
| 70 | + |
| 71 | + b := utils.ConfigureBundleWithVariables(cmd) |
| 72 | + if b == nil || logdiag.HasError(ctx) { |
| 73 | + return root.ErrAlreadyPrinted |
| 74 | + } |
| 75 | + |
| 76 | + phases.Initialize(ctx, b) |
| 77 | + if logdiag.HasError(ctx) { |
| 78 | + return root.ErrAlreadyPrinted |
| 79 | + } |
| 80 | + |
| 81 | + arg, err := resolveOpenArgument(ctx, b, args) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + |
| 86 | + cacheDir, err := terraform.Dir(ctx, b) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + _, stateFileErr := os.Stat(filepath.Join(cacheDir, b.StateFilename())) |
| 91 | + _, configFileErr := os.Stat(filepath.Join(cacheDir, terraform.TerraformConfigFileName)) |
| 92 | + noCache := errors.Is(stateFileErr, os.ErrNotExist) || errors.Is(configFileErr, os.ErrNotExist) |
| 93 | + |
| 94 | + if forcePull || noCache { |
| 95 | + bundle.ApplyContext(ctx, b, statemgmt.StatePull()) |
| 96 | + if logdiag.HasError(ctx) { |
| 97 | + return root.ErrAlreadyPrinted |
| 98 | + } |
| 99 | + |
| 100 | + if !b.DirectDeployment { |
| 101 | + bundle.ApplySeqContext(ctx, b, |
| 102 | + terraform.Interpolate(), |
| 103 | + terraform.Write(), |
| 104 | + ) |
| 105 | + } |
| 106 | + |
| 107 | + if logdiag.HasError(ctx) { |
| 108 | + return root.ErrAlreadyPrinted |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + bundle.ApplySeqContext(ctx, b, |
| 113 | + statemgmt.Load(), |
| 114 | + mutator.InitializeURLs(), |
| 115 | + ) |
| 116 | + if logdiag.HasError(ctx) { |
| 117 | + return root.ErrAlreadyPrinted |
| 118 | + } |
| 119 | + |
| 120 | + // Locate resource to open. |
| 121 | + ref, err := resources.Lookup(b, arg) |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + |
| 126 | + // Confirm that the resource has a URL. |
| 127 | + url := ref.Resource.GetURL() |
| 128 | + if url == "" { |
| 129 | + return errors.New("pipeline does not have a URL associated with it (has it been deployed?)") |
| 130 | + } |
| 131 | + |
| 132 | + cmdio.LogString(ctx, "Opening browser at "+url) |
| 133 | + return browser.OpenURL(url) |
| 134 | + } |
| 135 | + |
| 136 | + cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 137 | + b := root.MustConfigureBundle(cmd) |
| 138 | + if logdiag.HasError(cmd.Context()) { |
| 139 | + return nil, cobra.ShellCompDirectiveError |
| 140 | + } |
| 141 | + |
| 142 | + // No completion in the context of a bundle. |
| 143 | + // Source and destination paths are taken from bundle configuration. |
| 144 | + if b == nil { |
| 145 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 146 | + } |
| 147 | + |
| 148 | + if len(args) == 0 { |
| 149 | + completions := resources.Completions(b) |
| 150 | + return maps.Keys(completions), cobra.ShellCompDirectiveNoFileComp |
| 151 | + } else { |
| 152 | + return nil, cobra.ShellCompDirectiveNoFileComp |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return cmd |
| 157 | +} |
0 commit comments