Embed Directive in Fortress
Here’s the translation of the Go code to Fortress, formatted in Markdown suitable for Hugo:
The embed directive in Fortress allows programs to include arbitrary files and folders in the binary at build time. This feature is similar to resource bundling in other languages.
component main
import System.IO
import System.FileSystem
(* Embed the contents of a single file into a String *)
val fileString : String = FileSystem.embedFileAsString("folder/single_file.txt")
(* Embed the contents of a file into a ByteArray *)
val fileByte : ByteArray = FileSystem.embedFileAsByteArray("folder/single_file.txt")
(* Embed multiple files or folders *)
val folder : EmbeddedFileSystem = FileSystem.embedFolder("folder")
export run()
(* Print out the contents of single_file.txt *)
println(fileString)
println(String.fromByteArray(fileByte))
(* Retrieve some files from the embedded folder *)
val content1 = folder.readFileAsString("folder/file1.hash")
println(content1)
val content2 = folder.readFileAsString("folder/file2.hash")
println(content2)
end
end
In this Fortress example, we’re using hypothetical FileSystem
and EmbeddedFileSystem
APIs to demonstrate the concept of embedding files. The FileSystem.embedFileAsString
and FileSystem.embedFileAsByteArray
functions are used to embed single files, while FileSystem.embedFolder
is used to embed multiple files or folders.
To run this example, you would first need to create the necessary files:
$ mkdir -p folder
$ echo "hello fortress" > folder/single_file.txt
$ echo "123" > folder/file1.hash
$ echo "456" > folder/file2.hash
Then, you can compile and run the Fortress program:
$ fortress main.fss
hello fortress
hello fortress
123
456
This example demonstrates how Fortress can embed files into the program binary, allowing easy access to external resources without needing to manage separate files during deployment.