Skip to content

Commit e8784d0

Browse files
committed
feat: update command
1 parent 5f18aeb commit e8784d0

6 files changed

Lines changed: 87 additions & 34 deletions

File tree

.version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.5.0

Cargo.lock

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "oxup"
3-
version = "2.2.3"
3+
version = "2.5.0"
44
edition = "2021"
55

66
[profile.release]

src/install.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,29 @@ pub struct ReleaseData {
1717
assets: Vec<Release>,
1818
}
1919

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

24-
let target = "https://api.github.com/repositories/500013933/releases/latest";
24+
let target = if oxup {
25+
"https://api.github.com/repos/oxidic/oxup/releases/latest"
26+
} else {
27+
"https://api.github.com/repositories/500013933/releases/latest"
28+
};
2529
let client = Client::new();
2630
let response = client.get(target).headers(headers.clone()).send().await?;
2731
let result: ReleaseData = response.json().await?;
2832

29-
let filter = match os {
30-
OS::Mac => "oxido-darwin",
31-
OS::Linux => "oxido",
32-
OS::Windows => "oxido.exe",
33+
let bin = if oxup {
34+
"oxup"
35+
} else {
36+
"oxido"
37+
};
38+
39+
let filter = &match os {
40+
OS::Mac => bin.to_owned() + "darwin",
41+
OS::Linux => bin.to_owned(),
42+
OS::Windows => bin.to_owned() + ".exe",
3343
};
3444

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

42-
info!["Downloading oxido"];
52+
info![format!("Downloading {bin}")];
4353

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

4959
std::fs::write(
5060
match os {
51-
OS::Windows => String::from(r"C:\bin"),
61+
OS::Windows => format!(r"C:\bin\{bin}.exe"),
5262
_ => {
53-
format!("{}/.oxido/bin", std::env::var("HOME").unwrap())
63+
format!("{}/.oxido/bin/{bin}", std::env::var("HOME").unwrap())
5464
}
5565
},
5666
bytes,
5767
)?;
5868

59-
success!["Oxido has been installed!"];
69+
success![format!("{bin} has been installed!")];
6070

6171
Ok(())
6272
}

src/main.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ mod install;
33
mod os;
44
mod setup;
55
mod uninstall;
6+
mod update;
67

78
use crate::os::OS;
8-
use clap::{command, Parser, Subcommand};
9+
use clap::{command, Parser, Subcommand, ValueEnum};
910

1011
/// Oxup is a tool for managing installations and packages of oxido.
1112
#[derive(Parser, Debug)]
@@ -25,6 +26,10 @@ struct Oxup {
2526
/// Force run as Linux
2627
#[clap(short, long, value_parser)]
2728
linux: bool,
29+
30+
/// Do not check for updates
31+
#[clap(short, long, value_parser)]
32+
no_update: bool,
2833
}
2934

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

4853
/// Update oxido to latest version avaliable
49-
#[command()]
50-
Update,
54+
#[command(arg_required_else_help = true)]
55+
Update { update: Updateable },
56+
}
57+
58+
#[derive(Debug, Clone, ValueEnum)]
59+
pub enum Updateable {
60+
Oxido,
61+
Oxup,
5162
}
5263

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

6677
match args.command {
67-
Commands::Install | Commands::Update => {
68-
install::install(os).await?;
78+
Commands::Install => {
79+
install::install(os, false).await?;
6980
}
7081
Commands::Init { file } => init::init(file),
7182
Commands::Uninstall => {
7283
uninstall::uninstall(os);
7384
}
7485
Commands::Setup => setup::setup(os),
86+
Commands::Update { update } => update::update(update, os).await?,
87+
}
88+
89+
let current_version = env!("CARGO_PKG_VERSION");
90+
91+
let new_version = reqwest::get("https://raw.githubusercontent.com/oxidic/oxup/main/.version")
92+
.await?
93+
.text()
94+
.await?;
95+
96+
if !args.no_update && current_version != new_version {
97+
info![format!("Oxup v{new_version} is avaliable for download.")];
7598
}
7699

77100
Ok(())
@@ -82,14 +105,14 @@ mod macros {
82105
#[macro_export]
83106
macro_rules! info {
84107
($message:expr) => {
85-
println!("{} {}", "\x1b[1minfo:\x1b[0m", $message);
108+
println!("{} {}", "\x1b[1minfo:\x1b[0m", $message)
86109
};
87110
}
88111

89112
#[macro_export]
90113
macro_rules! error {
91114
($message:expr) => {
92-
println!("{} {}", "\x1b[1m\x1b[31merror:\x1b[0m", $message);
115+
println!("{} {}", "\x1b[1m\x1b[31merror:\x1b[0m", $message)
93116
};
94117
}
95118

src/update.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use crate::{install::install, Updateable, OS};
2+
3+
pub async fn update(update: Updateable, os: OS) -> Result<(), Box<dyn std::error::Error>> {
4+
let (bin, oxup) = match update {
5+
Updateable::Oxido => ("oxido", false),
6+
Updateable::Oxup => ("oxup", true),
7+
};
8+
9+
std::fs::remove_file(match os {
10+
OS::Windows => format!(r"C:\bin\{bin}.exe"),
11+
_ => {
12+
format!("{}/.oxido/bin/{bin}", std::env::var("HOME").unwrap())
13+
}
14+
})?;
15+
16+
install(os, oxup).await?;
17+
18+
Ok(())
19+
}

0 commit comments

Comments
 (0)