Skip to content

Os.hostname ​

ant
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:

ant
println(Os.hostname())

In a script, try unwraps the name, or ends the script with the error when the name cannot be read:

ant
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:

ant
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:

ant
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:

ant
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:

ant
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:

ant
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:

ant
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:

ant
let host: string = Os.hostname() // error: cannot use Result[string, Error] where string is expected

try 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:

ant
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