Skip to content

Commit

Permalink
run command & new log macros
Browse files Browse the repository at this point in the history
  • Loading branch information
diced committed Oct 17, 2021
1 parent da8848c commit 5711936
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 77 deletions.
4 changes: 3 additions & 1 deletion Cargo.lock

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

7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ description = "discord version manager for linux"
homepage = "https://github.com/diced/dvm"
repository = "https://github.com/diced/dvm.git"
license = "MIT"
version = "1.1.6"
version = "1.1.7"
authors = ["diced <pranaco2@gmail.com>"]
edition = "2018"
edition = "2021"

[[bin]]
name = "dvm"


# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tokio = { version = "1.6.0", features = ["full"] }
reqwest = { version = "0.11.3", features = ["json", "native-tls", "blocking"], default-features = false }
Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Allowing you to manage all of your discord versions. This was made as I was impa

# Usage
```
dvm 0.1.1
dvm 1.1.4
USAGE:
dvm <SUBCOMMAND>
Expand All @@ -14,11 +14,13 @@ FLAGS:
-V, --version Prints version information
SUBCOMMANDS:
help Prints this message or the help of the given subcommand(s)
install install the latest <type> of discord
remove remove the installed <type> of discord
show show all installed versions
update update to the latest <type> of discord
completions get shell completions
help Prints this message or the help of the given subcommand(s)
install install the latest <type> of discord
remove remove the installed <type> of discord
run run discord with specific options
show show all installed versions
update update to the latest <type> of discord
```

