@@ -6,10 +6,10 @@ import (
66 "os"
77 "os/signal"
88 "strings"
9+ "sync"
910 "syscall"
1011 "time"
1112
12- . "github.com/ForceCLI/force/error"
1313 . "github.com/ForceCLI/force/lib"
1414 "github.com/spf13/cobra"
1515)
@@ -38,53 +38,41 @@ func defaultDeployOutputOptions() *deployOutputOptions {
3838
3939var testFailureError = errors .New ("Apex tests failed" )
4040
41- func monitorDeploy (deployId string ) (ForceCheckDeploymentStatusResult , error ) {
42- var result ForceCheckDeploymentStatusResult
43- var err error
44- retrying := false
45- for {
46- result , err = force .Metadata .CheckDeployStatus (deployId )
47- if err != nil {
48- if retrying {
49- return result , fmt .Errorf ("Error getting deploy status: %w" , err )
50- } else {
51- retrying = true
52- Log .Info (fmt .Sprintf ("Received error checking deploy status: %s. Will retry once before aborting." , err .Error ()))
53- }
54- } else {
55- retrying = false
56- }
57- if result .Done {
58- break
59- }
60- if ! retrying {
61- Log .Info (result )
62- }
63- time .Sleep (5000 * time .Millisecond )
64- }
65- return result , err
41+ type deployStatus struct {
42+ mu sync.Mutex
43+ aborted bool
44+ }
45+
46+ func (c * deployStatus ) abort () {
47+ c .mu .Lock ()
48+ c .aborted = true
49+ c .mu .Unlock ()
50+ }
51+
52+ func (c * deployStatus ) isAborted () bool {
53+ c .mu .Lock ()
54+ defer c .mu .Unlock ()
55+ return c .aborted
6656}
6757
6858func deploy (force * Force , files ForceMetadataFiles , deployOptions * ForceDeployOptions , outputOptions * deployOutputOptions ) error {
69- if outputOptions .quiet {
70- previousLogger := Log
71- var l quietLogger
72- Log = l
73- defer func () {
74- Log = previousLogger
75- }()
76- }
59+ status := deployStatus {aborted : false }
60+
61+ return deployWith (force , & status , files , deployOptions , outputOptions )
62+ }
63+
64+ func deployWith (force * Force , status * deployStatus , files ForceMetadataFiles , deployOptions * ForceDeployOptions , outputOptions * deployOutputOptions ) error {
7765 startTime := time .Now ()
7866 deployId , err := force .Metadata .StartDeploy (files , * deployOptions )
7967 if err != nil {
80- ErrorAndExit ( err . Error ())
68+ return err
8169 }
8270 stopDeployUponSignal (force , deployId )
8371 if outputOptions .interactive {
8472 watchDeploy (deployId )
8573 return nil
8674 }
87- result , err := monitorDeploy (deployId )
75+ result , err := monitorDeploy (force , deployId , status )
8876 if err != nil {
8977 return err
9078 }
@@ -156,6 +144,39 @@ func deploy(force *Force, files ForceMetadataFiles, deployOptions *ForceDeployOp
156144 return nil
157145}
158146
147+ func monitorDeploy (force * Force , deployId string , status * deployStatus ) (ForceCheckDeploymentStatusResult , error ) {
148+ var result ForceCheckDeploymentStatusResult
149+ var err error
150+ retrying := false
151+ for {
152+ if status .isAborted () {
153+ fmt .Fprintf (os .Stderr , "Cancelling deploy %s\n " , deployId )
154+ force .Metadata .CancelDeploy (deployId )
155+ return result , nil
156+ }
157+ result , err = force .Metadata .CheckDeployStatus (deployId )
158+ if err != nil {
159+ if retrying {
160+ return result , fmt .Errorf ("Error getting deploy status: %w" , err )
161+ } else {
162+ retrying = true
163+ Log .Info (fmt .Sprintf ("Received error checking deploy status: %s. Will retry once before aborting." , err .Error ()))
164+ }
165+ } else {
166+ retrying = false
167+ }
168+ result .UserName = force .GetCredentials ().UserInfo .UserName
169+ if result .Done {
170+ break
171+ }
172+ if ! retrying {
173+ Log .Info (result )
174+ }
175+ time .Sleep (5000 * time .Millisecond )
176+ }
177+ return result , err
178+ }
179+
159180func stopDeployUponSignal (force * Force , deployId string ) {
160181 sigs := make (chan os.Signal , 1 )
161182 signal .Notify (sigs , syscall .SIGINT , syscall .SIGTERM )
0 commit comments