Skip to content

Commit

Permalink
chore: Use consatnts.rs instead of dotenv (#8)
Browse files Browse the repository at this point in the history
* Updated dotenv path

* Removed dotenv

* Added constants map

* Added temporary functions for macos
  • Loading branch information
priyansh17 authored Mar 6, 2024
1 parent 06e0f3f commit 5ab6977
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 47 deletions.
17 changes: 0 additions & 17 deletions .env

This file was deleted.

2 changes: 0 additions & 2 deletions .github/workflows/build_and_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ jobs:
# make a release folder
mkdir source
mv .env source/
mv target/release/jvem_amd64 source/
tar -czvf ${{ env.LINUX_ARTIFACT }} -C source/ .
Expand All @@ -63,7 +62,6 @@ jobs:
# make a release folder
mkdir source
copy .env source/
copy target\release\jvem.exe source/
Compress-Archive -Path source\* -DestinationPath ${{ env.WINDOWS_ARTIFACT }}
Expand Down
7 changes: 0 additions & 7 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ edition = "2021"

[dependencies]
clap = { version = "4.5.1", features = ["derive"] }
dotenv = "0.15.0"
lazy_static = "1.4.0"
serde = { version = "1.0.197", features = ["serde_derive"] }
tokio = { version = "1.36.0", features = ["full"] }
Expand Down
10 changes: 10 additions & 0 deletions src/commands/deactivate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ fn deactivate_util() {
}
}

#[cfg(target_os = "macos")]
fn deactivate_util() {
let output = run_command("rm", vec!["-rf", &format!("{}/.jvem/java", get_home_dir())]);
if output.status.success() {
println!("deactivation successful");
} else {
println!("deactivation failed");
}
}

pub fn deactivate() {
let _ = deactivate_util();
}
5 changes: 5 additions & 0 deletions src/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ fn install_util(name: String, link: String) {
}
}

#[cfg(target_os = "macos")]
fn install_util(name: String, link: String) {
// Implement installation logic for macOS
}

