is there possibility to append text to a file, it would be extremely useful for now I am always replacing content
app "append"
packages {
cli: "https://github.com/roc-lang/basic-cli/releases/download/0.5.0/Cufzl36_SnJ4QbOoEmiJ5dIpUxBvdB3NEySvuH82Wio.tar.br",
}
imports [
cli.File,
cli.Path.{ Path },
cli.Task.{ Task },
cli.Stdout,
]
provides [main] to cli
main =
result <- appendTask (Path.fromStr "filename.txt") (Str.toUtf8 "This is some text to append\n") |> Task.attempt
when result is
Ok {} -> Stdout.line "Success"
Err msg -> Stdout.line "Failed with \(msg)"
appendTask : Path, List U8 -> Task {} Str
appendTask = \path, bytes ->
path
|> File.readBytes
|> Task.await \contents ->
File.writeBytes path (List.concat contents bytes)
|> Task.mapErr \err ->
when err is
FileReadErr _ _ -> "file read error"
FileWriteErr _ _ -> "file write error"
I tried using Cmd
and doing something like echo "This is some text to append" >> filename.txt
but couldn't get that working.
Maybe worth adding another function to basic-cli
file module for just appending some content.
Yes, I've made an issue for it. I want to take some time to think about the best API, so I recommend downloading the basic-cli files and adding append functions yourself if you need highly performant appending, Luke's solution will be slower but will likely be fast enough for the majority of cases.
Sounds good, when you have a good idea I'm happy to take a stab at it. I would like to remove the duplication of IOError types in the InternalX.roc files and remove the redundant glue code.
@Luke Boswell for the API, I would go with separate functions:
append
appendBytes
appendUtf8
Writing to a file is so common, so I don't want to add visual or performance clutter to the existing write functions.
Thank you Anton, I've added this to my todo list, I should get to it sometime this week. I'm getting a bit distracted with another attempt at a graphics platform using zig, I feel like I'm really close this time :fingers_crossed: .
Hiya!
⮞ roc dev
Downloading https://github.com/roc-lang/basic-cli/releases/download/0.5.0/Cufzl36_SnJ4QbOoEmiJ5dIpUxBvdB3NEySvuH82Wio.tar.br
into /home/james/.cache/roc/packages
How big is this tarball? I'm trying to figure out whether it's just taking a while or it's hanging
ok it's medium sized, it downloads pretty quick when I fetch it in my browser. So I think this is a bug?
I opened an issue: https://github.com/roc-lang/roc/issues/5887
:thinking: I wonder if the download is succeeding, but then something after that is hanging...can you check if the file it says it was downloading made it into that directory?
my guess is that it made it there (and that it's got the same contents as if you curl it)
Last updated: Jul 06 2025 at 12:14 UTC