Skip to content

Commit 472cb69

Browse files
committed
Add log_content function
1 parent ba71276 commit 472cb69

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

lib/core.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,10 @@ defmodule ElixirScript.Core do
4242
Command.issue_command('set-output', name, CommandUtils.to_command_value(value))
4343
end
4444
end
45+
46+
def log_output do
47+
if System.get_env("GITHUB_OUTPUT") do
48+
EnvironmentFileCommand.log_output()
49+
end
50+
end
4551
end

lib/github_actions/environment_file_command.ex

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,58 @@ defmodule ElixirScript.GitHubActions.EnvironmentFileCommand do
4444
"#{key}<<#{delimiter}\n#{command_value}\n#{delimiter}"
4545
end
4646
end
47+
48+
@doc """
49+
Logs the given `content` to the console. If a `label` is provided, it is
50+
printed before the content, and the content is indented by two spaces.
51+
52+
## Examples
53+
54+
iex> ElixirScript.GitHubActions.EnvironmentFileCommand.log_content("Hello, world!", "Greeting")
55+
Greeting:
56+
Hello, world!
57+
58+
iex> ElixirScript.GitHubActions.EnvironmentFileCommand.log_content("Hello, world!")
59+
Hello, world!
60+
61+
"""
62+
def log_content(content, label \\ nil) do
63+
indented_content = String.replace(content, "\n", "\n ")
64+
65+
case label do
66+
nil -> IO.puts(indented_content)
67+
_ -> IO.puts("#{label}:\n #{indented_content}")
68+
end
69+
end
70+
71+
@doc """
72+
Logs the contents of the files specified by the "GITHUB_OUTPUT" and "GITHUB_ENV"
73+
environment variables to the console, each under its own labeled section.
74+
75+
## Example
76+
77+
# Assuming GITHUB_OUTPUT and GITHUB_ENV are set to valid file paths
78+
# with the contents "output content" and "env content" respectively.
79+
iex> ElixirScript.GitHubActions.EnvironmentFileCommand.log_output()
80+
::group::Set outputs
81+
GITHUB_OUTPUT:
82+
output content
83+
GITHUB_ENV:
84+
env content
85+
::endgroup::
86+
"""
87+
def log_output do
88+
IO.puts "::group::Set outputs"
89+
"GITHUB_OUTPUT"
90+
|> System.get_env()
91+
|> File.read!()
92+
|> log_content("GITHUB_OUTPUT")
93+
94+
"GITHUB_ENV"
95+
|> System.get_env()
96+
|> File.read!()
97+
|> log_content("GITHUB_ENV")
98+
99+
IO.puts "::endgroup::"
100+
end
47101
end

0 commit comments

Comments
 (0)