website/scripts/docsmg: Bug fixes (#10668)

* add docsmg tool

* moved to the correct scripts directory

* removed test files

* added install script and readme draft to docsmg

* fix install script

* fixed issues

* Revert "fixed issues"

This reverts commit a51192025f.

* Revert "Revert "fixed issues""

This reverts commit ab68918fea.

* added dotenv and updated readme

* fixed install script
This commit is contained in:
Bama
2024-07-28 21:32:29 -05:00
committed by GitHub
parent 270c9661a1
commit ca7705689d
8 changed files with 71 additions and 77 deletions

View File

@ -17,15 +17,6 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "anstream"
version = "0.6.14"
@ -171,11 +162,16 @@ dependencies = [
"anyhow",
"clap",
"colored",
"lazy_static",
"regex",
"dotenv",
"tokio",
]
[[package]]
name = "dotenv"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
[[package]]
name = "gimli"
version = "0.29.0"
@ -254,35 +250,6 @@ dependencies = [
"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]]
name = "rustc-demangle"
version = "0.1.24"

View File

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

View File

@ -2,6 +2,14 @@
## Steps to install
1. Install [rustup](https://rustup.rs/) if its not already installed
2. Update to the latest version of rust if you dont already have it with `rustup update`
3. Install the cli tool with `curl https://raw.githubusercontent.com/goauthentik/authentik/main/website/scripts/docsmg/install.sh | sh`
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`
2. Install the cli tool with `cargo install --git https://github.com/goauthentik/authentik --bin docsmg`
## Steps to use
1. Generate a migratefile with `docsmg generate >> migratefile`
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`
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,4 +1,5 @@
cargo install --git https://github.com/goauthentik/authentik
sudo wget https://raw.githubusercontent.com/goauthentik/authentik/main/website/scripts/docsmg/m.bash -O /bin/map
sudo wget https://raw.githubusercontent.com/goauthentik/authentik/main/website/scripts/docsmg/mcomplete.bash -O /bin/mapcomplete
sudo chmod 755 /bin/map
curl https://raw.githubusercontent.com/goauthentik/authentik/main/website/scripts/docsmg/mcomplete.bash >> ~/.zshrc
echo "source mapcomplete" >> ~/.zshrc

View File

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

View File

@ -43,6 +43,7 @@ enum Commands {
}
fn main() {
let _ = dotenv::dotenv();
let cli = Cli::parse();
match cli.command {

View File

@ -1,9 +1,10 @@
use std::{
collections::HashMap, ffi::OsStr, fmt::format, fs::{read_to_string, write}, path::{Component, Path, PathBuf}
ffi::OsStr,
fs::{read_to_string, write},
path::PathBuf,
};
use colored::Colorize;
use lazy_static::lazy_static;
use crate::{migratefile::read_migrate_file, recurse_directory};
@ -95,31 +96,38 @@ fn move_files(
}
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());
for file in files {
let relative_file = file.strip_prefix(migrate_path.clone()).unwrap().to_path_buf();
let relative_file = file
.strip_prefix(migrate_path.clone())
.unwrap()
.to_path_buf();
let mut contents = match read_to_string(file.clone()) {
Ok(i) => i,
Err(_) => continue,
};
let mut replace = vec![];
for successful_move in &successful_moves {
if migrate_path.join(successful_move.0.clone()).canonicalize().unwrap()
== file.clone().canonicalize().unwrap() {
if migrate_path
.join(successful_move.0.clone())
.canonicalize()
.unwrap()
== file.clone().canonicalize().unwrap()
{
continue;
}
let new_successful_move_from = 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_from =
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());
replace.push((new_successful_move_from, new_successful_move_to));
}
for i in replace {
println!("{} : {} -> {}", file.display(), i.0.display(), i.1.display());
contents = contents.replace(&format!("({})", i.0.display()), &format!("({})", i.1.display()));
contents = contents.replace(
&format!("({})", i.0.display()),
&format!("({})", i.1.display()),
);
}
write(file, contents).unwrap();
}
@ -132,21 +140,16 @@ fn make_path_relative(path: PathBuf, relative_to: PathBuf) -> PathBuf {
loop {
if path_components.len() <= subdirs {
break;
} else if path_components[subdirs]
!= relative_to_components[subdirs]
{
} else if path_components[subdirs] != relative_to_components[subdirs] {
break;
}
subdirs += 1;
}
let new_path = &path_components[subdirs..]
.iter()
.collect::<PathBuf>();
let backouts =
(0..relative_to_components.len() - subdirs - 1)
.map(|_| PathBuf::from(".."))
.reduce(|acc, e| acc.join(e))
.unwrap_or(PathBuf::from(""));
let new_path = &path_components[subdirs..].iter().collect::<PathBuf>();
let backouts = (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());
let new_path = backouts.join(new_path);
let new_path = if new_path
@ -162,13 +165,14 @@ fn make_path_relative(path: PathBuf, relative_to: PathBuf) -> PathBuf {
new_path
};
let new_path = if new_path.file_name() == Some(OsStr::new("index.md")) || new_path.file_name() == Some(OsStr::new("index.mdx")) {
let new_path = if new_path.file_name() == Some(OsStr::new("index.md"))
|| new_path.file_name() == Some(OsStr::new("index.mdx"))
{
new_path.parent().unwrap().to_path_buf()
} else {
new_path
};
new_path
}

View File

@ -19,3 +19,17 @@ pub fn read_migrate_file(file: PathBuf) -> anyhow::Result<Vec<(PathBuf, PathBuf)
.collect::<Vec<_>>();
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)
}