databricks-cli/cmd/bundle/exec.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

153 lines
4.5 KiB
Go
Raw Permalink Normal View History

2025-03-02 17:11:46 +00:00
package bundle
import (
2025-03-04 17:22:12 +00:00
"bufio"
2025-03-02 17:11:46 +00:00
"fmt"
"os/exec"
"strings"
2025-03-04 17:22:12 +00:00
"sync"
2025-03-02 17:11:46 +00:00
2025-03-03 17:26:17 +00:00
"github.com/databricks/cli/bundle/config/mutator"
2025-03-02 17:11:46 +00:00
"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/auth"
"github.com/spf13/cobra"
)
func newExecCommand() *cobra.Command {
execCmd := &cobra.Command{
Use: "exec",
Short: "Execute a command using the same authentication context as the bundle",
Args: cobra.MinimumNArgs(1),
2025-03-03 18:19:39 +00:00
Long: `Execute a command using the same authentication context as the bundle
2025-03-03 14:12:50 +00:00
2025-03-03 18:19:39 +00:00
The current working directory of the provided command will be set to the root
of the bundle.
Example usage:
2025-03-03 19:06:24 +00:00
1. databricks bundle exec -- echo "hello, world"
2. databricks bundle exec -- /bin/bash -c "echo hello"
3. databricks bundle exec -- uv run pytest`,
2025-03-02 17:11:46 +00:00
RunE: func(cmd *cobra.Command, args []string) error {
if cmd.ArgsLenAtDash() != 0 {
return fmt.Errorf("Please add a '--' separator. Usage: 'databricks bundle exec -- %s'", strings.Join(args, " "))
}
2025-03-03 19:07:51 +00:00
// Load the bundle configuration to get the authentication credentials.
2025-03-03 14:12:50 +00:00
b, diags := root.MustConfigureBundle(cmd)
2025-03-02 17:11:46 +00:00
if diags.HasError() {
return diags.Error()
}
2025-03-03 13:01:41 +00:00
childCmd := exec.Command(args[0], args[1:]...)
2025-03-03 14:19:18 +00:00
2025-03-03 15:58:57 +00:00
env := auth.ProcessEnv(root.ConfigUsed(cmd.Context()))
2025-03-03 17:26:17 +00:00
2025-03-03 19:06:24 +00:00
// If user has specified a target, pass it to the child command. DABs
2025-03-03 19:08:33 +00:00
// defines a "default" target which is a placeholder for when no target is defined.
2025-03-03 19:06:24 +00:00
// If that's the case, i.e. no targets are defined, then do not pass the target.
//
2025-03-03 17:26:17 +00:00
// This is only useful for when the Databricks CLI is the child command.
2025-03-03 18:58:14 +00:00
if b.Config.Bundle.Target != mutator.DefaultTargetPlaceholder {
2025-03-03 17:26:17 +00:00
env = append(env, "DATABRICKS_BUNDLE_TARGET="+b.Config.Bundle.Target)
}
2025-03-03 17:13:56 +00:00
2025-03-03 19:06:24 +00:00
// If the bundle has a profile configured, explicitly pass it to the child command.
//
// This is only useful for when the Databricks CLI is the child command,
// since if we do not explicitly pass the profile, the CLI will use the
2025-03-05 10:39:09 +00:00
// auth configured in the bundle YAML configuration (if any).
2025-03-03 17:13:56 +00:00
if b.Config.Workspace.Profile != "" {
env = append(env, "DATABRICKS_CONFIG_PROFILE="+b.Config.Workspace.Profile)
}
2025-03-03 17:26:17 +00:00
2025-03-03 15:58:57 +00:00
childCmd.Env = env
2025-03-02 17:11:46 +00:00
2025-03-03 14:19:18 +00:00
// Execute all scripts from the bundle root directory. This behavior can
// be surprising in isolation, but we do it to keep the behavior consistent
2025-03-03 19:10:27 +00:00
// for both these cases:
2025-03-03 14:19:18 +00:00
// 1. One shot commands like `databricks bundle exec -- echo hello`
2025-03-03 19:10:27 +00:00
// 2. (upcoming) Scripts that are defined in the scripts section of the DAB.
2025-03-03 14:19:18 +00:00
//
// TODO(shreyas): Add a DATABRICKS_BUNDLE_INITIAL_CWD environment variable
// that users can read to figure out the original CWD. I'll do that when
// adding support for the scripts section.
2025-03-03 14:12:50 +00:00
childCmd.Dir = b.BundleRootPath
2025-03-04 17:22:12 +00:00
stdout, err := childCmd.StdoutPipe()
if err != nil {
return fmt.Errorf("creating stdout pipe failed: %w", err)
}
stderr, err := childCmd.StderrPipe()
if err != nil {
return fmt.Errorf("creating stderr pipe failed: %w", err)
}
// Start the child command.
err = childCmd.Start()
if err != nil {
return fmt.Errorf("starting %q failed: %w", strings.Join(args, " "), err)
}
var wg sync.WaitGroup
wg.Add(2)
var stdoutErr error
go func() {
2025-03-05 09:44:43 +00:00
defer wg.Done()
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
_, err = fmt.Fprintln(cmd.OutOrStdout(), scanner.Text())
2025-03-04 17:22:12 +00:00
if err != nil {
stdoutErr = err
break
}
}
}()
var stderrErr error
go func() {
2025-03-05 09:44:43 +00:00
defer wg.Done()
scanner := bufio.NewScanner(stderr)
for scanner.Scan() {
_, err = fmt.Fprintln(cmd.ErrOrStderr(), scanner.Text())
2025-03-04 17:22:12 +00:00
if err != nil {
stderrErr = err
break
}
}
}()
wg.Wait()
if stdoutErr != nil {
return fmt.Errorf("writing stdout failed: %w", stdoutErr)
}
if stderrErr != nil {
return fmt.Errorf("writing stderr failed: %w", stderrErr)
}
err = childCmd.Wait()
2025-03-02 17:11:46 +00:00
if exitErr, ok := err.(*exec.ExitError); ok {
2025-03-03 19:06:24 +00:00
// We don't make the parent CLI process exit with the same exit code
// as the child process because the exit codes for the CLI have not
// been standardized yet.
//
// This keeps the door open for us to associate specific exit codes
// with specific classes of errors in the future.
2025-03-05 09:58:18 +00:00
return fmt.Errorf("running %q failed with exit code: %d", strings.Join(args, " "), exitErr.ExitCode())
2025-03-02 17:11:46 +00:00
}
2025-03-03 14:12:50 +00:00
if err != nil {
2025-03-04 17:22:12 +00:00
return fmt.Errorf("running %q failed: %w", strings.Join(args, " "), err)
2025-03-02 17:11:46 +00:00
}
return nil
},
}
return execCmd
}