Read environment variables and standard directories
Configure a script from its environment, and keep its files where the user's other programs keep theirs.
Read a variable with a default
Call Os.env. It returns null when the variable is not set, so give the default with ??:
let editor = Os.env("EDITOR") ?? "vi"
let pager = Os.env("PAGER") ?? "less"
println("${editor} ${pager}")nano lessA variable set to the empty string is "", not null. To treat a flag as on whenever it is set, compare with null:
let verbose = Os.env("VERBOSE") != null
println(verbose)trueParse a number
Convert the text with String.toInt, which returns null for text that is not a number. Chain it with ?. so that a missing and a malformed variable both fall back to the default:
let workers = Os.env("WORKERS")?.toInt() ?? 4
let retries = Os.env("RETRIES")?.toInt() ?? 3
println("${workers} workers, ${retries} retries")8 workers, 3 retriestoInt takes no surrounding spaces, so " 8" falls back to the default too; call String.trim before .toInt() to accept them:
let workers = Os.env("WORKERS")?.trim().toInt() ?? 4
println(workers)8Require a variable
Stop the script with an error when a variable it cannot do without is missing. ?? return returns the Err that Result.fail makes from the script, which prints API_TOKEN is not set to standard error and exits with status 1. String.length counts what it got:
let token = Os.env("API_TOKEN") ?? return Result.fail("API_TOKEN is not set")
println("token has ${token.length()} characters")List variables
Call Os.environ for a map of every variable. Its order is the host's, so take the names with Map.keys, keep the ones you want with Array.filter and String.startsWith, and sort them with Array.sort:
let names = Os.environ()
| Map.keys()
| Array.filter({ x.startsWith("APP_") })
| Array.sort()
println(names)["APP_HOST", "APP_PORT"]The map is a copy: changing it changes neither the environment nor what Os.env returns.
Find the standard directories
Os.home returns the user's home directory, Os.configDir the directory for configuration, and Os.cacheDir the directory for files the program can make again. Each returns a Result, which is Err when the variable it rests on, such as HOME, is unset or empty, or when an XDG variable holds a relative path.
Build a path under the home directory with Path.join; Path.base gives its last element:
let rc = Path.join([try Os.home(), ".greetrc"])
println(Path.base(rc)).greetrcThe directories are shared by every program, and may not exist yet, so make one of your own under them with File.mkdir, which creates the parents too:
let config = try Os.configDir()
let cache = try Os.cacheDir()
let settings = Path.join([config, "greet"])
let downloads = Path.join([cache, "greet"])
try File.mkdir(settings)
try File.mkdir(downloads)
println(File.exists(settings) && File.exists(downloads))trueThe directory names differ by platform: the configuration directory is $XDG_CONFIG_HOME or ~/.config on Unix, ~/Library/Application Support on macOS, and %AppData% on Windows; the cache directory is $XDG_CACHE_HOME or ~/.cache, ~/Library/Caches, and %LocalAppData%.
Use a temporary directory
For files that last only one run, call File.tempDir. It creates a new directory in the host's temporary directory, under a name no other program has. Nothing removes it, so remove it with File.removeAll in a defer right after you make it. Path.base gives the name it picked:
let scratch = try File.tempDir("greet-*")
defer { File.removeAll(scratch) }
try File.write(Path.join([scratch, "draft.txt"]), "hello")
println(Path.base(scratch).startsWith("greet-"))trueKeep a file with a fixed name in your configuration or cache directory, not in the temporary one, where another user can take the name first. Use a temporary file or directory shows more.
The whole program
This script greets a name saved in its configuration directory, which it creates with a default on the first run. GREET_NAME overrides the saved name, and GREET_TIMES says how many times to greet:
let config = try Os.configDir()
let dir = Path.join([config, "greet"])
try File.mkdir(dir)
let path = Path.join([dir, "name.txt"])
if !File.exists(path) {
try File.write(path, "world\n")
}
let saved = try File.read(path)
let name = Os.env("GREET_NAME") ?? saved.trim()
let times = Os.env("GREET_TIMES")?.toInt() ?? 1
for _ in Iter.range(0, times) {
println("Hello, ${name}")
}Hello, world
Hello, worldBesides the functions above, it uses File.exists, File.write, File.read, String.trim, and Iter.range.
