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

refactor: use constants for OSs #448

Merged
merged 1 commit into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/cmd/configssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func binPath() (string, error) {
// Bash and OpenSSH for Windows (used by Powershell and VS Code) to function
// correctly. Check if the current executable is in $PATH, and warn the user
// if it isn't.
if runtime.GOOS == "windows" {
if runtime.GOOS == goosWindows {
binName := filepath.Base(exePath)

// We use safeexec instead of os/exec because os/exec returns paths in
Expand Down Expand Up @@ -268,7 +268,7 @@ func makeSSHConfig(binPath, workspaceName, privateKeyFilepath string, additional
fmt.Sprintf("IdentityFile=%q", privateKeyFilepath),
)

if runtime.GOOS == "linux" || runtime.GOOS == "darwin" {
if runtime.GOOS == goosLinux || runtime.GOOS == goosDarwin {
options = append(options,
"ControlMaster auto",
"ControlPath ~/.ssh/.connection-%r@%h:%p",
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
const (
goosWindows = "windows"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unfortunate that Go doesn't seem to offer these constants in their code anywhere. It looks like Go themselves use hardcoded string comparisons everywhere: https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/golang/go%24+%22windows%22&patternType=literal -- given that, maybe it's okay for us to do that too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I think that doesn't sit well unless at least a type (like TS union of strings as a type alias) is defined. That feels a bit like walking on quicksand.

I don't know their reason, but I think it's reasonable we reach for them in our own domain model/application code base. I'd expect there to be some concept of a constant, enumeration or type in other languages when approaching a similar problem. Go sometimes makes odd choices IMO :P

goosLinux = "linux"
goosDarwin = "darwin"
apiPrivateVersion = "/api/private/version"
)

Expand Down Expand Up @@ -181,7 +182,7 @@ func (u *updater) Run(ctx context.Context, force bool, coderURLArg string, versi
// TODO: validate the checksum of the downloaded file. GitHub does not currently provide this information
// and we do not generate them yet.
var updatedBinaryName string
if u.osF() == "windows" {
if u.osF() == goosWindows {
updatedBinaryName = "coder.exe"
} else {
updatedBinaryName = "coder"
Expand Down