2023-09-14 10:14:13 +00:00
|
|
|
package scripts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/config"
|
|
|
|
"github.com/databricks/cli/libs/cmdio"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2023-12-21 15:45:23 +00:00
|
|
|
"github.com/databricks/cli/libs/exec"
|
2023-09-14 10:14:13 +00:00
|
|
|
"github.com/databricks/cli/libs/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Execute(hook config.ScriptHook) bundle.Mutator {
|
|
|
|
return &script{
|
|
|
|
scriptHook: hook,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type script struct {
|
|
|
|
scriptHook config.ScriptHook
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *script) Name() string {
|
|
|
|
return fmt.Sprintf("scripts.%s", m.scriptHook)
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (m *script) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
2024-09-27 10:03:05 +00:00
|
|
|
executor, err := exec.NewCommandExecutor(b.BundleRootPath)
|
2023-12-21 15:45:23 +00:00
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2023-12-21 15:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd, out, err := executeHook(ctx, executor, b, m.scriptHook)
|
2023-09-14 10:14:13 +00:00
|
|
|
if err != nil {
|
2024-07-10 11:14:57 +00:00
|
|
|
return diag.FromErr(fmt.Errorf("failed to execute script: %w", err))
|
2023-09-14 10:14:13 +00:00
|
|
|
}
|
|
|
|
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))
|
|
|
|
|
|
|
|
reader := bufio.NewReader(out)
|
|
|
|
line, err := reader.ReadString('\n')
|
|
|
|
for err == nil {
|
|
|
|
cmdio.LogString(ctx, strings.TrimSpace(line))
|
|
|
|
line, err = reader.ReadString('\n')
|
|
|
|
}
|
|
|
|
|
2024-07-10 11:14:57 +00:00
|
|
|
err = cmd.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return diag.FromErr(fmt.Errorf("failed to execute script: %w", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2023-09-14 10:14:13 +00:00
|
|
|
}
|
|
|
|
|
2023-12-21 15:45:23 +00:00
|
|
|
func executeHook(ctx context.Context, executor *exec.Executor, b *bundle.Bundle, hook config.ScriptHook) (exec.Command, io.Reader, error) {
|
2023-09-14 10:14:13 +00:00
|
|
|
command := getCommmand(b, hook)
|
|
|
|
if command == "" {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
2023-12-21 15:45:23 +00:00
|
|
|
cmd, err := executor.StartCommand(ctx, string(command))
|
2023-09-14 10:14:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-21 15:45:23 +00:00
|
|
|
return cmd, io.MultiReader(cmd.Stdout(), cmd.Stderr()), nil
|
2023-09-14 10:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getCommmand(b *bundle.Bundle, hook config.ScriptHook) config.Command {
|
|
|
|
if b.Config.Experimental == nil || b.Config.Experimental.Scripts == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Config.Experimental.Scripts[hook]
|
|
|
|
}
|