Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
/coder-v1-cliPublic archive

Commit24df4f7

Browse files
committed
internal/cmd/update.go: handle windows-specific behaviours
1 parent6238053 commit24df4f7

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

‎internal/cmd/update.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,22 +208,42 @@ func (u *updater) Run(ctx context.Context, force bool, coderURLArg string, versi
208208

209209
_=updatedBin.Close()
210210

211+
iferr:=u.doUpdate(ctx,updatedCoderBinaryPath);err!=nil {
212+
returnclog.Fatal("failed to update coder binary",clog.Causef(err.Error()))
213+
}
214+
215+
clog.LogSuccess("Updated coder CLI to version "+desiredVersion.String())
216+
returnnil
217+
}
218+
219+
func (u*updater)doUpdate(ctx context.Context,updatedCoderBinaryPathstring)error {
220+
varerrerror
221+
// TODO(cian): on Windows, we must do two things differnetly:
222+
// 1) Calling the updated binary fails due to the xterminal.MakeOutputRaw call in main; skipping this check on Windows.
223+
// 2) We must rename the currently running binary before renaming the new binary
224+
ifu.osF()==goosWindows {
225+
err=u.fs.Rename(u.executablePath,updatedCoderBinaryPath+".old")
226+
iferr!=nil {
227+
returnxerrors.Errorf("windows: rename current coder binary: %w",err)
228+
}
229+
err=u.fs.Rename(updatedCoderBinaryPath,u.executablePath)
230+
iferr!=nil {
231+
returnxerrors.Errorf("windows: rename updated coder binary: %w",err)
232+
}
233+
returnnil
234+
}
235+
211236
// validate that we can execute the new binary before overwriting
212237
updatedVersionOutput,err:=u.execF(ctx,updatedCoderBinaryPath,"--version")
213238
iferr!=nil {
214-
returnclog.Fatal("failed to check version of updated coder binary",
215-
clog.BlankLine,
216-
fmt.Sprintf("output: %q",string(updatedVersionOutput)),
217-
clog.Causef(err.Error()))
239+
returnxerrors.Errorf("check version of updated coder binary: %w",err)
218240
}
219-
220241
clog.LogInfo(fmt.Sprintf("updated binary reports %s",bytes.TrimSpace(updatedVersionOutput)))
221242

222243
iferr=u.fs.Rename(updatedCoderBinaryPath,u.executablePath);err!=nil {
223-
returnclog.Fatal("failed toupdate coder binary in-place",clog.Causef(err.Error()))
244+
returnxerrors.Errorf("update coder binary in-place: %w",err)
224245
}
225246

226-
clog.LogSuccess("Updated coder CLI to version "+desiredVersion.String())
227247
returnnil
228248
}
229249

‎internal/cmd/update_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,11 +345,25 @@ func Test_updater_run(t *testing.T) {
345345
u:=fromParams(p)
346346
assertFileContent(t,p.Fakefs,fakeExePathLinux,fakeOldVersion)
347347
err:=u.Run(p.Ctx,false,fakeCoderURL,"")
348-
assertCLIError(t,"update coder - cannot exec new binary",err,"failed tocheck version of updatedcoder binary","")
348+
assertCLIError(t,"update coder - cannot exec new binary",err,"failed toupdatecoder binary",fakeError.Error())
349349
assertFileContent(t,p.Fakefs,fakeExePathLinux,fakeOldVersion)
350350
})
351351

352352
ifruntime.GOOS==goosWindows {
353+
run(t,"update coder - windows",func(t*testing.T,p*params) {
354+
fakeFile(t,p.Fakefs,fakeExePathWindows,0755,fakeOldVersion)
355+
p.HTTPClient.M[apiPrivateVersionURL]=newFakeGetterResponse([]byte(fakeNewVersionJSON),200,variadicS(),nil)
356+
p.HTTPClient.M[fakeGithubReleaseURL]=newFakeGetterResponse([]byte(fakeGithubReleaseJSON),200,variadicS(),nil)
357+
p.HTTPClient.M[fakeAssetURLLinux]=newFakeGetterResponse(fakeValidTgzBytes,200,variadicS(),nil)
358+
p.VersionF=func()string {returnfakeOldVersion }
359+
p.ConfirmF=fakeConfirmYes
360+
p.OsF=func()string {returngoosWindows }
361+
u:=fromParams(p)
362+
assertFileContent(t,p.Fakefs,fakeExePathWindows,fakeOldVersion)
363+
err:=u.Run(p.Ctx,false,fakeCoderURL,"")
364+
assertCLIError(t,"update coder - cannot exec new binary",err,"failed to update coder binary",fakeError.Error())
365+
assertFileContent(t,p.Fakefs,fakeExePathWindows,fakeOldVersion)
366+
})
353367
run(t,"update coder - path blocklist - windows",func(t*testing.T,p*params) {
354368
p.ExecutablePath=`C:\Windows\system32\coder.exe`
355369
u:=fromParams(p)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp