11package cmd
22
33import (
4- "bytes"
54 "fmt"
65 "io"
76 "os"
@@ -12,6 +11,7 @@ import (
1211
1312 runtimevar "github.com/loft-sh/devspace/pkg/devspace/config/loader/variable/runtime"
1413 "github.com/loft-sh/devspace/pkg/devspace/imageselector"
14+ "github.com/loft-sh/devspace/pkg/util/command"
1515
1616 "github.com/loft-sh/devspace/pkg/devspace/config"
1717 "github.com/loft-sh/devspace/pkg/devspace/config/legacy"
@@ -603,6 +603,8 @@ func (cmd *DevCmd) startOutput(configInterface config.Config, dependencies []typ
603603 return 0 , pluginErr
604604 }
605605
606+ stdout , stderr , stdin := defaultStdStreams (cmd .Stdout , cmd .Stderr , cmd .Stdin )
607+
606608 // if config.Dev.Terminal is defined
607609 // config.Dev.Terminal.ImageSelector is empty &&
608610 // config.Dev.Terminal.LabelSelector is also empty &&
@@ -612,19 +614,14 @@ func (cmd *DevCmd) startOutput(configInterface config.Config, dependencies []typ
612614 config .Dev .Terminal .LabelSelector == nil &&
613615 config .Dev .Terminal .Command != nil &&
614616 len (config .Dev .Terminal .Command ) > 0 {
615- command := config .Dev .Terminal .Command [0 ]
616- c := exec .Command (command , config .Dev .Terminal .Command [1 :]... )
617- var stdout bytes.Buffer
618- c .Stdout = & stdout
619- if err := c .Run (); err != nil {
617+ c := command .NewStreamCommand (config .Dev .Terminal .Command [0 ], config .Dev .Terminal .Command [1 :])
618+ err := c .Run (stdout , stderr , stdin )
619+ if err != nil {
620620 if exitError , ok := err .(* exec.ExitError ); ok {
621- cmd .log .Failf ("Command '%s' returned an error: %s" , command , err )
621+ cmd .log .Failf ("Command '%s' returned an error: %s" , config . Dev . Terminal . Command [ 0 ] , err )
622622 return exitError .ExitCode (), err
623623 }
624624 }
625- // if command succeeds
626- cmd .log .Infof ("Command '%s' returned: %s" , command , stdout .String ())
627- _ , _ = cmd .Stdout .Write (stdout .Bytes ()) // pass output to the devspace's stdout
628625 return 0 , nil
629626 } else {
630627 selectorOptions := targetselector .NewDefaultOptions ().ApplyCmdParameter ("" , "" , cmd .Namespace , "" )
@@ -644,7 +641,7 @@ func (cmd *DevCmd) startOutput(configInterface config.Config, dependencies []typ
644641
645642 cmd .log .Info ("Terminal: Waiting for containers to start..." )
646643 selectorOptions .ImageSelector = imageSelectors
647- stdout , stderr , stdin := defaultStdStreams ( cmd . Stdout , cmd . Stderr , cmd . Stdin )
644+
648645 code , err := servicesClient .StartTerminal (selectorOptions , args , cmd .WorkingDirectory , exitChan , true , cmd .TerminalReconnect , stdout , stderr , stdin )
649646 if services .IsUnexpectedExitCode (code ) {
650647 cmd .log .Warnf ("Command terminated with exit code %d" , code )
@@ -783,35 +780,30 @@ func (cmd *DevCmd) loadConfig(configOptions *loader.ConfigOptions) (config.Confi
783780 c := configInterface .Config ()
784781
785782 if cmd .Terminal && c .Dev .Terminal == nil {
786- if c .Dev .Terminal == nil || (c .Dev .Terminal .ImageSelector == "" && len (c .Dev .Terminal .LabelSelector ) == 0 ) {
787- if len (c .Images ) == 0 {
788- return nil , errors .New ("No image available in devspace config" )
789- }
790-
791- imageNames := make ([]string , 0 , len (c .Images ))
792- for k := range c .Images {
793- imageNames = append (imageNames , k )
794- }
783+ if len (c .Images ) == 0 {
784+ return nil , errors .New ("No image available in devspace config" )
785+ }
795786
796- // if only one image exists, use it, otherwise show image picker
797- imageName := ""
798- if len (imageNames ) == 1 {
799- imageName = imageNames [0 ]
800- } else {
801- imageName , err = cmd .log .Question (& survey.QuestionOptions {
802- Question : "Which image do you want to open a terminal to?" ,
803- Options : imageNames ,
804- })
805- if err != nil {
806- return nil , err
807- }
808- }
809- c .Dev .Terminal = & latest.Terminal {
810- ImageSelector : fmt .Sprintf ("${runtime.images.%s.image}:${runtime.images.%s.tag}" , imageName , imageName ),
811- }
787+ imageNames := make ([]string , 0 , len (c .Images ))
788+ for k := range c .Images {
789+ imageNames = append (imageNames , k )
790+ }
812791
792+ // if only one image exists, use it, otherwise show image picker
793+ imageName := ""
794+ if len (imageNames ) == 1 {
795+ imageName = imageNames [0 ]
813796 } else {
814- c .Dev .Terminal .Disabled = false
797+ imageName , err = cmd .log .Question (& survey.QuestionOptions {
798+ Question : "Which image do you want to open a terminal to?" ,
799+ Options : imageNames ,
800+ })
801+ if err != nil {
802+ return nil , err
803+ }
804+ }
805+ c .Dev .Terminal = & latest.Terminal {
806+ ImageSelector : fmt .Sprintf ("${runtime.images.%s.image}:${runtime.images.%s.tag}" , imageName , imageName ),
815807 }
816808 }
817809
0 commit comments