pub fn install(name: String) {
let res: Result<String, Box<dyn Error>> = get_download_link(name.clone(), std::env::consts::OS);
match res {
Expand Down
15 changes: 15 additions & 0 deletions src/commands/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ fn uninstall_util(name: &str) {
}
}

#[cfg(target_os = "macos")]
fn uninstall_util(name: &str) {
// ToDo

let output = run_command(
"rm",
vec!["-rf", &format!("{}", get_installation_dir(&name))],
);
if output.status.success() {
println!("uninstall successful ");
} else {
println!("uninstall failed: maybe the mentioned jdk is not installed locally");
}
}

pub fn uninstall(name: String) {
let _ = uninstall_util(&name);
}
29 changes: 28 additions & 1 deletion src/commands/usev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ async fn usev_util(name: String) {
// remove the previously linked folder
let _ = run_command(
"powershell",
vec!["-Command", &format!("rm -r {}\\.jvem\\java", get_home_dir())],
vec![
"-Command",
&format!("rm -r {}\\.jvem\\java", get_home_dir()),
],
);
let output = run_command(
"powershell",
Expand Down Expand Up @@ -81,6 +84,30 @@ pub async fn usev_util(name: String) {
}
}

#[cfg(target_os = "macos")]
pub async fn usev_util(name: String) {
// ToDo
let _ = run_command("rm", vec!["-rf", &format!("{}/.jvem/java", get_home_dir())]);

let output = run_command(
"sh",
vec![
"-c",
&format!(
"ln --symbolic {} {}/.jvem/java",
get_installation_dir(&name),
get_home_dir()
),
],
);

if output.status.success() {
println!("set jdk version successfully");
} else {
println!("failed: {}", String::from_utf8_lossy(&output.stderr))
}
}

pub async fn usev(name: String) {
match check_jdk_exists(&name) {
true => {
Expand Down
1 change: 1 addition & 0 deletions src/constants/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod versions;
41 changes: 41 additions & 0 deletions src/constants/versions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
pub mod constants {
pub const LINUX_ARTIFACT: &str = "jvem.tar.gz";
pub const WINDOWS_ARTIFACT: &str = "jvem.zip";
pub const AVAILABLE_VERSIONS: &str = "ZULU8,ZULU11,ZULU17,ZULU21";
pub const ZULU21_LINUX: &str =
"https://cdn.azul.com/zulu/bin/zulu21.32.17-ca-jdk21.0.2-linux_x64.tar.gz";
pub const ZULU21_WINDOWS: &str = "https://cdn.azul.com/zulu/bin/zulu21.32.17-ca-jdk21.0.2-win_x64.zip?_gl=1*14dmzoc*_ga*MTg0NTAyMjU0LjE3MDg3MDUwMzU.*_ga_42DEGWGYD5*MTcwODcwNTAzNS4xLjEuMTcwODcwNTExMi40NC4wLjA.";
// ZULU21_MACOS

pub const ZULU17_LINUX: &str =
"https://cdn.azul.com/zulu/bin/zulu17.48.15-ca-jdk17.0.10-linux_x64.tar.gz";
pub const ZULU17_WINDOWS: &str = "https://cdn.azul.com/zulu/bin/zulu17.48.15-ca-jdk17.0.10-win_x64.zip?_gl=1*1nexba0*_ga*MTg0NTAyMjU0LjE3MDg3MDUwMzU.*_ga_42DEGWGYD5*MTcwODcwNTAzNS4xLjEuMTcwODcwNTE0Ny45LjAuMA..";
// ZULU17_MACOS

pub const ZULU11_LINUX: &str =
"https://cdn.azul.com/zulu/bin/zulu11.70.15-ca-jdk11.0.22-linux_x64.tar.gz";
pub const ZULU11_WINDOWS: &str = "https://cdn.azul.com/zulu/bin/zulu11.70.15-ca-jdk11.0.22-win_x64.zip?_gl=1*2j7qzc*_ga*MTg0NTAyMjU0LjE3MDg3MDUwMzU.*_ga_42DEGWGYD5*MTcwODcwNTAzNS4xLjEuMTcwODcwNTIwNS4yNS4wLjA.";
// ZULU11_MACOS

pub const ZULU8_LINUX: &str =
"https://cdn.azul.com/zulu/bin/zulu8.76.0.17-ca-jdk8.0.402-linux_i686.tar.gz";
pub const ZULU8_WINDOWS: &str ="https://cdn.azul.com/zulu/bin/zulu8.76.0.17-ca-jdk8.0.402-win_x64.zip?_gl=1*i45ppb*_ga*MTg0NTAyMjU0LjE3MDg3MDUwMzU.*_ga_42DEGWGYD5*MTcwODcwNTAzNS4xLjEuMTcwODcwNTIxMy4xNy4wLjA.";
// ZULU8_MACOS

pub fn get_constant(key: &str) -> Option<&'static str> {
match key {
"LINUX_ARTIFACT" => Some(LINUX_ARTIFACT),
"WINDOWS_ARTIFACT" => Some(WINDOWS_ARTIFACT),
"AVAILABLE_VERSIONS" => Some(AVAILABLE_VERSIONS),
"ZULU21_LINUX" => Some(ZULU21_LINUX),
"ZULU21_WINDOWS" => Some(ZULU21_WINDOWS),
"ZULU17_LINUX" => Some(ZULU17_LINUX),
"ZULU17_WINDOWS" => Some(ZULU17_WINDOWS),
"ZULU11_LINUX" => Some(ZULU11_LINUX),
"ZULU11_WINDOWS" => Some(ZULU11_WINDOWS),
"ZULU8_LINUX" => Some(ZULU8_LINUX),
"ZULU8_WINDOWS" => Some(ZULU8_WINDOWS),
_ => None,
}
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod commands;
pub mod constants;
pub mod utils;

use std::sync::{
Expand Down Expand Up @@ -73,7 +74,6 @@ async fn main() {
// the below logic is used for ctrl+c handling
// the idea here is to set a boolean atomic value, when program is running
// on ctrl+c, the value is set to false and it stops the process
dotenv::dotenv().ok();

let running = Arc::new(AtomicBool::new(true));
let running_clone = Arc::clone(&running);
Expand Down
30 changes: 12 additions & 18 deletions src/utils/env_ops.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
use std::error::Error;
use crate::constants::versions::constants;

pub fn read_versions() -> Result<(), Box<dyn std::error::Error>> {
// Load environment variables from .env file
dotenv::dotenv().ok();
let available_versions = constants::AVAILABLE_VERSIONS;
let versions: Vec<&str> = available_versions.split(',').collect();

if let Ok(available_versions) = std::env::var("AVAILABLE_VERSIONS") {
let versions: Vec<&str> = available_versions.split(',').collect();

for version in versions {
println!("{}", version);
}
println!("Availaible Versions:");
for version in versions {
println!("{}", version);
}

Ok(())
}

pub fn get_download_link(name: String, os: &str) -> Result<String, Box<dyn Error>> {
// Load environment variables from .env file
dotenv::dotenv().ok();

// Access the environment variable for the specified name
pub fn get_download_link(name: String, os: &str) -> Result<String, Box<dyn std::error::Error>> {
let env_var_name = format!("{}_{}", name.to_uppercase(), os.to_uppercase());
match std::env::var(&env_var_name) {
Ok(value) => Ok(value),
Err(_) => Err("Couldn't recognize OS or the specified JDK is not available".into()),
if let Some(value) = constants::get_constant(&env_var_name) {
Ok(value.to_string())
} else {
Err("Couldn't recognize OS or the specified JDK is not available".into())
}
}
}

0 comments on commit 5ab6977

Please # to comment.