mirror of
https://github.com/roc-lang/roc.git
synced 2024-09-22 08:17:40 +03:00
52 lines
1.7 KiB
Plaintext
52 lines
1.7 KiB
Plaintext
app "file-io"
|
|
packages { pf: "cli-platform/main.roc" }
|
|
imports [
|
|
pf.Program.{ Program, ExitCode },
|
|
pf.Stdout,
|
|
pf.Stderr,
|
|
pf.Task.{ Task },
|
|
pf.File,
|
|
pf.Path,
|
|
pf.Env,
|
|
pf.Dir,
|
|
]
|
|
provides [main] to pf
|
|
|
|
main : Program
|
|
main = Program.noArgs mainTask
|
|
|
|
mainTask : Task ExitCode [] [Write [File, Stdout, Stderr], Read [File], Env]
|
|
mainTask =
|
|
path = Path.fromStr "out.txt"
|
|
task =
|
|
cwd <- Env.cwd |> Task.await
|
|
cwdStr = Path.display cwd
|
|
|
|
_ <- Stdout.line "cwd: \(cwdStr)" |> Task.await
|
|
dirEntries <- Dir.list cwd |> Task.await
|
|
contentsStr = Str.joinWith (List.map dirEntries Path.display) "\n "
|
|
|
|
_ <- Stdout.line "Directory contents:\n \(contentsStr)\n" |> Task.await
|
|
_ <- Stdout.line "Writing a string to out.txt" |> Task.await
|
|
_ <- File.writeUtf8 path "a string!" |> Task.await
|
|
contents <- File.readUtf8 path |> Task.await
|
|
Stdout.line "I read the file back. Its contents: \"\(contents)\""
|
|
|
|
Task.attempt task \result ->
|
|
when result is
|
|
Ok {} ->
|
|
Stdout.line "Successfully wrote a string to out.txt"
|
|
|> Program.exit 0
|
|
|
|
Err err ->
|
|
msg =
|
|
when err is
|
|
FileWriteErr _ PermissionDenied -> "PermissionDenied"
|
|
FileWriteErr _ Unsupported -> "Unsupported"
|
|
FileWriteErr _ (Unrecognized _ other) -> other
|
|
FileReadErr _ _ -> "Error reading file"
|
|
_ -> "Uh oh, there was an error!"
|
|
|
|
Stderr.line msg
|
|
|> Program.exit 1
|