Skip to content

Commit

Permalink
feat: add update command
Browse files Browse the repository at this point in the history
  • Loading branch information
alexng353 committed Feb 20, 2025
1 parent 0f132c1 commit 4c411d2
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub mod shell;
pub mod sign;
pub mod unlink;
pub mod unset;
pub mod update;
pub mod upload;
pub mod variables;
pub mod version;
55 changes: 55 additions & 0 deletions src/commands/update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::cmp::Ordering;

use crate::utils::{compare_semver, config::Config};

use super::*;

/// If your key is not in the database, use this command to upload it
#[derive(Parser)]
pub struct Args {}

pub async fn command(_args: Args) -> Result<()> {
let mut config = Config::get()?;
let result = config.check_update(true).await?;
config.write()?;
let latest_version = if let Some(latest_version) = result {
latest_version
} else {
println!("No updates available");
return Ok(());
};

if matches!(
compare_semver(env!("CARGO_PKG_VERSION"), &latest_version),
Ordering::Less
) {
println!(
"{}: v{} -> v{}",
"Update available".green().bold(),
env!("CARGO_PKG_VERSION").yellow(),
latest_version.bright_yellow(),
);
// println!(
// "Run `{}` to update\n",
// "curl -fsSL https://get.envx.sh | sh".green()
// );
}

let mut output = tokio::process::Command::new("sh")
.arg("-c")
.arg("curl -fsSL https://get.envx.sh | sh")
.stdout(std::process::Stdio::inherit())
.stderr(std::process::Stdio::inherit())
.stdin(std::process::Stdio::inherit())
.spawn()?;

let status = output.wait().await?;

if status.success() {
println!("Command executed successfully.");
} else {
eprintln!("Command failed with status: {:?}", status);
}

Ok(())
}
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ commands_enum!(
);

fn spawn_update_task(
mut configs: Config,
mut config: Config,
) -> tokio::task::JoinHandle<Result<(), anyhow::Error>> {
tokio::spawn(async move {
if !std::io::stdout().is_terminal() {
return Ok::<(), anyhow::Error>(());
}

let result = configs.check_update(false).await;
let result = config.check_update(false).await;
if let Ok(Some(latest_version)) = result {
configs.new_version_available = Some(latest_version);
config.new_version_available = Some(latest_version);
}
configs.write()?;
config.write()?;
Ok::<(), anyhow::Error>(())
})
}
Expand Down
14 changes: 9 additions & 5 deletions src/utils/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ struct GithubApiRelease {
}

impl Config {
pub fn get() -> Result<Self> {
let path = get_config_path().context("Failed to get config path")?;
let contents =
fs::read_to_string(path).context("Failed to read config file")?;
serde_json::from_str::<Self>(&contents)
.context("Failed to parse config file")
}
pub async fn check_update(
&mut self,
force: bool,
Expand Down Expand Up @@ -319,10 +326,7 @@ pub fn get_config_path() -> Result<PathBuf> {
}

/// Read the configuration file and parse it into a Config struct
#[deprecated]
pub fn get_config() -> Result<Config> {
let path = get_config_path().context("Failed to get config path")?;
let contents =
fs::read_to_string(path).context("Failed to read config file")?;
serde_json::from_str::<Config>(&contents)
.context("Failed to parse config file")
Config::get()
}

0 comments on commit 4c411d2

Please sign in to comment.