@@ -6,10 +6,10 @@ import (
6
6
"os"
7
7
"os/signal"
8
8
"strings"
9
+ "sync"
9
10
"syscall"
10
11
"time"
11
12
12
- . "github.com/ForceCLI/force/error"
13
13
. "github.com/ForceCLI/force/lib"
14
14
"github.com/spf13/cobra"
15
15
)
@@ -38,53 +38,41 @@ func defaultDeployOutputOptions() *deployOutputOptions {
38
38
39
39
var testFailureError = errors .New ("Apex tests failed" )
40
40
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
66
56
}
67
57
68
58
func 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 {
77
65
startTime := time .Now ()
78
66
deployId , err := force .Metadata .StartDeploy (files , * deployOptions )
79
67
if err != nil {
80
- ErrorAndExit ( err . Error ())
68
+ return err
81
69
}
82
70
stopDeployUponSignal (force , deployId )
83
71
if outputOptions .interactive {
84
72
watchDeploy (deployId )
85
73
return nil
86
74
}
87
- result , err := monitorDeploy (deployId )
75
+ result , err := monitorDeploy (force , deployId , status )
88
76
if err != nil {
89
77
return err
90
78
}
@@ -156,6 +144,39 @@ func deploy(force *Force, files ForceMetadataFiles, deployOptions *ForceDeployOp
156
144
return nil
157
145
}
158
146
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
+
159
180
func stopDeployUponSignal (force * Force , deployId string ) {
160
181
sigs := make (chan os.Signal , 1 )
161
182
signal .Notify (sigs , syscall .SIGINT , syscall .SIGTERM )
0 commit comments