Normalize paths when printing configuration

This commit is contained in:
2022-05-14 18:42:31 +02:00
parent 664d44eddc
commit 127dd0535e
3 changed files with 27 additions and 12 deletions

View File

@@ -4,7 +4,6 @@ use std::process;
use crate::output::*; use crate::output::*;
use super::repo::RepoConfig; use super::repo::RepoConfig;
use std::path::Path; use std::path::Path;
use crate::get_token_from_command; use crate::get_token_from_command;
@@ -147,6 +146,27 @@ impl Config {
}) })
} }
pub fn normalize(&mut self) {
if let Config::ConfigTree(config) = self {
let home = super::env_home().display().to_string();
for tree in &mut config.trees.0 {
if tree.root.starts_with(&home) {
// The tilde is not handled differently, it's just a normal path component for `Path`.
// Therefore we can treat it like that during **output**.
//
// The `unwrap()` is safe here as we are testing via `starts_with()`
// beforehand
let mut path = tree.root.strip_prefix(&home).unwrap();
if path.starts_with('/') {
path = path.strip_prefix('/').unwrap();
}
tree.root = Path::new("~").join(path).display().to_string();
}
}
}
}
pub fn as_toml(&self) -> Result<String, String> { pub fn as_toml(&self) -> Result<String, String> {
match toml::to_string(self) { match toml::to_string(self) {
Ok(toml) => Ok(toml), Ok(toml) => Ok(toml),

View File

@@ -198,7 +198,9 @@ fn main() {
}) { }) {
print_warning("No repositories found"); print_warning("No repositories found");
} else { } else {
let config = trees.to_config(); let mut config = trees.to_config();
config.normalize();
match args.format { match args.format {
cmd::ConfigFormat::Toml => { cmd::ConfigFormat::Toml => {
@@ -403,7 +405,9 @@ fn main() {
trees.push(tree); trees.push(tree);
} }
let config = config::Config::from_trees(trees); let mut config = config::Config::from_trees(trees);
config.normalize();
match args.format { match args.format {
cmd::ConfigFormat::Toml => { cmd::ConfigFormat::Toml => {

View File

@@ -487,15 +487,6 @@ pub fn find_in_tree(path: &Path) -> Result<(Tree, Vec<String>), String> {
} }
} }
} }
let home = env_home();
if root.starts_with(&home) {
// The tilde is not handled differently, it's just a normal path component for `Path`.
// Therefore we can treat it like that during **output**.
//
// The `unwrap()` is safe here as we are testing via `starts_with()`
// beforehand
root = Path::new("~").join(root.strip_prefix(&home).unwrap());
}
Ok(( Ok((
Tree { Tree {