-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create md-table-action binary for use in a GitHub Actions environment
- Loading branch information
Showing
5 changed files
with
204 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,17 @@ description: A custom action written in Rust which generates markdown tables for | |
author: Brenek Harrison <[email protected]> | ||
|
||
inputs: | ||
file-path: | ||
description: "The path to the JSON file to use" | ||
required: true | ||
token: | ||
description: 'The GitHub token to use for downloading the action, defaults to workflow token' | ||
description: 'The GitHub token to use for downloading the action binary, defaults to workflow token' | ||
required: true | ||
default: ${{ github.token }} | ||
outputs: | ||
output-file: | ||
description: "The path of the generated markdown table file" | ||
value: ${{ steps.run.outputs.outputfile }} | ||
error: | ||
description: 'The description of any error that occurred' | ||
value: ${{ steps.run.outputs.error }} | ||
|
@@ -18,9 +24,9 @@ runs: | |
- name: Set reusable variables | ||
shell: bash | ||
run: | | ||
echo "action_repo=markdown-table-generator" >> $GITHUB_ENV | ||
echo "action_repo=md-table-action" >> $GITHUB_ENV | ||
echo "action_org=Locatarr" >> $GITHUB_ENV | ||
echo "binary_name=markdown-table-generator" >> $GITHUB_ENV | ||
echo "binary_name=md-table-action" >> $GITHUB_ENV | ||
- name: Add binary extension | ||
shell: bash | ||
|
@@ -50,4 +56,4 @@ runs: | |
- name: Run Action | ||
shell: bash | ||
id: run | ||
run: ./${{ runner.os }}/${{ env.binary_name }} | ||
run: ./${{ runner.os }}/${{ env.binary_name }} "${{ inputs.file-path }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::{env, path::PathBuf}; | ||
|
||
use gha_main::anyhow; | ||
use gha_main::{gha_main, gha_output, GitHubActionResult}; | ||
use uuid::Uuid; | ||
|
||
use markdown_table_generator::generate_md_table; | ||
use markdown_table_generator::models::Applications; | ||
|
||
#[gha_main] | ||
fn main() -> GitHubActionResult { | ||
let runner_temp_dir: PathBuf = env::var("RUNNER_TEMP")?.into(); | ||
let unique_id = Uuid::new_v4().to_string(); | ||
|
||
// Get a PathBuf with the input file path | ||
let args: Vec<String> = env::args().collect(); | ||
let input_file = args | ||
.get(1) | ||
.ok_or(anyhow::anyhow!("Input file not provided"))?; | ||
let input_filepath: PathBuf = input_file.into(); | ||
|
||
// Open the file and get a stream | ||
let input_stream = std::fs::File::open(&input_filepath)?; | ||
|
||
// Deserialize input file into correct struct and generate markdown table | ||
let apps_json: Applications = serde_json::from_reader(input_stream)?; | ||
let md_table = generate_md_table(&apps_json); | ||
|
||
// Create output filepath in form "markdown-table-<original filename>.md" | ||
// in the runner temporary directory (gets reset after each job, but persistent across steps). | ||
// If there's an issue reading the filepath name, use a uuid v4 instead. | ||
let in_file_stem = input_filepath | ||
.file_stem() | ||
.map(|a| a.to_str().unwrap_or(&unique_id)) | ||
.unwrap_or(&unique_id); | ||
let out_file_path = runner_temp_dir.join("markdown-table-".to_owned() + in_file_stem + ".md"); | ||
|
||
// Write markdown table to output file | ||
std::fs::write(&out_file_path, md_table)?; | ||
|
||
// Export output file path as a GitHub Actions output for other steps to consume | ||
let output_file = out_file_path.to_str().unwrap(); | ||
gha_output!(output_file); | ||
|
||
Ok(()) | ||
} |