Skip to content

Commit

Permalink
feat: list command
Browse files Browse the repository at this point in the history
  • Loading branch information
megatank58 committed Apr 25, 2023
1 parent 51cebc5 commit f4f58a6
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ version = \"0.1.0\"
write(format!("{name}/src/main.oxi"), default_function).unwrap();
write(format!("{name}/Oxate.toml"), metadata).unwrap();

success![format!("Successfully initialised {name}!")]
success!(format!("Successfully initialised {name}!"))
}
33 changes: 18 additions & 15 deletions src/install.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::{os::unix::prelude::PermissionsExt, env::var, fs::{write, set_permissions, Permissions}};
use crate::{info, success};
use reqwest::{
header::{HeaderMap, USER_AGENT},
Client,
};
use serde::Deserialize;
use std::{
fs::{set_permissions, write, Permissions},
os::unix::prelude::PermissionsExt,
};

use crate::os::OS;

Expand All @@ -15,24 +18,23 @@ pub struct Release {

#[derive(Deserialize, Debug)]
pub struct ReleaseData {
name: String,
assets: Vec<Release>,
}

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

let target = if oxate {
"https://api.github.com/repos/oxidic/oxate/releases/latest"
} else {
"https://api.github.com/repositories/500013933/releases/latest"
};
let client = Client::new();
let mut headers = HeaderMap::new();
headers.insert(USER_AGENT, "megatank58".parse().unwrap());
let response = client.get(target).headers(headers.clone()).send().await?;
let result: ReleaseData = response.json().await?;

let bin = if oxate { "oxate" } else { "oxido" };
let home = var("HOME")?;

let filter = &match os {
OS::Mac => bin.to_owned() + "darwin",
Expand All @@ -47,31 +49,32 @@ pub async fn install(os: OS, oxate: bool) -> Result<(), Box<dyn std::error::Erro
.map(|f| f.browser_download_url.clone())
.collect();

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

let response = client.get(&url).headers(headers.clone()).send().await?;
let bytes = response.bytes().await?;

info!["Moving package"];
info!("Moving package");

write(
match os {
OS::Windows => format!(r"C:\bin\{bin}.exe"),
_ => {
format!("{home}/.oxido/bin/{bin}")
}
},
os.path()
+ &match os {
OS::Windows => bin.to_string() + ".exe",
_ => bin.to_string(),
},
bytes,
)?;

if os == OS::Linux || os == OS::Mac {
set_permissions(
format!("{home}/.oxido/bin/{bin}"),
os.path() + bin,
Permissions::from_mode(0o770),
)?;
}

success![format!("{bin} has been installed!")];
write(os.path() + ".version", result.name)?;

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

Ok(())
}
46 changes: 46 additions & 0 deletions src/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::fs::read;

use reqwest::{
header::{HeaderMap, USER_AGENT},
Client,
};
use serde::Deserialize;

use crate::{os::OS, success};

#[derive(Deserialize, Debug)]
pub struct Release {
name: String,
}

pub async fn list(os: OS) {
let client = Client::new();
let mut headers = HeaderMap::new();
headers.insert(USER_AGENT, "megatank58".parse().unwrap());
let response = client
.get("https://api.github.com/repos/oxidic/oxido/releases")
.headers(headers.clone())
.send()
.await
.unwrap();
let releases: Vec<Release> = response.json().await.unwrap();

let bytes = match read(os.path() + ".version") {
Ok(b) => b,
_ => vec![]
};
let current = String::from_utf8(bytes).unwrap();

success!("Oxido versions:");
for release in &releases {
if current.trim() == release.name {
println!(" \x1b[1m{} * default\x1b[0m", release.name);
} else {
println!(" \x1b[1m{}\x1b[0m", release.name);
}
}

if current < releases.first().unwrap().name {
println!("\nupdate to {r} by running \x1b[1m`oxate install {r}`\x1b[0m", r=releases.first().unwrap().name);
}
}
13 changes: 10 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use clap::{command, Parser, Subcommand, ValueEnum};

mod init;
mod install;
mod list;
mod os;
mod setup;
mod uninstall;
Expand All @@ -26,9 +27,14 @@ enum Commands {
#[command(arg_required_else_help = true)]
Init { name: String },

/// List the avaliable oxido versions
#[command()]
List,

/// Setup oxate and its directories
#[command()]
Setup,

/// Uninstall oxido
#[command()]
Uninstall,
Expand All @@ -55,6 +61,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
install::install(os, false).await?;
}
Commands::Init { name } => init::init(name),
Commands::List => list::list(os).await,
Commands::Uninstall => {
uninstall::uninstall(os);
}
Expand All @@ -70,22 +77,22 @@ mod macros {
#[macro_export]
macro_rules! info {
($message:expr) => {
println!("{} {}", "\x1b[1minfo:\x1b[0m", $message)
println!("{} {}", "\x1b[1m\x1b[1m=|\x1b[0m\x1b[1m", $message)
};
}

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

#[macro_export]
macro_rules! success {
($message:expr) => {
println!("{} {}", "\x1b[32m=>\x1b[0m", $message)
println!("{} {}", "\x1b[1m\x1b[32m=|\x1b[0m", $message)
};
}
}
9 changes: 8 additions & 1 deletion src/os.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::env::consts;
use std::env::{consts, var};

#[derive(PartialEq, Eq, PartialOrd, Ord)]
pub enum OS {
Expand All @@ -15,4 +15,11 @@ impl OS {
_ => OS::Windows,
}
}

pub fn path(&self) -> String {
match self {
OS::Windows => String::from(r"C:\bin\"),
_ => var("HOME").unwrap() + "/.oxido/bin/",
}
}
}
4 changes: 2 additions & 2 deletions src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn setup(os: OS) {
OS::Windows => {
if metadata("C:\\bin\\oxido").is_err() {
create_dir_all("C:\\bin\\oxido").unwrap();
info!["Created directory C:\\bin\\oxido"];
info!("Created directory C:\\bin\\oxido");
}

copy("oxate.exe", "C:\\bin\\oxido\\oxate.exe").unwrap();
Expand Down Expand Up @@ -47,7 +47,7 @@ pub fn setup(os: OS) {
remove_file("oxate").unwrap();
}

success![format!("Created {home}/.oxido")];
success!(format!("Created {home}/.oxido"));
}
}
}
2 changes: 1 addition & 1 deletion src/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ pub fn uninstall(os: OS) {
}
}

success!["Oxido has been uninstalled"];
success!("Oxido has been uninstalled");
}

0 comments on commit f4f58a6

Please sign in to comment.