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

Lock subcommand #11

Merged
merged 9 commits into from
Jul 2, 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
94 changes: 84 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ path = "src/main.rs"
argh = "0.1.12"
cazan-common = "0.1.1"
serde_json = "1.0.116"
serde_ignored = "0.1.10"
image = "0.25.1"
cprint = { version = "1.0.0", features = ["ceprint"] }
crossterm = { version = "0.27.0", features = ["windows"] }
serde = { version = "1.0.203", features = ["derive"] }
semver = { version = "1.0.23", features = ["serde"] }
rand = "0.8.5"
hex = "0.4.3"
sha2 = "0.10.8"
16 changes: 5 additions & 11 deletions src/cli/init.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::cli::SubCommandTrait;
use crate::config::BasicConfig;
use crate::config::Config;
use argh::FromArgs;
use cprint::{ceprintln, cprintln};
use rand::Rng;
use semver::Version;
use std::process::ExitCode;
use std::{env, fs};
Expand All @@ -18,22 +17,17 @@ pub struct Init {
pub force: bool,
}

fn generate_salt() -> String {
let mut rng = rand::thread_rng();
let mut salt = vec![0u8; 16];
rng.fill(&mut salt[..]);
hex::encode(salt)
}

impl SubCommandTrait for Init {
fn run(&self) -> ExitCode {
let current_dir = env::current_dir().unwrap();
let dir_name = &current_dir.file_name().unwrap().to_str().unwrap();
let config = BasicConfig {
let config = Config {
name: dir_name,
version: Version::new(0, 0, 1),
authors: Vec::new(),
file_hash_salt: &generate_salt(),
use_autoplay_for_multimedia: None,
rdp_epsilon: None,
plugins: Some(vec![]),
};

let serialized_config = serde_json::to_string_pretty(&config).unwrap();
Expand Down
141 changes: 141 additions & 0 deletions src/cli/lock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
use crate::cli::SubCommandTrait;
use crate::config::{checksum, Config};
use argh::FromArgs;
use cprint::{ceprintln, cprintln};
use std::env;
use std::fs;
use std::process::ExitCode;

#[derive(PartialEq, Debug, FromArgs)]
#[argh(subcommand, name = "lock", description = "Init your Cazan project")]
pub struct Lock {
#[argh(
switch,
short = 'f',
description = "force locking the cazan.json file, without checking if the JSON is valid"
)]
pub force: bool,

#[argh(
switch,
short = 'u',
description = "allow unknown field to locked file"
)]
pub allow_unknown: bool,
}

impl SubCommandTrait for Lock {
fn run(&self) -> ExitCode {
let cazan_json = env::current_dir().unwrap().join("cazan.json");
let cazan_directory = env::current_dir().unwrap().join(".cazan");
let locked_config_json = cazan_directory.join("config.json");

if !cazan_json.exists() || !cazan_directory.exists() {
ceprintln!("Error cazan is not initialized for this directory");
return ExitCode::FAILURE;
}

let config = match fs::read_to_string(cazan_json.clone()) {
Ok(config) => config,
Err(_) => {
ceprintln!("Error reading cazan.json file");
return ExitCode::FAILURE;
}
};

let config_string = config.as_str();

let new_checksum = match checksum(&cazan_json) {
Ok(checksum) => checksum,
Err(_) => {
ceprintln!("Error calculating checksum of cazan.json");
return ExitCode::FAILURE;
}
};

let checksum_file = cazan_directory.join("checksum.txt");

let old_checksum = fs::read_to_string(checksum_file.clone()).unwrap_or_default();

if old_checksum.is_empty() && checksum_file.exists() {
ceprintln!("Error reading checksum file");
return ExitCode::FAILURE;
}

if self.force {
if fs::copy(cazan_json.clone(), locked_config_json).is_err() {
ceprintln!("Error copying cazan.json file to .cazan/config.json");
return ExitCode::FAILURE;
}

if fs::write(checksum_file, new_checksum).is_err() {
ceprintln!("Error saving checksum");
return ExitCode::FAILURE;
}

cprintln!("Locked config");
return ExitCode::SUCCESS;
}

if old_checksum == new_checksum {
cprintln!("Already up-to-date");
return ExitCode::SUCCESS;
}

let deserializer = &mut serde_json::Deserializer::from_str(config_string);
let mut unused: Vec<String> = vec![];

let config: Config = match serde_ignored::deserialize(deserializer, |field| {
unused.push(field.to_string())
}) {
Ok(config) => config,
Err(_) => {
ceprintln!("Error cazan.json is invalid");
return ExitCode::FAILURE;
}
};

if self.allow_unknown {
if fs::copy(cazan_json.clone(), locked_config_json).is_err() {
ceprintln!("Error copying cazan.json file to .cazan/config.json");
return ExitCode::FAILURE;
}

if fs::write(checksum_file, new_checksum).is_err() {
ceprintln!("Error saving checksum");
return ExitCode::FAILURE;
}

cprintln!("Locked config");
return ExitCode::SUCCESS;
}

if !unused.is_empty() {
let warning = format!(
"Warning unknown fields ({}) are ignored. To force using them, use --allow-unknown",
unused
.iter()
.map(|field| format!("`{}`", field))
.collect::<Vec<_>>()
.join(", ")
);
cprintln!(warning => Yellow);
}

let config = serde_json::to_string_pretty(&config).unwrap();

if fs::write(locked_config_json, config).is_err() {
ceprintln!("Error copying cazan.json to .cazan/config.json");
return ExitCode::FAILURE;
}

if fs::write(checksum_file, new_checksum).is_err() {
ceprintln!("Error saving checksum");
return ExitCode::FAILURE;
}

cprintln!("Locked config");

ExitCode::SUCCESS
}
}
1 change: 1 addition & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod init;
mod lock;
mod prebuild;
mod subcommands;

Expand Down
Loading
Loading