mirror of
https://github.com/openai/codex.git
synced 2026-05-02 12:21:26 +03:00
V3
This commit is contained in:
@@ -3,10 +3,15 @@ mod errors;
|
||||
|
||||
use async_trait::async_trait;
|
||||
pub use errors::Error;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use std::path::Path;
|
||||
|
||||
use crate::brew::BrewInstaller;
|
||||
|
||||
#[derive(Debug)]
|
||||
const AUTO_UPDATER_STATUS_KEY: &str = "auto_updater.status";
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct UpdateStatus {
|
||||
pub current_version: String,
|
||||
pub latest_version: String,
|
||||
@@ -31,10 +36,14 @@ pub fn installer() -> Result<Box<dyn Installer>, Error> {
|
||||
Err(Error::Unsupported)
|
||||
}
|
||||
|
||||
pub fn update_status() -> Result<UpdateStatus, Error> {
|
||||
fn compute_update_status() -> Result<UpdateStatus, Error> {
|
||||
installer()?.version_status()
|
||||
}
|
||||
|
||||
pub fn update_status() -> Result<UpdateStatus, Error> {
|
||||
compute_update_status()
|
||||
}
|
||||
|
||||
pub fn update_available() -> Result<bool, Error> {
|
||||
installer()?.update_available()
|
||||
}
|
||||
@@ -42,3 +51,38 @@ pub fn update_available() -> Result<bool, Error> {
|
||||
pub async fn update() -> Result<String, Error> {
|
||||
installer()?.update().await
|
||||
}
|
||||
|
||||
pub fn initialize_storage(codex_home: &Path) -> Result<(), Error> {
|
||||
codex_internal_storage::initialize(codex_home.to_path_buf());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn read_cached_status() -> Result<Option<UpdateStatus>, Error> {
|
||||
match codex_internal_storage::read(AUTO_UPDATER_STATUS_KEY) {
|
||||
Ok(Some(value)) => {
|
||||
let status =
|
||||
serde_json::from_str(&value).map_err(|err| Error::Json(err.to_string()))?;
|
||||
Ok(Some(status))
|
||||
}
|
||||
Ok(None) => Ok(None),
|
||||
Err(err) => Err(map_storage_error(err)),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn refresh_status() -> Result<UpdateStatus, Error> {
|
||||
let status = compute_update_status()?;
|
||||
let serialized = serde_json::to_string(&status).map_err(|err| Error::Json(err.to_string()))?;
|
||||
codex_internal_storage::write(AUTO_UPDATER_STATUS_KEY, &serialized)
|
||||
.map_err(map_storage_error)?;
|
||||
Ok(status)
|
||||
}
|
||||
|
||||
fn map_storage_error(err: codex_internal_storage::InternalStorageError) -> Error {
|
||||
match err {
|
||||
codex_internal_storage::InternalStorageError::Io(err) => Error::Io(err.to_string()),
|
||||
codex_internal_storage::InternalStorageError::Json(err) => Error::Json(err.to_string()),
|
||||
codex_internal_storage::InternalStorageError::Uninitialized => {
|
||||
Error::Io("internal storage not initialized".into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user