Skip to content

Commit

Permalink
cleaned up logging
Browse files Browse the repository at this point in the history
  • Loading branch information
blake-mealey committed Nov 9, 2021
1 parent 9f97ef9 commit c6549f6
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 62 deletions.
118 changes: 65 additions & 53 deletions src/commands/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use std::{
use yansi::Paint;

use crate::{
config::load_config_file,
config::{load_config_file, Config, DeploymentConfig},
logger::logger,
resource_manager::RobloxResourceManager,
resources::{EvaluateResults, ResourceGraph, ResourceManager},
state::{get_desired_graph, get_previous_state, save_state},
state::{get_desired_graph, get_previous_state, save_state, ResourceState},
};

fn run_command(command: &str) -> std::io::Result<std::process::Output> {
Expand Down Expand Up @@ -65,43 +65,32 @@ fn parse_project(project: Option<&str>) -> Result<(PathBuf, PathBuf), String> {
} else if project_path.is_file() {
(project_path.parent().unwrap().into(), project_path)
} else {
return Err(format!("Unable to parse project path: {}", project));
return Err(format!("Unable to load project path: {}", project));
};

if config_file.exists() {
return Ok((project_dir, config_file));
}

Err(format!(
"Config file does not exist: {}",
config_file.display()
))
Err(format!("Config file {} not found", config_file.display()))
}

pub async fn run(project: Option<&str>) -> i32 {
let (project_path, config_file) = match parse_project(project) {
Ok(v) => v,
Err(e) => {
logger::log_error(e);
return 1;
}
};
struct Project {
project_path: PathBuf,
next_graph: ResourceGraph,
previous_graph: ResourceGraph,
state: ResourceState,
deployment_config: DeploymentConfig,
config: Config,
}

let config = match load_config_file(&config_file) {
Ok(v) => v,
Err(e) => {
logger::log_error(e);
return 1;
}
};
async fn load_project(project: Option<&str>) -> Result<Option<Project>, String> {
let (project_path, config_file) = parse_project(project)?;

let current_branch = match get_current_branch() {
Ok(v) => v,
Err(e) => {
logger::log_error(e);
return 1;
}
};
let config = load_config_file(&config_file)?;
logger::log(format!("Loaded config file {}", config_file.display()));

let current_branch = get_current_branch()?;

let deployment_config = config
.deployments
Expand All @@ -111,39 +100,62 @@ pub async fn run(project: Option<&str>) -> i32 {
let deployment_config = match deployment_config {
Some(v) => v,
None => {
logger::log("No deployment configuration found for branch; no deployment necessary.");
return 0;
logger::log(format!(
"No deployment configuration found for branch '{}'",
current_branch
));
return Ok(None);
}
};

// Get our resource manager
let mut resource_manager =
ResourceManager::new(Box::new(RobloxResourceManager::new(&project_path)));
logger::log(format!(
"Found deployment configuration '{}' for branch '{}'",
deployment_config.name, current_branch
));

// Get previous state
let mut state =
match get_previous_state(project_path.as_path(), &config, deployment_config).await {
Ok(v) => v,
Err(e) => {
logger::log_error(e);
return 1;
}
};
let state = get_previous_state(project_path.as_path(), &config, deployment_config).await?;

// Get our resource graphs
let previous_graph =
ResourceGraph::new(state.deployments.get(&deployment_config.name).unwrap());
let mut next_graph = match get_desired_graph(project_path.as_path(), &config, deployment_config)
{
Ok(v) => v,
let next_graph = get_desired_graph(project_path.as_path(), &config, deployment_config)?;

Ok(Some(Project {
project_path,
next_graph,
previous_graph,
state,
deployment_config: deployment_config.clone(),
config,
}))
}

pub async fn run(project: Option<&str>) -> i32 {
logger::start_action("Loading project:");
let Project {
project_path,
config,
deployment_config,
mut next_graph,
previous_graph,
mut state,
} = match load_project(project).await {
Ok(Some(v)) => v,
Ok(None) => {
logger::end_action("No deployment necessary");
return 0;
}
Err(e) => {
logger::log_error(e);
logger::end_action(Paint::red(e));
return 1;
}
};
logger::end_action("Succeeded");

// Evaluate the resource graph
logger::start_action("Evaluating resource graph:");
let mut resource_manager =
ResourceManager::new(Box::new(RobloxResourceManager::new(&project_path)));

logger::start_action("Deploying resources:");
let exit_code = match next_graph.evaluate(&previous_graph, &mut resource_manager) {
Ok(results) => {
match results {
Expand All @@ -152,7 +164,7 @@ pub async fn run(project: Option<&str>) -> i32 {
updated_count: 0,
deleted_count: 0,
..
} => logger::end_action("Succeeded with no changes required"),
} => logger::end_action("No changes required"),
EvaluateResults {
created_count,
updated_count,
Expand All @@ -171,19 +183,19 @@ pub async fn run(project: Option<&str>) -> i32 {
}
};

// Save the results to the state file
logger::start_action("Saving state:");
state.deployments.insert(
deployment_config.name.clone(),
next_graph.get_resource_list(),
);
match save_state(&project_path, &config.state, &state).await {
Ok(_) => {}
Err(e) => {
logger::log_error(e);
logger::end_action(Paint::red(e));
return 1;
}
};
logger::end_action("Succeeded");

// If there were errors, return them
exit_code
}
25 changes: 16 additions & 9 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use tokio::io::AsyncReadExt;

use crate::{
config::{Config, DeploymentConfig, PlayabilityConfig, RemoteStateConfig, StateConfig},
logger::logger,
resource_manager::{resource_types, AssetId, SINGLETON_RESOURCE_ID},
resources::{InputRef, Resource, ResourceGraph},
roblox_api::{ExperienceConfigurationModel, PlaceConfigurationModel},
Expand Down Expand Up @@ -48,10 +49,10 @@ fn parse_state(file_name: &str, data: &str) -> Result<ResourceState, String> {

fn get_state_from_file(project_path: &Path) -> Result<Option<ResourceState>, String> {
let state_file_path = get_state_file_path(project_path);
println!(
"Loading previous state from local file: {}\n",
logger::log(format!(
"Loading previous state from local file: {}",
state_file_path.display()
);
));

if state_file_path.exists() {
let data = fs::read_to_string(&state_file_path).map_err(|e| {
Expand All @@ -74,7 +75,10 @@ fn get_state_from_file(project_path: &Path) -> Result<Option<ResourceState>, Str
async fn get_state_from_remote(
config: &RemoteStateConfig,
) -> Result<Option<ResourceState>, String> {
println!("Loading previous state from remote object: {}\n", config);
logger::log(format!(
"Loading previous state from remote object: {}",
config
));

let client = S3Client::new(config.region.clone());
let object_res = client
Expand Down Expand Up @@ -167,10 +171,10 @@ pub async fn get_previous_state(
};

if state.deployments.get(&deployment_config.name).is_none() {
println!(
"No previous state for deployment {}.",
logger::log(format!(
"No previous state for deployment {}",
deployment_config.name
);
));
state.deployments.insert(
deployment_config.name.clone(),
get_default_resources(config, deployment_config)?,
Expand Down Expand Up @@ -288,7 +292,7 @@ pub fn get_desired_graph(
}

pub async fn save_state_to_remote(config: &RemoteStateConfig, data: &[u8]) -> Result<(), String> {
println!("\nSaving state to remote object: {}", config);
logger::log(format!("Saving to remote object: {}", config));

let client = S3Client::new(config.region.clone());
let res = client
Expand All @@ -307,7 +311,10 @@ pub async fn save_state_to_remote(config: &RemoteStateConfig, data: &[u8]) -> Re
pub fn save_state_to_file(project_path: &Path, data: &[u8]) -> Result<(), String> {
let state_file_path = get_state_file_path(project_path);

println!("\nSaving state to local file. It is recommended you commit this file to your source control: {}", state_file_path.display());
logger::log(format!(
"Saving to local file: {}. It is recommended you commit this file to your source control",
state_file_path.display()
));

fs::write(&state_file_path, data).map_err(|e| {
format!(
Expand Down

0 comments on commit c6549f6

Please sign in to comment.