Os.hostname
func Os.hostname(): Result[string, Error]Os.hostname returns the name the host's kernel reports, as hostname prints it, Go's os.Hostname. It is not looked up in DNS, so it may or may not hold a domain. It is Err only when the host cannot report one.
Basic examples
Print the result as it comes back:
println(Os.hostname())In a script, try unwraps the name, or ends the script with the error when the name cannot be read:
let host = try Os.hostname()
println("running on ${host}")Scope
A fallback name
Result.unwrapOr gives a name to use when the host's name cannot be read, so the program goes on:
let host = Os.hostname().unwrapOr("unknown")
println("[${host}] started")Inside a function
In a function that returns a Result, try passes the error on to the caller. Wrap it first to say which step failed:
func instanceId(): Result[string, Error] {
let host = try (Os.hostname() | Result.wrapErr("read the host name"))
Result.ok("${host}:${Os.pid()}")
}
println(instanceId())Applications
Short host name
A host name may be fully qualified. Keep the part before the first dot:
func shortName(host: string): string { host.cut(".")?.before ?? host }
shortName("web-1.example.com") // "web-1"
shortName("laptop") // "laptop"Then apply it to this host:
let host = try Os.hostname()
println(shortName(host))A file name per host
Machines that share a directory each write their own file when the name includes the host:
let host = try Os.hostname()
let path = "status-${host}.txt"
try File.write(path, "ok\n")
println(try File.read(path))Choose a role by host
An if expression picks a setting from the host's name:
let host = Os.hostname().unwrapOr("")
let role = if host.startsWith("build-") { "builder" } else { "worker" }
println("${host}: ${role}")Possible issues
The result is not a string
Os.hostname returns a Result, which a string does not accept:
let host: string = Os.hostname() // error: cannot use Result[string, Error] where string is expectedtry binds before a pipe
try applies to the call right after it, so try Os.hostname() | Result.wrapErr(…) pipes the unwrapped string into Result.wrapErr. Parenthesize the pipeline, as instanceId does above:
func label(): Result[string, Error] {
let host = try Os.hostname() | Result.wrapErr("read the host name")
// error: cannot use string where Result[T, Error] is expected
Result.ok(host)
}See also
Os.platform, Os.arch, Os.pid, Os.uid, Os.env, Result.unwrapOr, Result.wrapErr
