databricks-cli/cmd/selftest/child.go

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

43 lines
1.2 KiB
Go
Raw Normal View History

2025-02-17 18:23:02 +00:00
package selftest
import (
"fmt"
"io"
"os"
2025-02-18 12:14:29 +00:00
"os/exec"
2025-02-17 18:23:02 +00:00
"github.com/databricks/cli/libs/daemon"
"github.com/spf13/cobra"
)
func newChildCommand() *cobra.Command {
return &cobra.Command{
Use: "child",
RunE: func(cmd *cobra.Command, args []string) error {
2025-02-18 12:16:35 +00:00
// wait_pid lives in acceptance/bin. We expect this command to only be called
// from acceptance tests.
//
// Note: The golang stdlib only provides a way to wait on processes
// that are children of the current process. While it's possible to
// rely on os native syscalls to wait on arbitrary processes, it's hard
2025-02-18 13:26:56 +00:00
// to get right and test. So I opted to just rely on the wait_pid
2025-02-18 12:16:35 +00:00
// script here.
2025-02-18 12:14:29 +00:00
waitCmd := exec.Command("bash", "-euo", "pipefail", "wait_pid", os.Getenv(daemon.DatabricksCliParentPid))
b, err := waitCmd.Output()
2025-02-17 18:23:02 +00:00
if err != nil {
return fmt.Errorf("failed to wait for parent process: %w", err)
}
2025-02-18 12:14:29 +00:00
fmt.Print("[child]" + string(b))
fmt.Println("[child] Parent process has exited")
2025-02-17 18:23:02 +00:00
in, err := io.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("failed to read from stdin: %w", err)
}
2025-02-18 12:14:29 +00:00
fmt.Println("[child] input from parent: " + string(in))
2025-02-17 18:23:02 +00:00
return nil
},
}
}