@@ -49,17 +49,15 @@ func TerminateExistingContainer(ctx context.Context) error {
49
49
return nil
50
50
}
51
51
52
- func runRebuild (ctx context.Context , supervisorClient * supervisor.SupervisorClient , event * utils.EventTracker ) error {
52
+ func runRebuild (ctx context.Context , supervisorClient * supervisor.SupervisorClient , event * utils.EventTracker ) ( string , error ) {
53
53
wsInfo , err := supervisorClient .Info .WorkspaceInfo (ctx , & api.WorkspaceInfoRequest {})
54
54
if err != nil {
55
- event .Set ("ErrorCode" , utils .SystemErrorCode )
56
- return err
55
+ return utils .Outcome_SystemErr , err
57
56
}
58
57
59
58
tmpDir , err := os .MkdirTemp ("" , "gp-rebuild-*" )
60
59
if err != nil {
61
- event .Set ("ErrorCode" , utils .SystemErrorCode )
62
- return err
60
+ return utils .Outcome_SystemErr , err
63
61
}
64
62
defer os .RemoveAll (tmpDir )
65
63
@@ -70,7 +68,7 @@ func runRebuild(ctx context.Context, supervisorClient *supervisor.SupervisorClie
70
68
fmt .Println ("For help check out the reference page:" )
71
69
fmt .Println ("https://www.gitpod.io/docs/references/gitpod-yml#gitpodyml" )
72
70
event .Set ("ErrorCode" , utils .RebuildErrorCode_MalformedGitpodYaml )
73
- return err
71
+ return utils . Outcome_UserErr , err
74
72
}
75
73
76
74
if gitpodConfig == nil {
@@ -81,7 +79,7 @@ func runRebuild(ctx context.Context, supervisorClient *supervisor.SupervisorClie
81
79
fmt .Println ("Alternatively, check out the following docs for getting started configuring your project" )
82
80
fmt .Println ("https://www.gitpod.io/docs/configure#configure-gitpod" )
83
81
event .Set ("ErrorCode" , utils .RebuildErrorCode_MissingGitpodYaml )
84
- return nil
82
+ return utils . Outcome_UserErr , nil
85
83
}
86
84
87
85
var baseimage string
@@ -95,13 +93,11 @@ func runRebuild(ctx context.Context, supervisorClient *supervisor.SupervisorClie
95
93
96
94
if _ , err := os .Stat (dockerfilePath ); os .IsNotExist (err ) {
97
95
fmt .Println ("Your .gitpod.yml points to a Dockerfile that doesn't exist: " + dockerfilePath )
98
- event .Set ("ErrorCode" , utils .RebuildErrorCode_DockerfileNotFound ).Send (ctx )
99
- return err
96
+ return utils .Outcome_UserErr , err
100
97
}
101
98
dockerfile , err := os .ReadFile (dockerfilePath )
102
99
if err != nil {
103
- event .Set ("ErrorCode" , utils .RebuildErrorCode_DockerfileCannotRead )
104
- return err
100
+ return utils .Outcome_SystemErr , err
105
101
}
106
102
if string (dockerfile ) == "" {
107
103
fmt .Println ("Your Gitpod's Dockerfile is empty" )
@@ -110,14 +106,13 @@ func runRebuild(ctx context.Context, supervisorClient *supervisor.SupervisorClie
110
106
fmt .Println ("https://www.gitpod.io/docs/configure/workspaces/workspace-image#use-a-custom-dockerfile" )
111
107
fmt .Println ("" )
112
108
fmt .Println ("Once you configure your Dockerfile, re-run this command to validate your changes" )
113
- event .Set ("ErrorCode" , utils .RebuildErrorCode_DockerfileEmpty )
114
- return nil
109
+ return utils .Outcome_UserErr , nil
115
110
}
116
111
baseimage = "\n " + string (dockerfile ) + "\n "
117
112
default :
118
113
fmt .Println ("Check your .gitpod.yml and make sure the image property is configured correctly" )
119
114
event .Set ("ErrorCode" , utils .RebuildErrorCode_MalformedGitpodYaml )
120
- return err
115
+ return utils . Outcome_UserErr , nil
121
116
}
122
117
123
118
if baseimage == "" {
@@ -126,23 +121,22 @@ func runRebuild(ctx context.Context, supervisorClient *supervisor.SupervisorClie
126
121
fmt .Println ("" )
127
122
fmt .Println ("https://www.gitpod.io/docs/configure/workspaces/workspace-image#use-a-public-docker-image" )
128
123
event .Set ("ErrorCode" , utils .RebuildErrorCode_NoCustomImage )
129
- return nil
124
+ return utils . Outcome_UserErr , nil
130
125
}
131
126
132
127
tmpDockerfile := filepath .Join (tmpDir , "Dockerfile" )
133
128
134
129
err = os .WriteFile (tmpDockerfile , []byte (baseimage ), 0644 )
135
130
if err != nil {
136
131
fmt .Println ("Could not write the temporary Dockerfile" )
137
- event .Set ("ErrorCode" , utils .RebuildErrorCode_DockerfileCannotWrite )
138
- return err
132
+ return utils .Outcome_SystemErr , err
139
133
}
140
134
141
135
dockerPath , err := exec .LookPath ("docker" )
142
136
if err != nil {
143
137
fmt .Println ("Docker is not installed in your workspace" )
144
138
event .Set ("ErrorCode" , utils .RebuildErrorCode_DockerNotFound )
145
- return err
139
+ return utils . Outcome_SystemErr , err
146
140
}
147
141
148
142
tag := "gp-rebuild-temp-build"
@@ -155,20 +149,19 @@ func runRebuild(ctx context.Context, supervisorClient *supervisor.SupervisorClie
155
149
err = dockerCmd .Run ()
156
150
if _ , ok := err .(* exec.ExitError ); ok {
157
151
fmt .Println ("Image Build Failed" )
158
- event .Set ("ErrorCode" , utils .RebuildErrorCode_DockerBuildFailed )
159
- return nil
152
+ event .Set ("ErrorCode" , utils .RebuildErrorCode_ImageBuildFailed )
153
+ return utils . Outcome_UserErr , nil
160
154
} else if err != nil {
161
155
fmt .Println ("Docker error" )
162
156
event .Set ("ErrorCode" , utils .RebuildErrorCode_DockerErr )
163
- return err
157
+ return utils . Outcome_SystemErr , err
164
158
}
165
159
ImageBuildDuration := time .Since (imageBuildStartTime ).Milliseconds ()
166
160
event .Set ("ImageBuildDuration" , ImageBuildDuration )
167
161
168
162
err = TerminateExistingContainer (ctx )
169
163
if err != nil {
170
- event .Set ("ErrorCode" , utils .SystemErrorCode )
171
- return err
164
+ return utils .Outcome_SystemErr , err
172
165
}
173
166
174
167
welcomeMessage := strings .Join ([]string {
@@ -209,12 +202,12 @@ func runRebuild(ctx context.Context, supervisorClient *supervisor.SupervisorClie
209
202
if err != nil {
210
203
fmt .Println ("Failed to run docker container" )
211
204
event .Set ("ErrorCode" , utils .RebuildErrorCode_DockerRunFailed )
212
- return err
205
+ return utils . Outcome_UserErr , err
213
206
}
214
207
215
208
_ = dockerRunCmd .Wait ()
216
209
217
- return nil
210
+ return utils . Outcome_Success , nil
218
211
}
219
212
220
213
var buildCmd = & cobra.Command {
@@ -240,10 +233,18 @@ var buildCmd = &cobra.Command{
240
233
Command : cmd .Name (),
241
234
})
242
235
243
- err = runRebuild (ctx , supervisorClient , event )
244
- if err != nil && event .Data .ErrorCode == "" {
245
- event .Set ("ErrorCode" , utils .SystemErrorCode )
236
+ outcome , err := runRebuild (ctx , supervisorClient , event )
237
+ event .Set ("Outcome" , outcome )
238
+
239
+ if outcome != utils .Outcome_Success && event .Data .ErrorCode == "" {
240
+ switch outcome {
241
+ case utils .Outcome_UserErr :
242
+ event .Set ("ErrorCode" , utils .UserErrorCode )
243
+ case utils .Outcome_SystemErr :
244
+ event .Set ("ErrorCode" , utils .SystemErrorCode )
245
+ }
246
246
}
247
+
247
248
event .Send (ctx )
248
249
249
250
if err != nil {
0 commit comments