Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions bundle/scripts/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,21 @@ func (m *script) Name() string {
}

func (m *script) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
command := getCommand(b, m.scriptHook)
if command == "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a case for having a script but no command? like variable command?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script hooks run after variable interpolation, so the command is always fully resolved by the time this code executes. If it resolves to empty then it is not really useful, of course...

log.Debugf(ctx, "No script defined for %s, skipping", m.scriptHook)
return nil
}

executor, err := exec.NewCommandExecutor(b.BundleRootPath)
if err != nil {
return diag.FromErr(err)
}

cmd, out, err := executeHook(ctx, executor, b, m.scriptHook)
cmd, out, err := executeHook(ctx, executor, command)
if err != nil {
return diag.FromErr(fmt.Errorf("failed to execute script: %w", err))
}
if cmd == nil {
log.Debugf(ctx, "No script defined for %s, skipping", m.scriptHook)
return nil
}

cmdio.LogString(ctx, fmt.Sprintf("Executing '%s' script", m.scriptHook))

Expand All @@ -63,12 +65,7 @@ func (m *script) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
return nil
}

func executeHook(ctx context.Context, executor *exec.Executor, b *bundle.Bundle, hook config.ScriptHook) (exec.Command, io.Reader, error) {
command := getCommmand(b, hook)
if command == "" {
return nil, nil, nil
}

func executeHook(ctx context.Context, executor *exec.Executor, command config.Command) (exec.Command, io.Reader, error) {
// Don't run any arbitrary code when restricted execution is enabled.
if _, ok := env.RestrictedExecution(ctx); ok {
return nil, nil, errors.New("running scripts is not allowed when DATABRICKS_BUNDLE_RESTRICTED_CODE_EXECUTION is set")
Expand All @@ -82,7 +79,7 @@ func executeHook(ctx context.Context, executor *exec.Executor, b *bundle.Bundle,
return cmd, io.MultiReader(cmd.Stdout(), cmd.Stderr()), nil
}

func getCommmand(b *bundle.Bundle, hook config.ScriptHook) config.Command {
func getCommand(b *bundle.Bundle, hook config.ScriptHook) config.Command {
if b.Config.Experimental == nil || b.Config.Experimental.Scripts == nil {
return ""
}
Expand Down
Loading