Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: enable to fix the missing property when failed to get it from the config rc #195

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading