Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 24df4f7

Browse files
committed
internal/cmd/update.go: handle windows-specific behaviours
1 parent 6238053 commit 24df4f7

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+
if err := u.doUpdate(ctx, updatedCoderBinaryPath); err != nil {
212+
return clog.Fatal("failed to update coder binary", clog.Causef(err.Error()))
213+
}
214+
215+
clog.LogSuccess("Updated coder CLI to version " + desiredVersion.String())
216+
return nil
217+
}
218+
219+
func (u *updater) doUpdate(ctx context.Context, updatedCoderBinaryPath string) error {
220+
var err error
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+
if u.osF() == goosWindows {
225+
err = u.fs.Rename(u.executablePath, updatedCoderBinaryPath+".old")
226+
if err != nil {
227+
return xerrors.Errorf("windows: rename current coder binary: %w", err)
228+
}
229+
err = u.fs.Rename(updatedCoderBinaryPath, u.executablePath)
230+
if err != nil {
231+
return xerrors.Errorf("windows: rename updated coder binary: %w", err)
232+
}
233+
return nil
234+
}
235+
211236
// validate that we can execute the new binary before overwriting
212237
updatedVersionOutput, err := u.execF(ctx, updatedCoderBinaryPath, "--version")
213238
if err != nil {
214-
return clog.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+
return xerrors.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
if err = u.fs.Rename(updatedCoderBinaryPath, u.executablePath); err != nil {
223-
return clog.Fatal("failed to update coder binary in-place", clog.Causef(err.Error()))
244+
return xerrors.Errorf("update coder binary in-place: %w", err)
224245
}
225246

226-
clog.LogSuccess("Updated coder CLI to version " + desiredVersion.String())
227247
return nil
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 to check version of updated coder binary", "")
348+
assertCLIError(t, "update coder - cannot exec new binary", err, "failed to update coder binary", fakeError.Error())
349349
assertFileContent(t, p.Fakefs, fakeExePathLinux, fakeOldVersion)
350350
})
351351

352352
if runtime.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 { return fakeOldVersion }
359+
p.ConfirmF = fakeConfirmYes
360+
p.OsF = func() string { return goosWindows }
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy