@@ -80,50 +80,59 @@ func appAndArgs(ctx context.Context, c client.Client, args []string) (string, []
8080 return appName , nil , err
8181}
8282
83- func ( s * Exec ) filterContainers ( containers []apiv1.ContainerReplica ) (result []apiv1.ContainerReplica ) {
83+ func filterContainers ( containerName string , containers []apiv1.ContainerReplica ) (result []apiv1.ContainerReplica ) {
8484 for _ , c := range containers {
85- if s . Container == "" {
85+ if containerName == "" {
8686 result = append (result , c )
87- } else if c .Spec .ContainerName == s . Container {
87+ } else if c .Spec .ContainerName == containerName {
8888 result = append (result , c )
8989 break
90- } else if c .Spec . ContainerName + "." + c . Spec . SidecarName == s . Container {
90+ } else if c .Name == containerName {
9191 result = append (result , c )
9292 break
9393 }
9494 }
9595 return result
9696}
9797
98- func ( s * Exec ) execApp ( ctx context.Context , c client.Client , app * apiv1.App , args [] string ) error {
98+ func getContainerForApp ( ctx context.Context , c client.Client , app * apiv1.App , containerName string , first bool ) ( string , error ) {
9999 containers , err := c .ContainerReplicaList (ctx , & client.ContainerReplicaListOptions {
100100 App : app .Name ,
101101 })
102102 if err != nil {
103- return err
103+ return "" , err
104104 }
105105
106106 var (
107107 displayNames []string
108108 names = map [string ]string {}
109109 )
110110
111- containers = s . filterContainers (containers )
111+ containers = filterContainers (containerName , containers )
112112
113113 for _ , container := range containers {
114+ if container .Status .Columns .State == "stopped" {
115+ continue
116+ }
114117 displayName := fmt .Sprintf ("%s (%s %s)" , container .Name , container .Status .Columns .State , table .FormatCreated (container .CreationTimestamp ))
115118 displayNames = append (displayNames , displayName )
116119 names [displayName ] = container .Name
117120 }
118121
122+ if first && containerName != "" && len (containers ) > 0 {
123+ for _ , name := range names {
124+ return name , nil
125+ }
126+ }
127+
119128 if len (containers ) == 0 {
120- return fmt .Errorf ("failed to find any containers for app %s" , app .Name )
129+ return "" , fmt .Errorf ("failed to find any containers for app %s" , app .Name )
121130 }
122131
123132 var choice string
124133 switch len (displayNames ) {
125134 case 0 :
126- return fmt .Errorf ("failed to find any containers for app %s" , app .Name )
135+ return "" , fmt .Errorf ("failed to find any containers for app %s" , app .Name )
127136 case 1 :
128137 choice = displayNames [0 ]
129138 default :
@@ -133,11 +142,11 @@ func (s *Exec) execApp(ctx context.Context, c client.Client, app *apiv1.App, arg
133142 Default : displayNames [0 ],
134143 }, & choice )
135144 if err != nil {
136- return err
145+ return "" , err
137146 }
138147 }
139148
140- return s . execContainer ( ctx , c , names [choice ], args )
149+ return names [choice ], nil
141150}
142151
143152func (s * Exec ) execContainer (ctx context.Context , c client.Client , containerName string , args []string ) error {
@@ -171,7 +180,10 @@ func (s *Exec) Run(cmd *cobra.Command, args []string) error {
171180
172181 app , appErr := c .AppGet (ctx , name )
173182 if appErr == nil {
174- return s .execApp (ctx , c , app , args )
183+ name , err = getContainerForApp (ctx , c , app , s .Container , false )
184+ if err != nil {
185+ return err
186+ }
175187 }
176188 return s .execContainer (ctx , c , name , args )
177189}
0 commit comments