Skip to content

Commit

Permalink
Merge pull request #14 from gleich/custom-output-file
Browse files Browse the repository at this point in the history
feat: custom output file
  • Loading branch information
gleich authored Dec 13, 2022
2 parents 3bc0649 + 93306b9 commit a671376
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 20 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ anyhow = "1.0.40"
tracing = "0.1"
tracing-subscriber = "0.2.0"
percent-encoding = "2.1.0"
lazy_static = "1.4.0"
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ inputs:
required: false
default: '🚀'
description: 'The emoji for the projects column'
output_file:
required: false
default: README.md
description: 'File to output to'
19 changes: 12 additions & 7 deletions src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use serde::Deserialize;

#[derive(PartialEq, Debug)]
pub struct Env {
pub path: PathBuf,
pub badges: bool,
pub technology_emoji: char,
pub project_emoji: char,
pub output_file: PathBuf,
pub config_filename: PathBuf,
}

fn get_env_var(name: &str, default: &str) -> Result<String, anyhow::Error> {
Expand All @@ -19,10 +20,11 @@ fn get_env_var(name: &str, default: &str) -> Result<String, anyhow::Error> {

pub fn env_vars() -> Result<Env, anyhow::Error> {
Ok(Env {
path: Path::new(&get_env_var("path", "stack.yml")?).to_owned(),
badges: get_env_var("badges", "true")?.parse()?,
technology_emoji: get_env_var("technology_emoji", "💻")?.parse()?,
project_emoji: get_env_var("project_emoji", "🚀")?.parse()?,
config_filename: Path::new(&get_env_var("path", "stack.yml")?).to_owned(),
output_file: Path::new(&get_env_var("output_file", "README.md")?).to_owned(),
})
}

Expand All @@ -46,7 +48,7 @@ pub struct Technology {
}

pub fn config_file(env_conf: &Env) -> Result<Vec<Technology>, anyhow::Error> {
let content = fs::read_to_string(&env_conf.path)?;
let content = fs::read_to_string(&env_conf.config_filename)?;
let deserialized: Vec<Technology> =
serde_yaml::from_str(&content).context("Deserialize failed")?;
Ok(deserialized)
Expand All @@ -64,8 +66,9 @@ mod tests {
// Creating a test file
let tmp_dir = "tests";
fs::create_dir(tmp_dir)?;
let fpath = "tests/tmp.yml";
let mut file = File::create(fpath)?;
let config_path = "tests/tmp.yml";
let readme_path = "content/STACK.md";
let mut file = File::create(config_path)?;
file.write_all(
b"- name: Golang
logo: go
Expand All @@ -87,10 +90,11 @@ mod tests {

// Getting config data
let file_conf = config_file(&Env {
path: Path::new(fpath).to_owned(),
config_filename: Path::new(config_path).to_owned(),
badges: true,
technology_emoji: ' ',
project_emoji: ' ',
output_file: Path::new(readme_path).to_owned(),
})?;
fs::remove_dir_all(tmp_dir)?;

Expand Down Expand Up @@ -136,10 +140,11 @@ mod tests {
assert_eq!(
env_vars()?,
Env {
path: Path::new("stack.yml").to_owned(),
config_filename: Path::new("stack.yml").to_owned(),
badges: true,
technology_emoji: '💻',
project_emoji: '🚀',
output_file: Path::new("README.md").to_owned()
}
);
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use std::process::{Command, Stdio};

use anyhow::{Context, Result};

use crate::readme;
use crate::conf::Env;

const BINARY: &str = "git";

pub fn commit_and_push() -> Result<()> {
pub fn commit_and_push(env_var_conf: &Env) -> Result<()> {
Command::new(BINARY)
.arg("config")
.arg("--global")
Expand All @@ -23,7 +23,7 @@ pub fn commit_and_push() -> Result<()> {
.context("Failed to set commit name")?;
Command::new(BINARY)
.arg("add")
.arg(readme::FILE_NAME)
.arg(&env_var_conf.output_file)
.output()
.context("Failed to stage changes")?;
Command::new(BINARY)
Expand Down
19 changes: 12 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,27 @@ fn main() {
info!("Generated table");

// Inserting table into README
let readme_content = fs::read_to_string(readme::FILE_NAME)
.expect(&format!("Failed to read from {}", readme::FILE_NAME));
let readme_content = fs::read_to_string(&env_var_conf.output_file).expect(&format!(
"Failed to read from {}",
&env_var_conf.output_file.display()
));
let patched_content = readme::insert_table(&readme_content, &table)
.expect("Failed to insert table to README data");

// Writing the changes to the README
if readme_content != patched_content {
// Writing changes
let mut readme_file =
File::create(&readme::FILE_NAME).expect("Failed to create README.md file struct");
let mut readme_file = File::create(&&env_var_conf.output_file)
.expect("Failed to create README.md file struct");
readme_file
.write_all(patched_content.as_bytes())
.expect(&format!("Failed to write changes to {}", readme::FILE_NAME));
info!("Wrote changes to {}", readme::FILE_NAME);
.expect(&format!(
"Failed to write changes to {}",
&env_var_conf.output_file.display()
));
info!("Wrote changes to {}", &env_var_conf.output_file.display());

git::commit_and_push().expect("Failed to commit and push changes");
git::commit_and_push(&env_var_conf).expect("Failed to commit and push changes");

info!("Committed changes! Have a good day :)")
} else {
Expand Down
5 changes: 2 additions & 3 deletions src/readme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use crate::conf;
use anyhow::bail;
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};

pub const FILE_NAME: &'static str = "README.md";

pub fn gen_table(
env_conf: &conf::Env,
file_conf: &Vec<conf::Technology>,
Expand Down Expand Up @@ -128,10 +126,11 @@ mod tests {
assert_eq!(
gen_table(
&Env {
path: Path::new("stack.yml").to_owned(),
config_filename: Path::new("stack.yml").to_owned(),
badges: true,
technology_emoji: '💻',
project_emoji: '🚀',
output_file: Path::new("README.md").to_owned()
},
&vec![
Technology {
Expand Down

0 comments on commit a671376

Please sign in to comment.