Os.home
func Os.home(): Result[string, Error]Os.home returns the user's home directory: $HOME on Unix, %USERPROFILE% on Windows.
Details
Os.homeisErrwhen$HOME(%USERPROFILE%on Windows) is unset or empty.- A path is used as written:
~and$HOMEin a path are not expanded, so a path under the home directory isPath.join([try Os.home(), ...]). - The home directory is shared by every program. Keep configuration under Os.configDir and files that can be made again under Os.cacheDir, in a directory named after your program.
Basic examples
Print the home directory:
let home = try Os.home()
println(home)The home directory is an absolute path:
let home = try Os.home()
println(Path.isAbs(home))trueApplications
Find a file that a tool keeps in the home directory by convention, such as the SSH client configuration. Result.wrapErr says which step failed when $HOME is unset:
func sshConfigPath(): Result[string, Error] {
let home = try (Os.home() | Result.wrapErr("locate the SSH configuration"))
Result.ok(Path.join([home, ".ssh", "config"]))
}
let home = try Os.home()
let path = try sshConfigPath()
println(try Path.rel(home, path)).ssh/configExpand a leading ~ in a path the user typed, as a shell does:
func expandHome(path: string): Result[string, Error] {
if path != "~" && !path.startsWith("~/") { return Result.ok(path) }
let home = try Os.home()
Result.ok(Path.join([home, path.trimPrefix("~")]))
}
let home = try Os.home()
let config = try expandHome("~/.ssh/config")
println(try Path.rel(home, config))
println(try expandHome("/etc/hosts"))
println(try expandHome("~") == home).ssh/config
/etc/hosts
trueShorten a path under the home directory to ~ for display:
func tildify(path: string, home: string): string {
if path == home { return "~" }
if path.startsWith("${home}/") { return "~${path.trimPrefix(home)}" }
path
}
let home = try Os.home()
println(tildify(Path.join([home, "src", "app"]), home))
println(tildify(home, home))
println(tildify("/etc/hosts", home))~/src/app
~
/etc/hostsProperties and relations
On Unix, Os.home is the value of $HOME:
if Os.platform() != "windows" {
let home = try Os.home()
assert(Os.env("HOME") == home)
}Possible issues
~ is shell syntax. File.read("~/notes.txt") looks for a directory named ~ in the working directory. Expand it with Os.home first, as expandHome does under Applications:
println(File.read("~/notes.txt"))Err{ error: Error{ message: "open ~/notes.txt: no such file or directory" } }A program started without $HOME gets Err from Os.home. Here a child Ant program runs with an env that holds only PATH, which Cmd.new gives it in place of the parent's:
let dir = try File.tempDir("home-*")
defer { File.removeAll(dir) }
let script = Path.join([dir, "main.ant"])
try File.write(script, "println(Os.home())")
let child = Cmd.new("ant", ["run", script], env: Map{ "PATH": Os.env("PATH") ?? "" })
print(try Cmd.read(child))Err{ error: Error{ message: "$HOME is not defined" } }Where the program can go on without it, fall back to a directory of its own choosing with Result.unwrapOr:
let home = Os.home().unwrapOr(try Os.cwd())
println(Path.isAbs(home))trueSee also
Os.configDir · Os.cacheDir · Os.cwd · Os.env · Path.join · Path.rel · File.mkdir
