@@ -57,11 +57,12 @@ type UpCmdFlags struct {
5757 open string
5858 initRegistries bool
5959 build bool
60- shell string
6160 sync bool
6261 deploy bool
6362 portforwarding bool
6463 noSleep bool
64+ verboseSync bool
65+ container string
6566}
6667
6768//UpFlagsDefault are the default flags for UpCmdFlags
@@ -74,6 +75,8 @@ var UpFlagsDefault = &UpCmdFlags{
7475 deploy : false ,
7576 portforwarding : true ,
7677 noSleep : false ,
78+ verboseSync : false ,
79+ container : "" ,
7780}
7881
7982const clusterRoleBindingName = "devspace-users"
@@ -97,16 +100,16 @@ Starts and connects your DevSpace:
971004. Starts the sync client
981015. Enters the container shell
99102#######################################################` ,
100- Args : cobra .NoArgs ,
101- Run : cmd .Run ,
103+ Run : cmd .Run ,
102104 }
103105 rootCmd .AddCommand (cobraCmd )
104106
105107 cobraCmd .Flags ().BoolVar (& cmd .flags .tiller , "tiller" , cmd .flags .tiller , "Install/upgrade tiller" )
106108 cobraCmd .Flags ().BoolVar (& cmd .flags .initRegistries , "init-registries" , cmd .flags .initRegistries , "Initialize registries (and install internal one)" )
107109 cobraCmd .Flags ().BoolVarP (& cmd .flags .build , "build" , "b" , cmd .flags .build , "Build image if Dockerfile has been modified" )
108- cobraCmd .Flags ().StringVarP (& cmd .flags .shell , "shell " , "s " , "" , "Shell command (default: bash, fallback: sh) " )
110+ cobraCmd .Flags ().StringVarP (& cmd .flags .container , "container " , "c " , cmd . flags . container , "Container name where to open the shell " )
109111 cobraCmd .Flags ().BoolVar (& cmd .flags .sync , "sync" , cmd .flags .sync , "Enable code synchronization" )
112+ cobraCmd .Flags ().BoolVar (& cmd .flags .verboseSync , "verbose-sync" , cmd .flags .verboseSync , "When enabled the sync will log every file change" )
110113 cobraCmd .Flags ().BoolVar (& cmd .flags .portforwarding , "portforwarding" , cmd .flags .portforwarding , "Enable port forwarding" )
111114 cobraCmd .Flags ().BoolVarP (& cmd .flags .deploy , "deploy" , "d" , cmd .flags .deploy , "Deploy chart" )
112115 cobraCmd .Flags ().BoolVar (& cmd .flags .noSleep , "no-sleep" , cmd .flags .noSleep , "Enable no-sleep" )
@@ -180,7 +183,7 @@ func (cmd *UpCmd) Run(cobraCmd *cobra.Command, args []string) {
180183 }()
181184 }
182185
183- cmd .enterTerminal ()
186+ cmd .enterTerminal (args )
184187}
185188
186189func (cmd * UpCmd ) ensureNamespace () error {
@@ -675,12 +678,36 @@ func (cmd *UpCmd) startSync() []*synctool.SyncConfig {
675678 if err != nil {
676679 log .Panicf ("Unable to list devspace pods: %s" , err .Error ())
677680 } else if pod != nil {
681+ if len (pod .Spec .Containers ) == 0 {
682+ log .Warnf ("Cannot start sync on pod, because selected pod %s/%s has no containers" , pod .Namespace , pod .Name )
683+ continue
684+ }
685+
686+ container := & pod .Spec .Containers [0 ]
687+ if syncPath .ContainerName != nil && * syncPath .ContainerName != "" {
688+ found := false
689+
690+ for _ , c := range pod .Spec .Containers {
691+ if c .Name == * syncPath .ContainerName {
692+ container = & c
693+ found = true
694+ break
695+ }
696+ }
697+
698+ if found == false {
699+ log .Warnf ("Couldn't start sync, because container %s wasn't found in pod %s/%s" , * syncPath .ContainerName , pod .Namespace , pod .Name )
700+ continue
701+ }
702+ }
703+
678704 syncConfig := & synctool.SyncConfig {
679705 Kubectl : cmd .kubectl ,
680706 Pod : pod ,
681- Container : & pod . Spec . Containers [ 0 ] ,
707+ Container : container ,
682708 WatchPath : absLocalPath ,
683709 DestPath : * syncPath .ContainerPath ,
710+ Verbose : cmd .flags .verboseSync ,
684711 }
685712
686713 if syncPath .ExcludePaths != nil {
@@ -700,7 +727,7 @@ func (cmd *UpCmd) startSync() []*synctool.SyncConfig {
700727 log .Fatalf ("Sync error: %s" , err .Error ())
701728 }
702729
703- log .Donef ("Sync started on %s <-> %s" , absLocalPath , * syncPath .ContainerPath )
730+ log .Donef ("Sync started on %s <-> %s (Pod: %s/%s) " , absLocalPath , * syncPath .ContainerPath , pod . Namespace , pod . Name )
704731 syncConfigs = append (syncConfigs , syncConfig )
705732 }
706733 }
@@ -756,24 +783,37 @@ func (cmd *UpCmd) startPortForwarding() {
756783 }
757784}
758785
759- func (cmd * UpCmd ) enterTerminal () {
760- var shell []string
786+ func (cmd * UpCmd ) enterTerminal (args []string ) {
787+ var command []string
788+ config := configutil .GetConfig (false )
761789
762- if len (cmd . flags . shell ) == 0 {
763- shell = []string {
790+ if len (args ) == 0 && ( config . DevSpace . Terminal . Command == nil || len ( * config . DevSpace . Terminal . Command ) == 0 ) {
791+ command = []string {
764792 "sh" ,
765793 "-c" ,
766794 "command -v bash >/dev/null 2>&1 && exec bash || exec sh" ,
767795 }
768796 } else {
769- shell = []string {cmd .flags .shell }
797+ if len (args ) > 0 {
798+ command = args
799+ } else {
800+ for _ , cmd := range * config .DevSpace .Terminal .Command {
801+ command = append (command , * cmd )
802+ }
803+ }
770804 }
771805
772- _ , _ , _ , terminalErr := kubectl .Exec (cmd .kubectl , cmd .pod , cmd .pod .Spec .Containers [0 ].Name , shell , true , nil )
806+ containerName := cmd .pod .Spec .Containers [0 ].Name
807+ if cmd .flags .container != "" {
808+ containerName = cmd .flags .container
809+ } else if config .DevSpace .Terminal .ContainerName != nil {
810+ containerName = * config .DevSpace .Terminal .ContainerName
811+ }
773812
813+ _ , _ , _ , terminalErr := kubectl .Exec (cmd .kubectl , cmd .pod , containerName , command , true , nil )
774814 if terminalErr != nil {
775815 if _ , ok := terminalErr .(kubectlExec.CodeExitError ); ok == false {
776- log .Fatalf ("Unable to start terminal session: %s " , terminalErr . Error () )
816+ log .Fatalf ("Unable to start terminal session: %v " , terminalErr )
777817 }
778818 }
779819}
0 commit comments