diff --git a/cmd/deploy.go b/cmd/deploy.go index 2b4ecc50..eb7f2afd 100644 --- a/cmd/deploy.go +++ b/cmd/deploy.go @@ -53,6 +53,12 @@ func runDeploy(cmd *cobra.Command, args []string) error { log.Dim("Running containerized.") } + if env.RunningInteractively { + log.Dim("Running with a controlling terminal.") + } else { + log.Dim("Running without a controlling terminal.") + } + component := "both" if len(args) > 0 { component = args[0] @@ -62,6 +68,10 @@ func runDeploy(cmd *cobra.Command, args []string) error { return errors.New("already in a roxie sub-shell (ROXIE_SHELL environment variable is set), please exit the shell and try again") } + if !env.RunningInteractively && envrc == "" { + return errors.New("running without a controlling terminal requires --envrc to be set") + } + if envrc != "" && portForwarding { return errors.New("cannot use --envrc with --port-forwarding. The --envrc flag is for non-interactive mode with remote cluster access") } @@ -116,6 +126,7 @@ func runDeploy(cmd *cobra.Command, args []string) error { } if envrc != "" { + log.Dimf("Writing environment variables to %s", envrc) d.SetEnvrcFile(envrc) } diff --git a/go.mod b/go.mod index 91f382c0..a457e6c9 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/fatih/color v1.16.0 github.com/moby/sys/mountinfo v0.7.2 github.com/spf13/cobra v1.8.0 + golang.org/x/term v0.38.0 gopkg.in/yaml.v3 v3.0.1 ) @@ -14,5 +15,5 @@ require ( github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/spf13/pflag v1.0.5 // indirect - golang.org/x/sys v0.14.0 // indirect + golang.org/x/sys v0.39.0 // indirect ) diff --git a/go.sum b/go.sum index 651f17ee..d1733f43 100644 --- a/go.sum +++ b/go.sum @@ -17,8 +17,10 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= -golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk= +golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +golang.org/x/term v0.38.0 h1:PQ5pkm/rLO6HnxFR7N2lJHOZX6Kez5Y1gDSJla6jo7Q= +golang.org/x/term v0.38.0/go.mod h1:bSEAKrOT1W+VSu9TSCMtoGEOUcKxOKgl3LE5QEF/xVg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/internal/env/env.go b/internal/env/env.go index 0f9adbc3..75026755 100644 --- a/internal/env/env.go +++ b/internal/env/env.go @@ -9,10 +9,12 @@ import ( "strings" "github.com/stackrox/roxie/internal/containerutil" + "golang.org/x/term" ) var ( - RunningInContainer bool + RunningInContainer bool + RunningInteractively bool ) // ClusterType represents different types of Kubernetes clusters @@ -47,6 +49,18 @@ func init() { if RunningInContainer { os.Setenv("KUBECONFIG", "/kubeconfig") } + RunningInteractively = isRunningInteractively() +} + +// isRunningInteractively detects if roxie is running interactively +// by checking if stdin, stdout, and stderr are all connected to a terminal. +func isRunningInteractively() bool { + for _, f := range []*os.File{os.Stdin, os.Stdout, os.Stderr} { + if !term.IsTerminal(int(f.Fd())) { + return false + } + } + return true } // ensureInitialized performs lazy initialization of cluster information