From 46edc108cf9a046b57973a7e75d6e94d69986a23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=AD=90=E5=87=8C?= Date: Fri, 22 Dec 2023 16:56:23 +0800 Subject: [PATCH] chore: enable to fix the missing property when failed to get it from the config rc (#195) --- src/commands/install.rs | 5 +++-- src/commands/registry.rs | 8 +++++--- src/commands/use_version.rs | 5 +++-- src/configrc.rs | 10 ++++++++++ src/utils.rs | 4 ++-- src/version.rs | 4 ++-- 6 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/commands/install.rs b/src/commands/install.rs index 5fe045d..72e470e 100644 --- a/src/commands/install.rs +++ b/src/commands/install.rs @@ -1,7 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2020-2022 justjavac. All rights reserved. MIT license. use super::use_version; -use crate::configrc::rc_get; +use crate::configrc::rc_get_with_fix; use crate::consts::{ DVM_CACHE_PATH_PREFIX, DVM_CANARY_PATH_PREFIX, DVM_CONFIGRC_KEY_REGISTRY_BINARY, DVM_VERSION_CANARY, DVM_VERSION_LATEST, REGISTRY_OFFICIAL, @@ -30,7 +30,8 @@ cfg_if! { } pub fn exec(_: &DvmMeta, no_use: bool, version: Option) -> Result<()> { - let binary_registry_url = rc_get(DVM_CONFIGRC_KEY_REGISTRY_BINARY).unwrap_or_else(|_| REGISTRY_OFFICIAL.to_string()); + let binary_registry_url = + rc_get_with_fix(DVM_CONFIGRC_KEY_REGISTRY_BINARY).unwrap_or_else(|_| REGISTRY_OFFICIAL.to_string()); if let Some(version) = version.clone() { if version == *DVM_VERSION_CANARY { diff --git a/src/commands/registry.rs b/src/commands/registry.rs index 584dc38..8f46d91 100644 --- a/src/commands/registry.rs +++ b/src/commands/registry.rs @@ -7,14 +7,16 @@ use crate::consts::{DVM_CONFIGRC_KEY_REGISTRY_BINARY, DVM_CONFIGRC_KEY_REGISTRY_ use crate::consts::{REGISTRY_CN, REGISTRY_LIST_CN, REGISTRY_LIST_OFFICIAL}; use crate::DvmMeta; -use crate::configrc::{rc_get, rc_update}; +use crate::configrc::{rc_get_with_fix, rc_update}; use crate::utils::is_http_like_url; use anyhow::Result; use colored::Colorize; pub fn exec(meta: &mut DvmMeta, registry: RegistryCommands) -> Result<()> { - let rc_binary_registry = rc_get(DVM_CONFIGRC_KEY_REGISTRY_BINARY).unwrap_or_else(|_| REGISTRY_OFFICIAL.to_string()); - let rc_version_registry = rc_get(DVM_CONFIGRC_KEY_REGISTRY_VERSION).unwrap_or_else(|_| REGISTRY_OFFICIAL.to_string()); + let rc_binary_registry = + rc_get_with_fix(DVM_CONFIGRC_KEY_REGISTRY_BINARY).unwrap_or_else(|_| REGISTRY_OFFICIAL.to_string()); + let rc_version_registry = + rc_get_with_fix(DVM_CONFIGRC_KEY_REGISTRY_VERSION).unwrap_or_else(|_| REGISTRY_OFFICIAL.to_string()); match registry { RegistryCommands::List => { diff --git a/src/commands/use_version.rs b/src/commands/use_version.rs index ca6e33a..e1e6d15 100644 --- a/src/commands/use_version.rs +++ b/src/commands/use_version.rs @@ -1,5 +1,5 @@ use crate::commands::install; -use crate::configrc::{rc_get, rc_update}; +use crate::configrc::{rc_get_with_fix, rc_update}; use crate::consts::{ DVM_CONFIGRC_KEY_DENO_VERSION, DVM_CONFIGRC_KEY_REGISTRY_BINARY, DVM_VERSION_CANARY, DVM_VERSION_LATEST, DVM_VERSION_SYSTEM, REGISTRY_OFFICIAL, @@ -18,7 +18,8 @@ use std::process::Command; /// using a tag or a specific version pub fn exec(meta: &mut DvmMeta, version: Option, write_local: bool) -> Result<()> { - let rc_binary_url = rc_get(DVM_CONFIGRC_KEY_REGISTRY_BINARY).unwrap_or_else(|_| REGISTRY_OFFICIAL.to_string()); + let rc_binary_url = + rc_get_with_fix(DVM_CONFIGRC_KEY_REGISTRY_BINARY).unwrap_or_else(|_| REGISTRY_OFFICIAL.to_string()); let version_req: VersionArg; if let Some(ref version) = version { diff --git a/src/configrc.rs b/src/configrc.rs index a2540ec..9222825 100644 --- a/src/configrc.rs +++ b/src/configrc.rs @@ -73,6 +73,16 @@ pub fn rc_get(key: &str) -> io::Result { .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "key not found")) } +/// get value by key from configuration with a possible fix +/// first try to get from current folder +/// if not found, try to get from home folder +/// if not found, try to the fix the missing properties. +/// and then try to get this key's value again without the fix +pub fn rc_get_with_fix(key: &str) -> io::Result { + // always return the error which is from `rc_get` fn + rc_get(key).or_else(|err| rc_fix().and_then(|_| rc_get(key)).map_err(|_| err)) +} + /// update the config file key with the new value /// create the file if it doesn't exist /// create key value pair if it doesn't exist diff --git a/src/utils.rs b/src/utils.rs index d457753..910d66e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,4 @@ -use crate::configrc::rc_get; +use crate::configrc::rc_get_with_fix; use crate::consts::{DENO_EXE, DVM_CACHE_PATH_PREFIX, DVM_CANARY_PATH_PREFIX, DVM_CONFIGRC_KEY_DENO_VERSION}; use crate::version::VersionArg; use anyhow::Result; @@ -97,7 +97,7 @@ where /// Find and load the dvmrc /// local -> user -> default pub fn load_dvmrc() -> VersionArg { - rc_get(DVM_CONFIGRC_KEY_DENO_VERSION) + rc_get_with_fix(DVM_CONFIGRC_KEY_DENO_VERSION) .map(|v| VersionArg::from_str(&v).unwrap()) .unwrap_or_else(|_| VersionArg::from_str("*").unwrap()) } diff --git a/src/version.rs b/src/version.rs index 6b29722..a77f05c 100644 --- a/src/version.rs +++ b/src/version.rs @@ -1,5 +1,5 @@ // Copyright 2022 justjavac. All rights reserved. MIT license. -use crate::configrc::rc_get; +use crate::configrc::rc_get_with_fix; use crate::consts::{ DVM_CACHE_PATH_PREFIX, DVM_CACHE_REMOTE_PATH, DVM_CONFIGRC_KEY_REGISTRY_VERSION, REGISTRY_LATEST_CANARY_PATH, REGISTRY_LATEST_RELEASE_PATH, @@ -99,7 +99,7 @@ pub fn cache_remote_versions() -> Result<()> { |_| { let cached_remote_versions_location = cached_remote_versions_location(); - let remote_versions_url = rc_get(DVM_CONFIGRC_KEY_REGISTRY_VERSION)?; + let remote_versions_url = rc_get_with_fix(DVM_CONFIGRC_KEY_REGISTRY_VERSION)?; let remote_versions = tinyget::get(remote_versions_url).send()?.as_str()?.to_owned(); std::fs::write(cached_remote_versions_location, remote_versions).map_err(|e| anyhow::anyhow!(e)) },