8000 feat(cli): allow specifying name of provisioner daemon by johnstcn · Pull Request #11077 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat(cli): allow specifying name of provisioner daemon #11077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 7, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
strip domain suffix of host
  • Loading branch information
johnstcn committed Dec 7, 2023
commit 80e49ffc3c612878494fefc70af4b7b4b920607d
31 changes: 23 additions & 8 deletions cli/cliutil/hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cliutil

import (
"os"
"strings"
"sync"

"golang.org/x/xerrors"
Expand All @@ -12,14 +13,28 @@ var (
hostnameOnce sync.Once
)

// Hostname returns the hostname of the machine, lowercased,
// with any trailing domain suffix stripped.
// It is cached after the first call.
// If the hostname cannot be determined, this will panic.
func Hostname() string {
hostnameOnce.Do(func() {
h, err := os.Hostname()
if err != nil {
// Something must be very wrong if this fails.
panic(xerrors.Errorf("lookup hostname: %w", err))
}
hostname = h
})
hostnameOnce.Do(func() { hostname = getHostname() })
return hostname
}

func getHostname() string {
h, err := os.Hostname()
if err != nil {
// Something must be very wrong if this fails.
panic(xerrors.Errorf("lookup hostname: %w", err))
}

// On some platforms, the hostname can be an FQDN. We only want the hostname.
if idx := strings.Index(h, "."); idx != -1 {
h = h[:idx]
}

// For the sake of consistency, we also want to lowercase the hostname.
// Per RFC 4343, DNS lookups must be case-insensitive.
return strings.ToLower(h)
}
0