@@ -34,6 +34,7 @@ import (
3434 "github.com/vmware-tanzu/apps-cli-plugin/pkg/cli-runtime/wait"
3535 "github.com/vmware-tanzu/apps-cli-plugin/pkg/completion"
3636 "github.com/vmware-tanzu/apps-cli-plugin/pkg/flags"
37+ "github.com/vmware-tanzu/apps-cli-plugin/pkg/printer"
3738)
3839
3940type WorkloadApplyOptions struct {
@@ -206,45 +207,55 @@ func (opts *WorkloadApplyOptions) Exec(ctx context.Context, c *cli.Config) error
206207 }
207208 }
208209
209- var workers []wait.Worker
210- if opts .Output != "" && okToApply {
211- workers = opts .WaitToBeReady (c , workload )
212- if err := opts .WaitError (ctx , c , workload , workers ); err != nil {
213- return err
214- }
215- // once the workload is ready, get it as is in the cluster
216- if err := c .Get (ctx , client.ObjectKey {Namespace : opts .Namespace , Name : opts .Name }, workload ); err != nil {
217- return err
218- }
219- if err := opts .OutputWorkload (c , workload ); err != nil {
220- return err
221- }
222-
223- return nil
224- }
225-
226- anyTail := opts .Tail || opts .TailTimestamps
227- if okToApply && (opts .Wait || anyTail ) {
228- c .Infof ("Waiting for workload %q to become ready...\n " , opts .Name )
229-
230- workers = opts .WaitToBeReady (c , workload )
210+ if okToApply {
211+ anyTail := opts .Tail || opts .TailTimestamps
212+ if opts .Wait || anyTail {
213+ cli .PrintPrompt (shouldPrint , c .Infof , "Waiting for workload %q to become ready...\n " , opts .Name )
214+
215+ workers := opts .WaitToBeReady (c , workload )
216+
217+ if anyTail {
218+ workers = append (workers , func (ctx context.Context ) error {
219+ selector , err := labels .Parse (fmt .Sprintf ("%s=%s" , cartov1alpha1 .WorkloadLabelName , workload .Name ))
220+ if err != nil {
221+ return err
222+ }
223+ containers := []string {}
224+ return logs .Tail (ctx , c , opts .Namespace , selector , containers , time .Minute , opts .TailTimestamps )
225+ })
226+ }
231227
232- if anyTail {
233- workers = append (workers , func (ctx context.Context ) error {
234- selector , err := labels .Parse (fmt .Sprintf ("%s=%s" , cartov1alpha1 .WorkloadLabelName , workload .Name ))
235- if err != nil {
236- panic (err )
228+ err := wait .Race (ctx , opts .WaitTimeout , workers )
229+ // print wait error only if output is not set or it was not used with --yes
230+ if err != nil {
231+ if err == context .DeadlineExceeded {
232+ cli .PrintPrompt (shouldPrint , c .Printf , "%s timeout after %s waiting for %q to become ready\n " , printer .Serrorf ("Error:" ), opts .WaitTimeout , workload .Name )
233+ } else {
234+ cli .PrintPrompt (shouldPrint , c .Eprintf , "%s %s\n " , printer .Serrorf ("Error:" ), err )
237235 }
238- containers := []string {}
239- return logs .Tail (ctx , c , opts .Namespace , selector , containers , time .Minute , opts .TailTimestamps )
240- })
241- }
236+ }
237+ // do not return if --output is set
238+ // because workload has to be printed despite it's in a failing state
239+ if err != nil && opts .Output == "" {
240+ return cli .SilenceError (err )
241+ }
242242
243- if err := opts .WaitError (ctx , c , workload , workers ); err != nil {
244- return err
243+ // since there is a possibility that wait failed but did not return
244+ // make sure this prompt is printed only if there is no error
245+ if err == nil {
246+ cli .PrintPrompt (shouldPrint , c .Infof , "Workload %q is ready\n \n " , workload .Name )
247+ }
245248 }
246249
247- c .Infof ("Workload %q is ready\n \n " , workload .Name )
250+ if opts .Output != "" {
251+ // once the workload is applied, get it as is in the cluster
252+ if err := c .Get (ctx , client.ObjectKey {Namespace : opts .Namespace , Name : opts .Name }, workload ); err != nil {
253+ return err
254+ }
255+ if err := opts .OutputWorkload (c , workload ); err != nil {
256+ return err
257+ }
258+ }
248259 }
249260
250261 return nil
0 commit comments