|
| 1 | +defmodule ElixirScript.FileCommand do |
| 2 | + alias ElixirScript.CommandUtils |
| 3 | + |
| 4 | + def issue_file_command(command, message) do |
| 5 | + file_path = System.fetch_env!("GITHUB_#{command}") |
| 6 | + |
| 7 | + if not File.exists?(file_path) do |
| 8 | + raise "Missing file at path: #{file_path}" |
| 9 | + end |
| 10 | + |
| 11 | + File.write!(file_path, CommandUtils.to_command_value(message) <> "\n", [:append]) |
| 12 | + end |
| 13 | + |
| 14 | + @doc """ |
| 15 | + Prepares a key-value message in a format that uses a unique GitHub Actions-style delimiter |
| 16 | +
|
| 17 | + This function safely prepares a message based on the provided key and value. |
| 18 | + It encodes that message using a specific delimiter format "ghadelimiter_" followed by a unique reference. |
| 19 | +
|
| 20 | + Note: This format is taken from https://github.com/actions/toolkit/blob/main/packages/core/src/file-command.ts#L27 |
| 21 | + I don't really understand how this delimiter works, and I can't find it documented. |
| 22 | + But I guess GitHub Actions listens for the "ghadelimiter_" part and then uses that to delimit the message. |
| 23 | +
|
| 24 | + ## Raises: |
| 25 | + - An error if the key or converted value contains the delimiter |
| 26 | + (this can't realistically happen as the delimiter is randomly generated, but better cautious than sorry) |
| 27 | + """ |
| 28 | + def prepare_key_value_message(key, value) do |
| 29 | + delimiter = "ghadelimiter_" <> to_string(:erlang.ref_to_list(:erlang.make_ref())) |
| 30 | + |
| 31 | + converted_value = CommandUtils.to_command_value(value) |
| 32 | + |
| 33 | + cond do |
| 34 | + String.contains?(key, delimiter) -> |
| 35 | + raise "Unexpected input: name should not contain the delimiter \"#{delimiter}\"" |
| 36 | + |
| 37 | + String.contains?(converted_value, delimiter) -> |
| 38 | + raise "Unexpected input: value should not contain the delimiter \"#{delimiter}\"" |
| 39 | + |
| 40 | + true -> |
| 41 | + "#{key}<<#{delimiter}\n#{converted_value}\n#{delimiter}" |
| 42 | + end |
| 43 | + end |
| 44 | +end |
0 commit comments