Skip to content

Commit 5629b1b

Browse files
committed
Support processing inputs and outputting results correctly
1 parent 4db3f30 commit 5629b1b

File tree

6 files changed

+65
-13
lines changed

6 files changed

+65
-13
lines changed

lib/CLI.ex

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
defmodule ElixirScript.CLI do
22
alias ElixirScript.Context
33
alias ElixirScript.Core
4+
alias ElixirScript.CustomLogger, as: Logger
45

56
def main(_args \\ []) do
6-
script = Core.get_input("script", required: true)
7+
Logger.debug("Running in debug mode")
8+
Logger.debug("All Environment Variables: #{inspect(System.get_env(), limit: :infinity, printable_limit: :infinity)}")
9+
10+
script = Core.get_env_input("script", required: true)
11+
Logger.debug("Script input: #{inspect(script, limit: :infinity, printable_limit: :infinity)}")
12+
713
{value, _binding} = Code.eval_string(script, context: Context.from_github_environment())
814
Core.set_output("result", value)
15+
Logger.debug("Result output: #{inspect(value, limit: :infinity, printable_limit: :infinity)}")
16+
end
17+
18+
defp log_debug(message) do
19+
if @debug_mode, do: Logger.debug(message)
920
end
1021
end

lib/command.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
defmodule ElixirScript.Command do
2+
alias ElixirScript.CommandUtils
3+
4+
def issue_command(command, name, command_value) do
5+
IO.puts("::#{command} name=#{name}::#{command_value}")
6+
end
7+
end

lib/command_utils.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule ElixirScript.CommandUtils do
2+
23
def to_command_value(nil), do: ""
34

45
def to_command_value(input) when is_binary(input), do: input

lib/core.ex

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
defmodule ElixirScript.Core do
2+
alias ElixirScript.Command
3+
alias ElixirScript.CommandUtils
24
alias ElixirScript.FileCommand
35

4-
def get_input(name, opts \\ []) do
6+
def parse_args(args) do
7+
aliases = [script: :s, debug: :d]
8+
parsed = OptionParser.parse(args, aliases: aliases)
9+
10+
case parsed do
11+
{opts, _remaining_args, _invalid_opts} ->
12+
%{script: Map.get(opts, :script), debug: Map.get(opts, :debug)}
13+
end
14+
end
15+
16+
def get_env_input(name, opts \\ []) do
517
required = Keyword.get(opts, :required, false)
618
trim_whitespace = Keyword.get(opts, :trim_whitespace, true)
719

@@ -21,6 +33,13 @@ defmodule ElixirScript.Core do
2133
end
2234

2335
def set_output(name, value) do
24-
FileCommand.issue_file_command("OUTPUT", FileCommand.prepare_key_value_message(name, value))
36+
if System.get_env("GITHUB_OUTPUT") do
37+
FileCommand.issue_file_command(
38+
"OUTPUT",
39+
FileCommand.prepare_key_value_message(name, CommandUtils.to_command_value(value))
40+
)
41+
else
42+
Command.issue_command('set-output', name, CommandUtils.to_command_value(value))
43+
end
2544
end
2645
end

lib/custom_logger.ex

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
defmodule ElixirScript.CustomLogger do
2+
@moduledoc """
3+
A custom logger module suitable for GitHub Actions' debug mode.
4+
"""
5+
6+
def debug(message) do
7+
if debug_mode?(), do: log(:debug, message)
8+
end
9+
10+
defp debug_mode?() do
11+
System.get_env("INPUT_DEBUG") == "true"
12+
end
13+
14+
defp log(level, message) do
15+
IO.puts("[#{level}] #{message}")
16+
end
17+
end

lib/file_command.ex

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
defmodule ElixirScript.FileCommand do
22
alias ElixirScript.CommandUtils
33

4-
def issue_file_command(command, message) do
5-
file_path = System.fetch_env!("GITHUB_#{command}")
4+
def issue_file_command(command, command_value) do
5+
file_path = System.get_env("GITHUB_#{command}", "")
66

77
if not File.exists?(file_path) do
88
raise "Missing file at path: #{file_path}"
99
end
1010

11-
File.write!(file_path, CommandUtils.to_command_value(message) <> "\n", [:append])
11+
File.write!(file_path, command_value <> "\n", [:append])
1212
end
1313

1414
@doc """
@@ -18,27 +18,24 @@ defmodule ElixirScript.FileCommand do
1818
It encodes that message using a specific delimiter format "ghadelimiter_" followed by a unique reference.
1919
2020
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.
21+
I can't find this behavior described in GitHub documentation.
2322
2423
## Raises:
2524
- An error if the key or converted value contains the delimiter
2625
(this can't realistically happen as the delimiter is randomly generated, but better cautious than sorry)
2726
"""
28-
def prepare_key_value_message(key, value) do
27+
def prepare_key_value_message(key, command_value) do
2928
delimiter = "ghadelimiter_" <> to_string(:erlang.ref_to_list(:erlang.make_ref()))
3029

31-
converted_value = CommandUtils.to_command_value(value)
32-
3330
cond do
3431
String.contains?(key, delimiter) ->
3532
raise "Unexpected input: name should not contain the delimiter \"#{delimiter}\""
3633

37-
String.contains?(converted_value, delimiter) ->
34+
String.contains?(command_value, delimiter) ->
3835
raise "Unexpected input: value should not contain the delimiter \"#{delimiter}\""
3936

4037
true ->
41-
"#{key}<<#{delimiter}\n#{converted_value}\n#{delimiter}"
38+
"#{key}<<#{delimiter}\n#{command_value}\n#{delimiter}"
4239
end
4340
end
4441
end

0 commit comments

Comments
 (0)