Skip to content

Commit

Permalink
chore: enable to fix the missing property when failed to get it from …
Browse files Browse the repository at this point in the history
…the config rc (#195)
  • Loading branch information
wangziling authored Dec 22, 2023
1 parent 25fe57d commit 46edc10
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/commands/install.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -30,7 +30,8 @@ cfg_if! {
}

pub fn exec(_: &DvmMeta, no_use: bool, version: Option<String>) -> 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 {
Expand Down
8 changes: 5 additions & 3 deletions src/commands/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
5 changes: 3 additions & 2 deletions src/commands/use_version.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -18,7 +18,8 @@ use std::process::Command;

/// using a tag or a specific version
pub fn exec(meta: &mut DvmMeta, version: Option<String>, 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 {
Expand Down
10 changes: 10 additions & 0 deletions src/configrc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ pub fn rc_get(key: &str) -> io::Result<String> {
.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<String> {
// 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
Expand Down
4 changes: 2 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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())
}
Expand Down
4 changes: 2 additions & 2 deletions src/version.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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))
},
Expand Down

0 comments on commit 46edc10

Please sign in to comment.