Skip to content

Commit

Permalink
feat: update command
Browse files Browse the repository at this point in the history
  • Loading branch information
megatank58 committed Oct 7, 2022
1 parent 5f18aeb commit e8784d0
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 34 deletions.
1 change: 1 addition & 0 deletions .version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.5.0
32 changes: 16 additions & 16 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "oxup"
version = "2.2.3"
version = "2.5.0"
edition = "2021"

[profile.release]
Expand Down
30 changes: 20 additions & 10 deletions src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,29 @@ pub struct ReleaseData {
assets: Vec<Release>,
}

pub async fn install(os: OS) -> Result<(), Box<dyn std::error::Error>> {
pub async fn install(os: OS, oxup: bool) -> Result<(), Box<dyn std::error::Error>> {
let mut headers = HeaderMap::new();
headers.insert(USER_AGENT, "megatank58".parse().unwrap());

let target = "https://api.github.com/repositories/500013933/releases/latest";
let target = if oxup {
"https://api.github.com/repos/oxidic/oxup/releases/latest"
} else {
"https://api.github.com/repositories/500013933/releases/latest"
};
let client = Client::new();
let response = client.get(target).headers(headers.clone()).send().await?;
let result: ReleaseData = response.json().await?;

let filter = match os {
OS::Mac => "oxido-darwin",
OS::Linux => "oxido",
OS::Windows => "oxido.exe",
let bin = if oxup {
"oxup"
} else {
"oxido"
};

let filter = &match os {
OS::Mac => bin.to_owned() + "darwin",
OS::Linux => bin.to_owned(),
OS::Windows => bin.to_owned() + ".exe",
};

let url: String = result
Expand All @@ -39,7 +49,7 @@ pub async fn install(os: OS) -> Result<(), Box<dyn std::error::Error>> {
.map(|f| f.browser_download_url.clone())
.collect();

info!["Downloading oxido"];
info![format!("Downloading {bin}")];

let response = client.get(&url).headers(headers.clone()).send().await?;
let bytes = response.bytes().await?;
Expand All @@ -48,15 +58,15 @@ pub async fn install(os: OS) -> Result<(), Box<dyn std::error::Error>> {

std::fs::write(
match os {
OS::Windows => String::from(r"C:\bin"),
OS::Windows => format!(r"C:\bin\{bin}.exe"),
_ => {
format!("{}/.oxido/bin", std::env::var("HOME").unwrap())
format!("{}/.oxido/bin/{bin}", std::env::var("HOME").unwrap())
}
},
bytes,
)?;

success!["Oxido has been installed!"];
success![format!("{bin} has been installed!")];

Ok(())
}
37 changes: 30 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ mod install;
mod os;
mod setup;
mod uninstall;
mod update;

use crate::os::OS;
use clap::{command, Parser, Subcommand};
use clap::{command, Parser, Subcommand, ValueEnum};

/// Oxup is a tool for managing installations and packages of oxido.
#[derive(Parser, Debug)]
Expand All @@ -25,6 +26,10 @@ struct Oxup {
/// Force run as Linux
#[clap(short, long, value_parser)]
linux: bool,

/// Do not check for updates
#[clap(short, long, value_parser)]
no_update: bool,
}

#[derive(Debug, Subcommand)]
Expand All @@ -46,8 +51,14 @@ enum Commands {
Uninstall,

/// Update oxido to latest version avaliable
#[command()]
Update,
#[command(arg_required_else_help = true)]
Update { update: Updateable },
}

#[derive(Debug, Clone, ValueEnum)]
pub enum Updateable {
Oxido,
Oxup,
}

#[tokio::main]
Expand All @@ -64,14 +75,26 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}

match args.command {
Commands::Install | Commands::Update => {
install::install(os).await?;
Commands::Install => {
install::install(os, false).await?;
}
Commands::Init { file } => init::init(file),
Commands::Uninstall => {
uninstall::uninstall(os);
}
Commands::Setup => setup::setup(os),
Commands::Update { update } => update::update(update, os).await?,
}

let current_version = env!("CARGO_PKG_VERSION");

let new_version = reqwest::get("https://raw.githubusercontent.com/oxidic/oxup/main/.version")
.await?
.text()
.await?;

if !args.no_update && current_version != new_version {
info![format!("Oxup v{new_version} is avaliable for download.")];
}

Ok(())
Expand All @@ -82,14 +105,14 @@ mod macros {
#[macro_export]
macro_rules! info {
($message:expr) => {
println!("{} {}", "\x1b[1minfo:\x1b[0m", $message);
println!("{} {}", "\x1b[1minfo:\x1b[0m", $message)
};
}

#[macro_export]
macro_rules! error {
($message:expr) => {
println!("{} {}", "\x1b[1m\x1b[31merror:\x1b[0m", $message);
println!("{} {}", "\x1b[1m\x1b[31merror:\x1b[0m", $message)
};
}

Expand Down
19 changes: 19 additions & 0 deletions src/update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::{install::install, Updateable, OS};

pub async fn update(update: Updateable, os: OS) -> Result<(), Box<dyn std::error::Error>> {
let (bin, oxup) = match update {
Updateable::Oxido => ("oxido", false),
Updateable::Oxup => ("oxup", true),
};

std::fs::remove_file(match os {
OS::Windows => format!(r"C:\bin\{bin}.exe"),
_ => {
format!("{}/.oxido/bin/{bin}", std::env::var("HOME").unwrap())
}
})?;

install(os, oxup).await?;

Ok(())
}

0 comments on commit e8784d0

Please sign in to comment.