Skip to content

Commit 09bf336

Browse files
committed
Very basic script to evaluate script via GHA input and output result to GHA output
Very much inspired by https://github.com/actions/github-script
1 parent c9519de commit 09bf336

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed

lib/CLI.ex

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
defmodule ElixirScript.CLI do
2-
def main(args \\ []) do
3-
IO.puts "ElixirScript.CLI 👋"
4-
IO.inspect args
2+
alias ElixirScript.Core
3+
4+
def main(_args \\ []) do
5+
script = Core.get_input("script", required: true)
6+
{value, _binding} = Code.eval_string(script)
7+
Core.set_output("result", value)
58
end
69
end

lib/command_utils.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
defmodule ElixirScript.CommandUtils do
2+
def to_command_value(nil), do: ""
3+
4+
def to_command_value(input) when is_binary(input), do: input
5+
end

lib/core.ex

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
defmodule ElixirScript.Core do
2+
alias ElixirScript.FileCommand
3+
4+
def get_input(name, opts \\ []) do
5+
required = Keyword.get(opts, :required, false)
6+
trim_whitespace = Keyword.get(opts, :trim_whitespace, true)
7+
8+
env_var_name = "INPUT_#{String.replace(name, " ", "_") |> String.upcase()}"
9+
val = System.get_env(env_var_name, "")
10+
11+
cond do
12+
required && val == "" ->
13+
raise "Input '#{name}' required but not supplied in environment variable '#{env_var_name}'"
14+
15+
trim_whitespace == false ->
16+
val
17+
18+
true ->
19+
String.trim(val)
20+
end
21+
end
22+
23+
def set_output(name, value) do
24+
FileCommand.issue_file_command("OUTPUT", FileCommand.prepare_key_value_message(name, value))
25+
end
26+
end

lib/file_command.ex

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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

Comments
 (0)