Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to run CLI with a recipe file instead of command line args #22

Merged
merged 3 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 50 additions & 9 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use popgetter::{
formatters::{CSVFormatter, GeoJSONFormatter, GeoJSONSeqFormatter, OutputFormatter, OutputGenerator}, Popgetter
};
use serde::{Deserialize, Serialize};
use std::{fs::File, str::FromStr};
use std::{fs::{self, File}, str::FromStr};
sgreenbury marked this conversation as resolved.
Show resolved Hide resolved
use strum_macros::EnumString;

/// Defines the output formats we are able to produce data in.
Expand Down Expand Up @@ -51,13 +51,9 @@ pub struct DataCommand {
output_file: String
}

impl RunCommand for DataCommand {
async fn run(&self) -> Result<()> {
let popgetter = Popgetter::new()?;
let data_request = DataRequestSpec::from(self);
let mut results = popgetter.get_data_request(&data_request).await?;

let formatter = match &self.output_format{
impl From<&OutputFormat> for OutputFormatter{
sgreenbury marked this conversation as resolved.
Show resolved Hide resolved
fn from(value: &OutputFormat) -> Self {
match value{
OutputFormat::GeoJSON=>{
OutputFormatter::GeoJSON(GeoJSONFormatter)
},
Expand All @@ -68,10 +64,25 @@ impl RunCommand for DataCommand {
OutputFormatter::GeoJSONSeq(GeoJSONSeqFormatter)
},
_=>todo!("output format not implemented")
};
}
}
}

impl From<OutputFormat> for OutputFormatter{
fn from(value: OutputFormat) -> Self {
Self::from(&value)
}
}

impl RunCommand for DataCommand {
async fn run(&self) -> Result<()> {
let popgetter = Popgetter::new()?;
let data_request = DataRequestSpec::from(self);
let mut results = popgetter.get_data_request(&data_request).await?;

println!("{results:#?}");
penelopeysm marked this conversation as resolved.
Show resolved Hide resolved
let mut f = File::create(&self.output_file)?;
let formatter: OutputFormatter = (&self.output_format).into();
formatter.save(&mut f,&mut results)?;

Ok(())
Expand Down Expand Up @@ -141,6 +152,33 @@ impl RunCommand for SurveysCommand {
}
}

/// The Recipe command loads a recipy file and generates the output data requested
#[derive(Args, Debug)]
pub struct RecipeCommand{
#[arg(index=1)]
recipe_file: String,
sgreenbury marked this conversation as resolved.
Show resolved Hide resolved

#[arg(short='f', long)]
sgreenbury marked this conversation as resolved.
Show resolved Hide resolved
output_format: OutputFormat,
sgreenbury marked this conversation as resolved.
Show resolved Hide resolved

#[arg(short='o',long)]
output_file: String
}

impl RunCommand for RecipeCommand{
async fn run(&self) -> Result<()> {
let popgetter = Popgetter::new()?;
let config = fs::read_to_string(&self.recipe_file)?;
let data_request: DataRequestSpec = serde_json::from_str(&config)?;
let mut results = popgetter.get_data_request(&data_request).await?;
println!("{results}");
penelopeysm marked this conversation as resolved.
Show resolved Hide resolved
let formatter: OutputFormatter = (&self.output_format).into();
let mut f = File::create(&self.output_file)?;
formatter.save(&mut f,&mut results)?;
Ok(())
}
}

/// The entrypoint for the CLI.
#[derive(Parser, Debug)]
#[command(version, about, long_about = None, name="popgetter", long_about="Popgetter is a tool to quickly get the data you need!")]
Expand All @@ -162,8 +200,11 @@ pub enum Commands {
Metrics(MetricsCommand),
/// Surveys
Surveys(SurveysCommand),
/// From recipe
Recipe(RecipeCommand)
}


#[cfg(test)]
mod tests {
use super::*;
Expand Down
20 changes: 20 additions & 0 deletions test_recipe.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"region": [
{
"BoundingBox": [
-74.251785,
40.647043,
-73.673286,
40.91014
]
}
],
"metrics": [
{
"NamedMetric": "B01001_E002"
},
{
"NamedMetric": "B01001_E001"
}
]
}
Loading