# Installing
Expand Down
7 changes: 3 additions & 4 deletions src/cli/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub async fn install(release_type: Type, verbose: bool) -> Res<()> {
let user = env::var("USER")?;
fs::create_dir_all(format!("/home/{}/.dvm/bin", user))?;
if verbose {
info("created .dvm dir")
info!("created .dvm dir")
}

let pascal_pkg = match release_type {
Expand All @@ -20,12 +20,11 @@ pub async fn install(release_type: Type, verbose: bool) -> Res<()> {
let exists = Path::new(&format!("/home/{}/.dvm/{}", user, pascal_pkg)).exists();

if exists {
error(format!("{} is already installed", release_type));
std::process::exit(1);
error!("{} is already installed", release_type);
}

let (latest, _) = install_version(false, release_type.clone(), verbose, user).await?;

success(format!("installed {}:{}", release_type, latest));
success!("installed {}:{}", release_type, latest);
Ok(())
}
3 changes: 2 additions & 1 deletion src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ mod install;
mod remove;
mod show;
mod update;
mod run;

pub use {install::install, remove::remove, show::show, update::update};
pub use {install::install, remove::remove, show::show, update::update, run::run};
19 changes: 9 additions & 10 deletions src/cli/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,32 @@ pub async fn remove(release_type: Type, verbose: bool) -> Res<()> {

let exists = Path::new(&format!("/home/{}/.dvm/{}", user, pascal_pkg)).exists();
if verbose {
info("checking if installation exists")
info!("checking if installation exists")
}

if !exists {
error(format!("{} not installed", release_type));
std::process::exit(1);
error!("{} not installed", release_type);
}

let version = fs::read_to_string(format!("/home/{}/.dvm/{}/version", user, pascal_pkg))
.expect("could not read version file: malformed installation detected");
if verbose {
info("reading version file")
info!("reading version file")
}

info(format!("removing version {}:{}", release_type, version));
info!("removing version {}:{}", release_type, version);

// remove all {release type} associated files
fs::remove_dir_all(format!("/home/{}/.dvm/{}", user, pascal_pkg))
.expect("error when removing data dirs");
if verbose {
info("removed data dirs")
info!("removed data dirs")
}

fs::remove_file(format!("/home/{}/.dvm/bin/{}", user, pkg_name))
.expect("error when removing bin file");
if verbose {
info("removed bin file")
info!("removed bin file")
}

fs::remove_file(format!(
Expand All @@ -58,7 +57,7 @@ pub async fn remove(release_type: Type, verbose: bool) -> Res<()> {
))
.expect("error when removing desktop file");
if verbose {
info("removed desktop file")
info!("removed desktop file")
}

fs::remove_file(format!(
Expand All @@ -67,9 +66,9 @@ pub async fn remove(release_type: Type, verbose: bool) -> Res<()> {
))
.expect("error when removing icon");
if verbose {
info("removed icon")
info!("removed icon")
}

success(format!("removed version {}:{}", release_type, version));
success!("removed version {}:{}", release_type, version);
Ok(())
}
38 changes: 38 additions & 0 deletions src/cli/run.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use std::{env, fs, path::Path};

use tokio::process::Command;

use crate::{Res, error, info, r#type::Type};

pub async fn run(release_type: Type, args: Vec<String>, verbose: bool) -> Res<()> {
// create user var & create .dvm dirs
let user = env::var("USER")?;
fs::create_dir_all(format!("/home/{}/.dvm/bin", user))?;

// create user var & create .dvm dirs
let user = env::var("USER")?;
fs::create_dir_all(format!("/home/{}/.dvm/bin", user))?;
if verbose {
info!("created .dvm dir")
}

let pascal_pkg = match release_type {
Type::STABLE => "Discord",
Type::PTB => "DiscordPTB",
Type::CANARY => "DiscordCanary",
Type::DEVELOPMENT => "DiscordDevelopment",
};

let exists = Path::new(&format!("/home/{}/.dvm/{}", user, pascal_pkg)).exists();

if !exists {
error!("{} is not installed", release_type);
}

Command::new(format!("/home/{}/.dvm/{}/{}", user, pascal_pkg, pascal_pkg))
.args(&args)
.spawn()?
.wait_with_output().await?;

Ok(())
}
9 changes: 4 additions & 5 deletions src/cli/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub async fn update(release_type: Type, verbose: bool) -> Res<()> {
let user = env::var("USER")?;
fs::create_dir_all(format!("/home/{}/.dvm/bin", user))?;
if verbose {
info("created .dvm dir")
info!("created .dvm dir")
}

let pascal_pkg = match release_type {
Expand All @@ -24,16 +24,15 @@ pub async fn update(release_type: Type, verbose: bool) -> Res<()> {
let exists = Path::new(&format!("/home/{}/.dvm/{}", user, pascal_pkg)).exists();

if !exists {
error(format!("{} is not installed", release_type));
std::process::exit(1);
error!("{} is not installed", release_type);
}

let (latest, version) = install_version(true, release_type.clone(), verbose, user).await?;

success(format!(
success!(
"updated {}:{} -> {}:{}",
release_type, version, release_type, latest
));
);

Ok(())
}
2 changes: 2 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub const SHOW_DESC: &str = "show all installed versions";
pub const SHOW_ALIASES: &[&str] = &["s", "installed", "all", "a", "versions", "types"];
pub const COMP_DESC: &str = "get shell completions";
pub const COMP_ALIASES: &[&str] = &["c", "comp"];
pub const RUN_DESC: &str = "run discord with specific options";
pub const RUN_ALIASES: &[&str] = &["r", "start", "s"];

pub const POSSIBLE_SHELLS: &[&str] = &[
"bash", "b",
Expand Down
3 changes: 1 addition & 2 deletions src/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ pub fn give_completions(shell: &str) {
"powershell" | "pwsh" | "ps" | "p" => return_bash(),
"zsh" | "z" => return_bash(),
_ => {
error(format!("shell \"{}\" is not supported", shell));
std::process::exit(1);
error!("shell \"{}\" is not supported", shell);
}
};
}
Expand Down
34 changes: 23 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(format_args_nl)]

pub mod cli;
pub mod util;
pub mod r#type;
Expand All @@ -7,25 +9,34 @@ pub mod completions;
pub type Res<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;

use clap::{App, Arg};
use colored::*;
use common::*;

pub fn info(text: impl Into<String>) {
println!("{}{}", "info: ".white().bold(), text.into());
#[macro_export]
macro_rules! info {
($($arg:tt)*) => ({
use colored::Colorize;
println!("{}{}", "info: ".white().bold(), std::format_args!($($arg)*));
})
}

pub fn success(text: impl Into<String>) {
println!(
"\n\t{}{}\n",
"success: ".green().bold(),
text.into()
);
#[macro_export]
macro_rules! success {
($($arg:tt)*) => ({
use colored::Colorize;
println!("\n\t{}{}\n", "success: ".green().bold(), std::format_args!($($arg)*));
})
}

pub fn error(text: impl Into<String>) {
println!("{}{}", "error: ".red().bold(), text.into());
#[macro_export]
macro_rules! error {
($($arg:tt)*) => ({
use colored::Colorize;
println!("{}{}", "error: ".red().bold(), std::format_args!($($arg)*));
std::process::exit(1);
})
}


pub fn build_cli() -> App<'static> {
App::new("dvm")
.version("1.1.4")
Expand Down Expand Up @@ -60,4 +71,5 @@ pub fn build_cli() -> App<'static> {
.aliases(COMP_ALIASES)
.arg(Arg::new("type").possible_values(POSSIBLE_SHELLS))
)

}
41 changes: 28 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
compile_error!("can only be compiled on linux ;)");

use clap::{AppSettings, Clap};
use dvm::{Res, cli::{install, remove, show, update}, common::*, common::VERSION, completions, error, r#type::Type};
use dvm::{Res, cli::{install, remove, show, update, run}, common::*, common::VERSION, completions, error, r#type::Type};

#[derive(Clap, Debug)]
#[clap(version = VERSION, setting = AppSettings::ColoredHelp)]
Expand All @@ -26,7 +26,10 @@ enum Command {
Show(ShowOption),

#[clap(about = COMP_DESC, aliases = COMP_ALIASES)]
Completions(CompletionsOption)
Completions(CompletionsOption),

#[clap(about = RUN_DESC, aliases = RUN_ALIASES)]
Run(RunOptions)
}

#[derive(Clap, Debug)]
Expand Down Expand Up @@ -71,15 +74,26 @@ struct CompletionsOption {
shell: String
}

#[derive(Clap, Debug)]
struct RunOptions {
#[clap(short, long)]
verbose: bool,

#[clap(possible_values = POSSIBLE_VALUES)]
r#type: String,

#[clap(last = true)]
args: Vec<String>
}

fn str_to_type(s: String) -> Type {
match s.as_str() {
"stable" | "discord-stable" | "s" => Type::STABLE,
"canary" | "discord-canary" | "c" => Type::CANARY,
"ptb" | "discord-ptb" | "p" => Type::PTB,
"development" | "dev" | "discord-development" | "d" => Type::DEVELOPMENT,
_ => {
error(format!("type \"{}\" does not exist", s));
std::process::exit(1);
error!("type \"{}\" does not exist", s);
}
}
}
Expand All @@ -88,29 +102,30 @@ fn str_to_type(s: String) -> Type {
async fn main() -> Res<()> {
let opts = Opts::parse();

match opts.command {
Ok(match opts.command {
Command::Install(opt) => {
for r#type in opt.r#type {
install(str_to_type(r#type), opt.verbose).await?;
install(str_to_type(r#type), opt.verbose).await?
}
}
Command::Update(opt) => {
for r#type in opt.r#type {
update(str_to_type(r#type), opt.verbose).await?;
update(str_to_type(r#type), opt.verbose).await?
}
}
Command::Remove(opt) => {
for r#type in opt.r#type {
remove(str_to_type(r#type), opt.verbose).await?;
remove(str_to_type(r#type), opt.verbose).await?
}
}
Command::Show(opt) => {
show(opt.verbose, opt.check).await?;
show(opt.verbose, opt.check).await?
}
Command::Completions(opt) => {
completions::give_completions(&opt.shell);
completions::give_completions(&opt.shell)
}
};

Ok(())
Command::Run(opt) => {
run(str_to_type(opt.r#type), opt.args.clone(), opt.verbose).await?
}
})
}
Loading

0 comments on commit 5711936

Please # to comment.