Skip to content

Commit

Permalink
add state
Browse files Browse the repository at this point in the history
  • Loading branch information
orelvis15 committed Jun 25, 2022
1 parent c4a3596 commit 762bd87
Show file tree
Hide file tree
Showing 38 changed files with 273 additions and 48 deletions.
79 changes: 79 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ file_diff = "1.0.0"
crossterm = "0.23.2"
lazy_static = "1.4.0"
fs_extra = "1.2.0"
faccess = "0.2.4"
faccess = "0.2.4"
sha256 = "1.0.3"
3 changes: 2 additions & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod config;
pub mod remote_config;
pub mod state_config;
7 changes: 4 additions & 3 deletions src/config/config.rs → src/config/remote_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use crate::{Message, url_build};
use crate::utils::download_manager::download_in_path;

const CONFIG_URL: &str = "https://raw.githubusercontent.com/orelvis15/cvm_config/master/config.toml";
const FILE_NAME: &str = "config.tom";
const FILE_NAME: &str = "config_remote.tom";
const PROJECT_FOLDER: &str = ".cvm";

pub fn get_config() -> Result<Config, Message> {
pub fn get_remote_config() -> Result<Config, Message> {
let home_dir = get_home_dir()?;
let project_folder = url_build(vec![&home_dir, &PROJECT_FOLDER.to_string()], false);
let file_path = download_in_path(&CONFIG_URL.to_string(), project_folder, FILE_NAME.to_string())?;
Expand Down Expand Up @@ -47,7 +47,8 @@ pub struct Config {

#[derive(Deserialize, Debug, Clone)]
pub struct Binaries {
pub files: Vec<String>,
pub required_files: Vec<String>,
pub others_files: Vec<String>,
}

#[derive(Deserialize, Debug, Clone)]
Expand Down
92 changes: 92 additions & 0 deletions src/config/state_config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#![allow(dead_code, unused_variables)]

use serde::{Deserialize, Serialize};
use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use crate::{Message, Success, url_build};
use crate::config::remote_config::get_home_dir;

const FILE_NAME: &str = "state.tom";
const PROJECT_FOLDER: &str = ".cvm";

pub fn get_state() -> Result<State, Message> {
let home_dir = get_home_dir()?;
let file_path = url_build(vec![&home_dir, &PROJECT_FOLDER.to_string(), &FILE_NAME.to_string()], false);

if !Path::new(file_path.as_str()).exists() {
create_state_file()?;
};

let file = fs::read_to_string(file_path)?;
let parse_file = toml::from_str(&file)?;
Ok(parse_file)
}

pub fn set_state(state: State) -> Result<Success, Message> {
let home_dir = get_home_dir()?;
let file_path = url_build(vec![&home_dir, &PROJECT_FOLDER.to_string(), &FILE_NAME.to_string()], false);
let mut file = File::options().truncate(true).write(true).open(file_path)?;
let toml_str = toml::to_string(&state).unwrap();
file.write_all(toml_str.as_bytes())?;
Ok(Success {})
}

pub fn add_init_file(path: String) -> Result<Success, Message> {
let mut state = get_state()?;

let file_path = Path::new(&path);
let hash = sha256::digest_file(file_path)?;
state.init.files_item.push(ConfigFiles{ name: file_path.file_name().unwrap().to_str().unwrap().to_string(), hash });

set_state(state)?;
Ok(Success{})
}

pub fn set_init_success(value: bool) -> Result<Success, Message> {
let mut state = get_state()?;
state.init.success = value;
set_state(state)?;
Ok(Success{})
}

pub fn set_version_use(version: String) -> Result<Success, Message> {
let mut state = get_state()?;
state.r#use = Use{ version };
set_state(state)?;
Ok(Success{})
}

fn create_state_file() -> Result<Success, Message> {
let home_dir = get_home_dir()?;
let file_path = url_build(vec![&home_dir, &PROJECT_FOLDER.to_string(), &FILE_NAME.to_string()], false);
let state = State { init: Init { success: false, files_item: vec![] }, r#use: Use { version: "".to_string() } };
let toml_str = toml::to_string(&state).unwrap();
let mut file = File::create(Path::new(file_path.as_str()))?;
file.write_all(toml_str.as_bytes())?;
Ok(Success {})
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct State {
pub init: Init,
pub r#use: Use,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Use {
pub version: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Init {
pub success: bool,
pub files_item: Vec<ConfigFiles>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ConfigFiles {
pub name: String,
pub hash: String,
}
2 changes: 2 additions & 0 deletions src/error/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub enum Message {
VersionInstaller(Error),
VersionBadFormed(Error),
CheckCardanoVersion(Error),
UseVersion(Error),

//IO Errors
FileNotFound(Error),
Expand Down Expand Up @@ -107,6 +108,7 @@ impl Message {
Message::PermissionDenied(this) => { &this }
Message::SettingPermission(this) => { &this }
Message::Libsodium(this) => { &this }
Message::UseVersion(this) => { &this }
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ mod term;
const VERSION: &str = env!("CARGO_PKG_VERSION");

fn main() {
let config = config::config::get_config();
let config = config::remote_config::get_remote_config();

if let Err(error) = &config {
error.print();
Expand Down
2 changes: 1 addition & 1 deletion src/subcommands/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::{Display, Formatter};
use clap::{Arg, ArgMatches, Command};
use crate::config::config::CommandItem;
use crate::config::remote_config::CommandItem;
use crate::{Message, EmptyTask, Error, Success};

pub fn command_config() -> ArgMatches {
Expand Down
2 changes: 1 addition & 1 deletion src/subcommands/subcommand.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(dead_code, unused_variables)]

use clap::ArgMatches;
use crate::config::config::Config;
use crate::config::remote_config::Config;
use crate::{Message, Success, Term};

pub trait Command {
Expand Down
2 changes: 1 addition & 1 deletion src/subcommands/subcommands_impl/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use clap::{ArgMatches};
use crate::{Command, Message, Success, Term};
use crate::config::config::{Config, get_home_dir};
use crate::config::remote_config::{Config, get_home_dir};
use crate::task::task_impl::commons::folder_manager_task::{FolderManagerAction, FolderManagerTask};
use crate::task::task_impl::commons::run_command_task::{Cmd, RunCommandInputData, RunCommandTask};
use crate::task_manager::task_manager::TaskManager;
Expand Down
8 changes: 6 additions & 2 deletions src/subcommands/subcommands_impl/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
use clap::ArgMatches;
use crate::subcommands::subcommand::Command;
use crate::subcommands::config::Args;
use crate::config::config::Config;
use crate::config::remote_config::Config;
use crate::config::state_config::set_init_success;
use crate::error::message::Message;
use crate::task::task::Success;
use crate::task::task_impl::commons::permission_task::{PermissionAction, PermissionTask};
Expand Down Expand Up @@ -34,12 +35,15 @@ impl Command for Init {
}
None => {}
}

TaskManager{}.start(vec![
Box::new(PermissionTask { input_data: PermissionAction::CheckWrite(vec![Folder::project_folder().to_string()]) }),
Box::new(InstallDependencesTask {}),
Box::new(InstallHanskellGhcTask {}),
Box::new(CreateFolderStructure {}),
Box::new(DownloadConfigFilesTask { network: network.to_string() }),
], config, term, L1)
], config, term, L1)?;

set_init_success(true)
}
}
2 changes: 1 addition & 1 deletion src/subcommands/subcommands_impl/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use clap::ArgMatches;
use crate::subcommands::config::{Args};
use crate::task::task::Success;
use crate::utils::version_utils::{get_last_tag, LATEST, verify_version};
use crate::config::config::Config;
use crate::config::remote_config::Config;
use crate::{Message, Command, Term};
use crate::task::task_impl::install::build_cardano_node_task::BuildCardanoNodeTask;
use crate::task_manager::task_manager::TaskManager;
Expand Down
7 changes: 4 additions & 3 deletions src/subcommands/subcommands_impl/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use fs_extra::dir::get_size;
use owo_colors::OwoColorize;
use walkdir::WalkDir;
use crate::{Command, Message, Success, Term};
use crate::utils::version_utils::{read_version, verify_version};
use crate::config::config::Config;
use crate::utils::version_utils::verify_version;
use crate::config::remote_config::Config;
use crate::config::state_config::get_state;
use crate::utils::folders::Folder;

pub struct List{}
Expand All @@ -17,7 +18,7 @@ impl Command for List{
let bin_folder = Folder::get_path(Folder::BIN, &config);
let current_folder = Folder::get_path(Folder::CURRENT, &config);

let current_version = read_version(&current_folder);
let current_version = get_state()?.r#use.version;

for entry in WalkDir::new(bin_folder) {
let entry = entry.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/subcommands/subcommands_impl/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use clap::{ArgMatches};
use crate::{Command, Message, Success, Term, url_build};
use crate::subcommands::config::Args;
use crate::utils::version_utils::{get_last_tag, LATEST, read_version, verify_version};
use crate::config::config::Config;
use crate::config::remote_config::Config;
use crate::task::task::Task;
use crate::task::task_impl::commons::folder_manager_task::{FolderManagerAction, FolderManagerTask};
use crate::task::task_impl::r#use::service_manager_task::{ServicesAction, ServicesManagerTask};
Expand Down
Loading

0 comments on commit 762bd87

Please sign in to comment.