Skip to content

Commit

Permalink
trust: find .rtx.local.toml files (#1183)
Browse files Browse the repository at this point in the history
Fixes #1050
  • Loading branch information
jdx authored Dec 14, 2023
1 parent 16921e3 commit 5a314a6
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 18 deletions.
4 changes: 0 additions & 4 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,11 @@ test-coverage:
if [[ "${TEST_TRANCHE:-}" == 0 ]]; then
echo "::group::Unit tests"
cargo test --all-features
echo "::group::Trust"
rtx trust
echo "::group::render-help render-completions render-mangen"
just render-help render-completions render-mangen
echo "::group::Implode"
rtx implode
elif [[ "${TEST_TRANCHE:-}" == 1 ]]; then
echo "::group::Trust"
rtx trust
echo "::group::Self update"
rtx self-update -fy
fi
Expand Down
5 changes: 5 additions & 0 deletions src/cli/snapshots/rtx__cli__trust__tests__trust-4.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: src/cli/trust.rs
expression: output
---
untrusted ~/cwd/.test-tool-versions
48 changes: 39 additions & 9 deletions src/cli/trust.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::collections::BTreeMap;
use std::path::PathBuf;

use clap::ValueHint;
use color_eyre::eyre::Result;

use crate::cli::local;
use crate::config;
use crate::config::config_file;

/// Marks a config file as trusted
Expand All @@ -29,19 +30,47 @@ pub struct Trust {

impl Trust {
pub fn run(self) -> Result<()> {
let path = match &self.config_file {
Some(filename) => PathBuf::from(filename),
None => local::get_parent_path()?,
};
if self.untrust {
config_file::untrust(&path)?;
rtxprintln!("untrusted {}", &path.canonicalize()?.display());
self.untrust()
} else {
config_file::trust(&path)?;
rtxprintln!("trusted {}", &path.canonicalize()?.display());
self.trust()
}
}
fn untrust(&self) -> Result<()> {
let path = match &self.config_file {
Some(filename) => PathBuf::from(filename),
None => match self.get_next_trusted() {
Some(path) => path,
None => bail!("No trusted config files found."),
},
};
config_file::untrust(&path)?;
rtxprintln!("untrusted {}", &path.canonicalize()?.display());
Ok(())
}
fn trust(&self) -> Result<()> {
let path = match &self.config_file {
Some(filename) => PathBuf::from(filename),
None => match self.get_next_untrusted() {
Some(path) => path,
None => bail!("No untrusted config files found."),
},
};
config_file::trust(&path)?;
rtxprintln!("trusted {}", &path.canonicalize()?.display());
Ok(())
}

fn get_next_trusted(&self) -> Option<PathBuf> {
config::load_config_filenames(&BTreeMap::new())
.into_iter()
.find(|p| config_file::is_trusted(p))
}
fn get_next_untrusted(&self) -> Option<PathBuf> {
config::load_config_filenames(&BTreeMap::new())
.into_iter()
.find(|p| !config_file::is_trusted(p))
}
}

static AFTER_LONG_HELP: &str = color_print::cstr!(
Expand All @@ -63,5 +92,6 @@ mod tests {
assert_cli_snapshot!("trust");
assert_cli_snapshot!("trust", "--untrust");
assert_cli_snapshot!("trust", ".test-tool-versions");
assert_cli_snapshot!("trust", "--untrust", ".test-tool-versions");
}
}
11 changes: 7 additions & 4 deletions src/config/config_file/tool_versions.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::fmt::{Display, Formatter};
use std::path::{Path, PathBuf};

use crate::config::config_file;
use color_eyre::eyre::Result;
use console::{measure_text_width, pad_str, Alignment};
use indexmap::IndexMap;
use itertools::Itertools;
use tera::Context;

use crate::config::config_file::{is_trusted, ConfigFile, ConfigFileType};
use crate::config::config_file::{ConfigFile, ConfigFileType};

use crate::file;
use crate::file::display_path;
Expand Down Expand Up @@ -56,7 +57,7 @@ impl ToolVersions {
pub fn parse_str(s: &str, path: PathBuf) -> Result<Self> {
let mut cf = Self::init(&path);
let dir = path.parent().unwrap();
let s = if is_trusted(&path) {
let s = if config_file::is_trusted(&path) {
get_tera(dir).render_str(s, &cf.context)?
} else {
s.to_string()
Expand Down Expand Up @@ -202,7 +203,7 @@ pub(crate) mod tests {
use insta::{assert_display_snapshot, assert_snapshot};
use pretty_assertions::assert_eq;

use crate::dirs;
use crate::{assert_cli, dirs};

use super::*;

Expand Down Expand Up @@ -247,7 +248,9 @@ pub(crate) mod tests {
python {{exec(command='echo 3.11.0')}}
"};
let path = dirs::CURRENT.join(".test-tool-versions");
let tv = ToolVersions::parse_str(orig, path).unwrap();
assert_cli!("trust", path.to_string_lossy());
let tv = ToolVersions::parse_str(orig, path.clone()).unwrap();
assert_cli!("trust", "--untrust", path.to_string_lossy());
assert_snapshot!(tv.dump(), @r###"
ruby 3.0.5
python 3.11.0
Expand Down
2 changes: 1 addition & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ fn load_legacy_files(settings: &Settings, tools: &PluginMap) -> BTreeMap<String,
legacy_filenames
}

fn load_config_filenames(legacy_filenames: &BTreeMap<String, Vec<PluginName>>) -> Vec<PathBuf> {
pub fn load_config_filenames(legacy_filenames: &BTreeMap<String, Vec<PluginName>>) -> Vec<PathBuf> {
let mut filenames = legacy_filenames.keys().cloned().collect_vec();
filenames.push(env::RTX_DEFAULT_TOOL_VERSIONS_FILENAME.clone());
filenames.push(env::RTX_DEFAULT_CONFIG_FILENAME.clone());
Expand Down

0 comments on commit 5a314a6

Please sign in to comment.