Revert "fixed issues"

This reverts commit a51192025f.
This commit is contained in:
ObamaTheLlama114
2024-07-28 19:36:11 -05:00
parent a51192025f
commit ab68918fea
6 changed files with 77 additions and 57 deletions

View File

@ -17,6 +17,15 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "anstream" name = "anstream"
version = "0.6.14" version = "0.6.14"
@ -162,6 +171,8 @@ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
"colored", "colored",
"lazy_static",
"regex",
"tokio", "tokio",
] ]
@ -243,6 +254,35 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "regex"
version = "1.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b"
[[package]] [[package]]
name = "rustc-demangle" name = "rustc-demangle"
version = "0.1.24" version = "0.1.24"

View File

@ -9,4 +9,6 @@ edition = "2021"
anyhow = "1.0.86" anyhow = "1.0.86"
clap = { version = "4.5.9", features = ["derive", "env"] } clap = { version = "4.5.9", features = ["derive", "env"] }
colored = "2.1.0" colored = "2.1.0"
lazy_static = "1.5.0"
regex = "1.10.5"
tokio = "1.38.0" tokio = "1.38.0"

View File

@ -4,12 +4,10 @@
1. Verify that you have the latest version of rust installed 1. Verify that you have the latest version of rust installed
- Install [rust](rustup.rs) or update rust to the latest version with `rustup update` - Install [rust](rustup.rs) or update rust to the latest version with `rustup update`
2. Install the cli tool with `cargo install --git https://github.com/goauthentik/authentik --bin docsmg` 2. Install the cli tool with `curl https://raw.githubusercontent.com/goauthentik/authentik/main/website/scripts/docsmg/install.sh | sh`
## Steps to use ## Steps to use
1. Generate a migratefile with `docsmg generate` ### Adding entries
2. Find the files you want to move in `migratefile` and insert the path you want to move them to after the arrow; ex `path/to/move/from/file.md -> path/to/move/to/file.md` Note: make sure to put spaces on either side of the arrow or that line won't be recognized
3. Once you have entered all the paths you want to move, migrate the files with `docsmg migrate` Add entries to be moved with `map <MOVE_FROM> <MOVE_TO>`
4. To revert the migration, use `docsmg unmigrate`; Note: DO NOT edit the migrate file inbetween steps 3 and 4
5. Repeat steps 2-4 until you are satified with the result

View File

