Skip to content

Commit

Permalink
Introduce Context struct
Browse files Browse the repository at this point in the history
  • Loading branch information
riseshia committed Jun 24, 2024
1 parent 01053ae commit 0d366ba
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/commands/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ use std::process::exit;

use serde_json::Value;

use crate::context::Context;

use super::plan::PlanCommand;

pub struct ApplyCommand;

impl ApplyCommand {
pub async fn run(force: &bool, config: &Value) {
pub async fn run(context: &Context, force: &bool, config: &Value) {
if !force {
PlanCommand::run(config).await;
PlanCommand::run(context, config).await;

print!(
r#"
Expand Down
9 changes: 8 additions & 1 deletion src/commands/export.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
use crate::context::Context;

pub struct ExportCommand;

impl ExportCommand {
pub async fn run(config: &str, sfn_arn: &str, schedule_arn: &Option<String>) {
pub async fn run(
_context: &Context,
config: &str,
sfn_arn: &str,
schedule_arn: &Option<String>,
) {
println!("export called with");
println!("- export path: {}", config);
println!("- target sfn arn: {}", sfn_arn);
Expand Down
4 changes: 3 additions & 1 deletion src/commands/plan.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use serde_json::Value;

use crate::context::Context;

pub struct PlanCommand;

impl PlanCommand {
pub async fn run(config: &Value) {
pub async fn run(_context: &Context, config: &Value) {
// read config
// fetch resource from aws api
// diff
Expand Down
44 changes: 44 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::scheduler_client::Scheduler;
use crate::sfn_client::Sfn;

pub struct Context {
pub sfn_client: Sfn,
pub scheduler_client: Scheduler,
}

impl Context {
pub fn new(sfn_client: Sfn, scheduler_client: Scheduler) -> Self {
Self {
sfn_client,
scheduler_client,
}
}

#[cfg(not(test))]
pub async fn async_default() -> Self {
use aws_config::BehaviorVersion;

use crate::{scheduler_client, sfn_client};

let aws_config = aws_config::load_defaults(BehaviorVersion::latest()).await;
let sfn_client = sfn_client::Sfn::new(aws_sdk_sfn::Client::new(&aws_config));
let scheduler_client =
scheduler_client::Scheduler::new(aws_sdk_scheduler::Client::new(&aws_config));

Self {
sfn_client,
scheduler_client,
}
}

#[cfg(test)]
pub async fn async_default() -> Self {
use crate::scheduler_client::MockSchedulerImpl;
use crate::sfn_client::MockSfnImpl;

Self {
sfn_client: MockSfnImpl::default(),
scheduler_client: MockSchedulerImpl::default(),
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod cli;
pub mod commands;
pub mod context;
pub mod jsonnet_evaluator;
pub mod scheduler_client;
pub mod sfn_client;
11 changes: 8 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use fubura::cli::{Cli, Commands};
use fubura::commands::apply::ApplyCommand;
use fubura::commands::export::ExportCommand;
use fubura::commands::plan::PlanCommand;
use fubura::context::Context;
use fubura::jsonnet_evaluator;

#[tokio::main]
Expand All @@ -17,20 +18,24 @@ async fn main() {
ext_str,
} => {
let config = jsonnet_evaluator::eval(config, ext_str).unwrap();
let context = Context::async_default().await;

ApplyCommand::run(force, &config).await;
ApplyCommand::run(&context, force, &config).await;
}
Commands::Plan { config, ext_str } => {
let config = jsonnet_evaluator::eval(config, ext_str).unwrap();
let context = Context::async_default().await;

PlanCommand::run(&config).await;
PlanCommand::run(&context, &config).await;
}
Commands::Export {
config,
sfn_arn,
schedule_arn,
} => {
ExportCommand::run(config, sfn_arn, schedule_arn).await;
let context = Context::async_default().await;

ExportCommand::run(&context, config, sfn_arn, schedule_arn).await;
}
}
}

0 comments on commit 0d366ba

Please sign in to comment.