Skip to content

Commit

Permalink
revert(json): remove json because it was really bad
Browse files Browse the repository at this point in the history
  • Loading branch information
Thumuss committed Jun 4, 2024
1 parent 0449ef1 commit cc0b231
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 246 deletions.
4 changes: 0 additions & 4 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,4 @@ pub enum Commands {
pub struct Cli {
#[command(subcommand)]
pub command: Commands,

/// Output everything into a json format. Available on every commands.
#[arg(short = 'j', long, global = true)]
pub json: bool,
}
24 changes: 8 additions & 16 deletions src/commands/bulk_delete.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use serde_json::json;

use crate::utils::state::{Error, ResponseKind::*, Responses, Result};
use crate::utils::state::{Error, Result};

use super::{unlink, BulkDeleteArgs, UnlinkArgs};

pub fn run(cmd: &BulkDeleteArgs, res: &mut Responses) -> Result<bool> {
pub fn run(cmd: &BulkDeleteArgs) -> Result<bool> {
let mut vec: Vec<Error> = Vec::new();
for name in &cmd.names {
let name_and_version = name
Expand All @@ -22,23 +20,17 @@ pub fn run(cmd: &BulkDeleteArgs, res: &mut Responses) -> Result<bool> {
None
},
};
match unlink::run(&ulnk, res) {
match unlink::run(&ulnk) {
Ok(_) => (),
Err(err) => {
vec.push(err);
}
};
}
res.pushs(vec![
Value(json!({
"success": cmd.names.len() - vec.len(),
"failed": vec.len(),
})),
Message(format!(
"{}/{} successful",
cmd.names.len() - vec.len(),
cmd.names.len()
)),
]);
println!(
"{}/{} successful",
cmd.names.len() - vec.len(),
cmd.names.len()
);
Ok(true)
}
13 changes: 6 additions & 7 deletions src/commands/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ use semver::Version;
use crate::utils::{
paths::{check_path_file, get_current_dir},
specs::{Extra, Package, TypstConfig},
state::{ResponseKind::*, Responses, Result},
state::Result,
};

use super::CreateArgs;

pub fn run(cmd: &mut CreateArgs, res: &mut Responses) -> Result<bool> {
pub fn run(cmd: &mut CreateArgs) -> Result<bool> {
let curr = get_current_dir()?;
let typ = curr.clone() + "/typst.toml";

Expand All @@ -41,16 +41,15 @@ pub fn run(cmd: &mut CreateArgs, res: &mut Responses) -> Result<bool> {
//let mut tmpl: Template = Template::new(cmd.template, entrypoint, thumbnail)

if check_path_file(&typ) && !cmd.force {
res.push(Message("Nothing to do".into()));
return Ok(false);
}

if cmd.force {
res.push(Message(format!(
println!(
"{} {}",
"WARNING:".bold().yellow(),
"--force is a dangerous flag, use it cautiously".bold()
)));
);
}

if !cmd.cli {
Expand Down Expand Up @@ -221,9 +220,9 @@ pub fn run(cmd: &mut CreateArgs, res: &mut Responses) -> Result<bool> {
}

TypstConfig::new(pkg, Some(extra), None).write(&typ); //todo: add template // typst.toml
res.push(Message(format!(
println!(
"{}",
"File created to {typ}".bold().to_string()
)));
);
Ok(true)
}
12 changes: 6 additions & 6 deletions src/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ use crate::{
get_ssh_dir,
},
specs::{Extra, TypstConfig},
state::{Error, ErrorKind, Responses, Result},
state::{Error, ErrorKind, Result},
},
};
use git2::{build::RepoBuilder, Cred, FetchOptions, RemoteCallbacks, Repository};
use owo_colors::OwoColorize;

use super::{link, InstallArgs};

pub fn run(cmd: &InstallArgs, res: &mut Responses) -> Result<bool> {
pub fn run(cmd: &InstallArgs) -> Result<bool> {
let path = format!("{}/tmp", datalocalutpm());
if check_path_dir(&path) {
fs::remove_dir_all(path)?;
}
init(cmd, res, 0)?;
init(cmd, 0)?;
Ok(true)
}

pub fn init(cmd: &InstallArgs, res: &mut Responses, i: usize) -> Result<bool> {
pub fn init(cmd: &InstallArgs, i: usize) -> Result<bool> {
let path = if cmd.url.is_none() {
get_current_dir()?
} else {
Expand Down Expand Up @@ -106,7 +106,7 @@ pub fn init(cmd: &InstallArgs, res: &mut Responses, i: usize) -> Result<bool> {
force: cmd.force,
url: Some(a.to_string()),
};
init(&ins, res, i * vec_depend.len() + y)?;
init(&ins, i * vec_depend.len() + y)?;
Ok(true)
})
.collect::<Vec<Result<bool>>>();
Expand All @@ -123,7 +123,7 @@ pub fn init(cmd: &InstallArgs, res: &mut Responses, i: usize) -> Result<bool> {
no_copy: false,
};

link::run(&lnk, Some(path.clone()), res)?; //TODO: change here too
link::run(&lnk, Some(path.clone()))?; //TODO: change here too
fs::remove_dir_all(&path)?;
println!(
"{}",
Expand Down
16 changes: 5 additions & 11 deletions src/commands/link.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use owo_colors::OwoColorize;
use serde_json::json;
use std::fs;

use crate::utils::{
copy_dir_all,
paths::{check_path_dir, d_packages, get_current_dir},
state::{Error, ErrorKind, ResponseKind::*, Responses, Result},
state::{Error, ErrorKind, Result},
symlink_all, specs::{Extra, TypstConfig},
};

use super::LinkArgs;

pub fn run(cmd: &LinkArgs, path: Option<String>, res: &mut Responses) -> Result<bool> {
pub fn run(cmd: &LinkArgs, path: Option<String>) -> Result<bool> {
let curr = path.unwrap_or(get_current_dir()?);

let config = TypstConfig::load(&(curr.clone() + "/typst.toml"));
Expand Down Expand Up @@ -42,22 +41,17 @@ pub fn run(cmd: &LinkArgs, path: Option<String>, res: &mut Responses) -> Result<

if cmd.no_copy {
symlink_all(&curr, &path)?;
let s = format!(
println!(
"Project linked to: {} \nTry importing with:\n #import \"@{}/{}:{}\": *",
path, namespace, name, version
);
res.push(Value(json!({
"message": s,
})));

} else {
copy_dir_all(&curr, &path)?;
let s = format!(
println!(
"Project copied to: {} \nTry importing with:\n #import \"@{}/{}:{}\": *",
path, namespace, name, version
);
res.push(Value(json!({
"message": s,
})));
}
Ok(true)
}
13 changes: 3 additions & 10 deletions src/commands/package_path.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
use serde_json::json;

use crate::utils::{
paths::d_packages,
state::{ResponseKind::*, Responses, Result},
state::Result,
};

pub fn run(res: &mut Responses) -> Result<bool> {
res.pushs(vec![
Value(json!({
"path": d_packages(),
})),
Message(format!("Packages are located at: '{}'", d_packages())),
]);
pub fn run() -> Result<bool> {
println!("Packages are located at: '{}'", d_packages());
Ok(true)
}
48 changes: 7 additions & 41 deletions src/commands/tree.rs
Original file line number Diff line number Diff line change
@@ -1,65 +1,31 @@
use owo_colors::OwoColorize;
use serde_json::{json, Map, Value};
use std::fs;

use crate::utils::{
paths::d_packages,
state::{ResponseKind::*, Responses, Result},
state::Result,
};

pub fn run(res: &mut Responses) -> Result<bool> {
pub fn run() -> Result<bool> {
let typ = d_packages();
if !res.json {
println!("{}", "Tree listing of your packages\n".bold())
};
println!("{}", "Tree listing of your packages\n".bold());
let dirs = fs::read_dir(&typ)?;

let mut data: Vec<Value> = vec![];

for dir_res in dirs {
let dir = dir_res?;
if !res.json {
println!("@{}:", dir.file_name().to_str().unwrap().green().bold());
}
println!("@{}:", dir.file_name().to_str().unwrap().green().bold());
let subupdirs = fs::read_dir(dir.path())?;

let mut map = Map::new();
let mut list: Vec<Value> = vec![];

for dir_res in subupdirs {
let dir = dir_res?;
if !res.json {
println!(" {}:", dir.file_name().to_str().unwrap().green().bold());
}
println!(" {}:", dir.file_name().to_str().unwrap().green().bold());

let subdirs = fs::read_dir(dir.path())?;

let mut map2 = Map::new();
let mut list2: Vec<Value> = vec![];

for sub_dir_res in subdirs {
let subdir = sub_dir_res?;
list2.push(json!(subdir.file_name().to_str()));
if !res.json {
println!(" - {}", subdir.file_name().to_str().unwrap().green());
}
println!(" - {}", subdir.file_name().to_str().unwrap().green());
}

let array2 = Value::Array(list2);
map2.insert(dir.file_name().to_str().unwrap().into(), array2);
let dir_dataa: Value = Value::Object(map2);

list.push(dir_dataa)
}
let array = Value::Array(list);
map.insert(dir.file_name().to_str().unwrap().into(), array);
let dir_data: Value = Value::Object(map);

data.push(dir_data);
}
// TODO: It's working for now but it will be changed one day
if res.json {
res.push(Value(json!(data)))
};
}
Ok(true)
}
15 changes: 3 additions & 12 deletions src/commands/unlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use std::fs;

use crate::utils::{
paths::d_packages,
state::{Error, ErrorKind, ResponseKind::*, Responses, Result},
state::{Error, ErrorKind, Result},
};

use super::UnlinkArgs;

pub fn run(cmd: &UnlinkArgs, res: &mut Responses) -> Result<bool> {
pub fn run(cmd: &UnlinkArgs) -> Result<bool> {
let mut new_namespace = String::from("local");
if let Some(nspace) = &cmd.namespace {
new_namespace = nspace.to_owned();
Expand All @@ -35,7 +35,6 @@ pub fn run(cmd: &UnlinkArgs, res: &mut Responses) -> Result<bool> {

let bool = ans?;
if !bool {
res.push(Message("Nothing to do".into()));
return Ok(false);
}

Expand All @@ -62,8 +61,6 @@ pub fn run(cmd: &UnlinkArgs, res: &mut Responses) -> Result<bool> {

let bool = ans?;
if !bool {
res.push(Message("Nothing to do".into()));

return Ok(false);
}

Expand All @@ -79,18 +76,12 @@ pub fn run(cmd: &UnlinkArgs, res: &mut Responses) -> Result<bool> {

let bool = ans?;
if !bool {
res.push(Message("Nothing to do".into()));

return Ok(false);
}

fs::remove_dir_all(d_packages() + format!("/{}/{}", new_namespace, nm).as_str())?;
}
if res.json {
res.push(Message(format!("{}", "Removed!".bold())));
} else {
println!("{}", "Removed!".bold())
}
println!("{}", "Removed!".bold());

Ok(true)
}
35 changes: 10 additions & 25 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,18 @@ use commands::{
bulk_delete, create, install, link, tree, package_path, unlink, Cli, Commands,
};

use utils::state::{Error, Responses};
use utils::state::Error;

fn main() {
let x = Cli::parse();
let json = x.json;
let mut res = Responses::new(json);
let result: Result<bool, Error> = match &x.command {
Commands::Create(cmd) => create::run(&mut cmd.clone(), &mut res),
Commands::Link(cmd) => link::run(cmd, None, &mut res),
Commands::Tree => tree::run(&mut res),
Commands::PackagesPath => package_path::run(&mut res),
Commands::Unlink(cmd) => unlink::run(cmd, &mut res),
Commands::BulkDelete(cmd) => bulk_delete::run(cmd, &mut res),
Commands::Install(cmd) => install::run(cmd, &mut res),

let _: Result<bool, Error> = match &x.command {
Commands::Create(cmd) => create::run(&mut cmd.clone()),
Commands::Link(cmd) => link::run(cmd, None),
Commands::Tree => tree::run(),
Commands::PackagesPath => package_path::run(),
Commands::Unlink(cmd) => unlink::run(cmd),
Commands::BulkDelete(cmd) => bulk_delete::run(cmd),
Commands::Install(cmd) => install::run(cmd),
};
match result {
Ok(_) => {
if json {
print!("{}", res.to_json())
}
}
Err(val) => {
if json {
eprint!("{}", val.json())
} else {
eprint!("{}", val.to_str())
}
}
}
}
5 changes: 1 addition & 4 deletions src/portable.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::env;

use commands::InstallArgs;
use utils::state::Responses;

#[allow(unused)]
mod commands;
Expand All @@ -13,10 +12,8 @@ mod utils;
fn main() {
let args: Vec<String> = env::args().collect();
let force = args.contains(&"--force".to_string()) || args.contains(&"-f".to_string());
let json = args.contains(&"--json".to_string()) || args.contains(&"-j".to_string());
let mut res = Responses::new(json);
let install = InstallArgs { url: None, force };
match commands::install::run(&install, &mut res) {
match commands::install::run(&install) {
Err(err) => println!("{}", err.to_string()),
Ok(_) => println!("Everything is good to go!"),
}
Expand Down
Loading

0 comments on commit cc0b231

Please sign in to comment.