@ -1,14 +1,14 @@
use std::path::PathBuf; use std::path::PathBuf;
use crate::{migratefile::read_migrate_file_left_side, recurse_directory}; use crate::{migratefile::read_migrate_file, recurse_directory};
pub fn generate(migratefile: Option<PathBuf>, migrate_path: PathBuf) { pub fn generate(migratefile: Option<PathBuf>, migrate_path: PathBuf) {
// if there is a migrate file, read it and get the paths from the left side // if there is a migrate file, read it and get the paths from the left side
let paths: Vec<PathBuf> = match migratefile { let paths: Vec<PathBuf> = match migratefile {
Some(i) => { Some(i) => {
let contents = read_migrate_file_left_side(i); let contents = read_migrate_file(i);
if let Ok(contents) = contents { if let Ok(contents) = contents {
contents contents.iter().map(|x| x.0.clone()).collect()
} else { } else {
vec![] vec![]
} }
@ -20,12 +20,12 @@ pub fn generate(migratefile: Option<PathBuf>, migrate_path: PathBuf) {
// get rid of paths already in the specified migrate file // get rid of paths already in the specified migrate file
let paths: Vec<PathBuf> = recurse_directory(migrate_path.clone()) let paths: Vec<PathBuf> = recurse_directory(migrate_path.clone())
.iter() .iter()
.filter(|x| !paths.contains(x))
.filter_map(|x| x.strip_prefix(migrate_path.clone()).ok()) .filter_map(|x| x.strip_prefix(migrate_path.clone()).ok())
.filter(|x| !paths.contains(&x.to_path_buf()))
.map(|x| x.to_path_buf()) .map(|x| x.to_path_buf())
.collect(); .collect();
for path in paths { for path in paths {
println!("{} ->", path.display()); println!("{} -> ", path.display());
} }
} }

View File

@ -1,10 +1,9 @@
use std::{ use std::{
ffi::OsStr, collections::HashMap, ffi::OsStr, fmt::format, fs::{read_to_string, write}, path::{Component, Path, PathBuf}
fs::{read_to_string, write},
path::PathBuf,
}; };
use colored::Colorize; use colored::Colorize;
use lazy_static::lazy_static;
use crate::{migratefile::read_migrate_file, recurse_directory}; use crate::{migratefile::read_migrate_file, recurse_directory};
@ -27,8 +26,6 @@ pub fn migrate(quiet: bool, migratefile: PathBuf, migrate_path: PathBuf) {
} }
}; };
println!("len: {}", files.len());
replace_links(migrate_path.clone(), files.clone()); replace_links(migrate_path.clone(), files.clone());
let successful_moves = move_files(quiet, migrate_path.clone(), files); let successful_moves = move_files(quiet, migrate_path.clone(), files);
add_redirects(successful_moves.clone(), migrate_path.clone()); add_redirects(successful_moves.clone(), migrate_path.clone());
@ -98,38 +95,31 @@ fn move_files(
} }
fn replace_links(migrate_path: PathBuf, successful_moves: Vec<(PathBuf, PathBuf)>) { fn replace_links(migrate_path: PathBuf, successful_moves: Vec<(PathBuf, PathBuf)>) {
lazy_static! {
static ref find_link: regex::Regex =
regex::Regex::new(r"\[(?<a>.*)\]\((?<b>.*)\)").unwrap();
}
let files = recurse_directory(migrate_path.clone()); let files = recurse_directory(migrate_path.clone());
for file in files { for file in files {
let relative_file = file let relative_file = file.strip_prefix(migrate_path.clone()).unwrap().to_path_buf();
.strip_prefix(migrate_path.clone())
.unwrap()
.to_path_buf();
let mut contents = match read_to_string(file.clone()) { let mut contents = match read_to_string(file.clone()) {
Ok(i) => i, Ok(i) => i,
Err(_) => continue, Err(_) => continue,
}; };
let mut replace = vec![]; let mut replace = vec![];
for successful_move in &successful_moves { for successful_move in &successful_moves {
if migrate_path if migrate_path.join(successful_move.0.clone()).canonicalize().unwrap()
.join(successful_move.0.clone()) == file.clone().canonicalize().unwrap() {
.canonicalize()
.unwrap()
== file.clone().canonicalize().unwrap()
{
continue; continue;
} }
let new_successful_move_from = let new_successful_move_from = make_path_relative(successful_move.0.clone(), relative_file.clone());
make_path_relative(successful_move.0.clone(), relative_file.clone()); let new_successful_move_to = make_path_relative(successful_move.1.clone(), relative_file.clone());
let new_successful_move_to =
make_path_relative(successful_move.1.clone(), relative_file.clone());
replace.push((new_successful_move_from, new_successful_move_to)); replace.push((new_successful_move_from, new_successful_move_to));
} }
for i in replace { for i in replace {
contents = contents.replace( println!("{} : {} -> {}", file.display(), i.0.display(), i.1.display());
&format!("({})", i.0.display()), contents = contents.replace(&format!("({})", i.0.display()), &format!("({})", i.1.display()));
&format!("({})", i.1.display()),
);
} }
write(file, contents).unwrap(); write(file, contents).unwrap();
} }
@ -142,16 +132,21 @@ fn make_path_relative(path: PathBuf, relative_to: PathBuf) -> PathBuf {
loop { loop {
if path_components.len() <= subdirs { if path_components.len() <= subdirs {
break; break;
} else if path_components[subdirs] != relative_to_components[subdirs] { } else if path_components[subdirs]
!= relative_to_components[subdirs]
{
break; break;
} }
subdirs += 1; subdirs += 1;
} }
let new_path = &path_components[subdirs..].iter().collect::<PathBuf>(); let new_path = &path_components[subdirs..]
let backouts = (0..relative_to_components.len() - subdirs - 1) .iter()
.map(|_| PathBuf::from("..")) .collect::<PathBuf>();
.reduce(|acc, e| acc.join(e)) let backouts =
.unwrap_or(PathBuf::from("")); (0..relative_to_components.len() - subdirs - 1)
.map(|_| PathBuf::from(".."))
.reduce(|acc, e| acc.join(e))
.unwrap_or(PathBuf::from(""));
//println!("{}, {}", relative_to_components.len() - subdirs - 1, backouts.display()); //println!("{}, {}", relative_to_components.len() - subdirs - 1, backouts.display());
let new_path = backouts.join(new_path); let new_path = backouts.join(new_path);
let new_path = if new_path let new_path = if new_path
@ -167,13 +162,12 @@ fn make_path_relative(path: PathBuf, relative_to: PathBuf) -> PathBuf {
new_path new_path
}; };
let new_path = if new_path.file_name() == Some(OsStr::new("index.md")) let new_path = if new_path.file_name() == Some(OsStr::new("index.md")) || new_path.file_name() == Some(OsStr::new("index.mdx")) {
|| new_path.file_name() == Some(OsStr::new("index.mdx"))
{
new_path.parent().unwrap().to_path_buf() new_path.parent().unwrap().to_path_buf()
} else { } else {
new_path new_path
}; };
new_path new_path
} }

View File

@ -19,17 +19,3 @@ pub fn read_migrate_file(file: PathBuf) -> anyhow::Result<Vec<(PathBuf, PathBuf)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
Ok(migrations) Ok(migrations)
} }
pub fn read_migrate_file_left_side(file: PathBuf) -> anyhow::Result<Vec<PathBuf>> {
let contents = read_to_string(file)?;
let lines: Vec<String> = contents
.split('\n')
.map(|x| x.to_owned())
.filter(|x| x != "")
.collect();
let migrations = lines
.iter()
.map(|x| x.split(" -> ").collect::<Vec<&str>>()[0].into())
.collect::<Vec<_>>();
Ok(migrations)
}