Move repo into subfolder, ran clippy

This commit is contained in:
Mutzi 2023-01-20 17:21:47 +01:00
parent 4550f1a7cd
commit a6dfe600e2
8 changed files with 109 additions and 32 deletions

View File

@ -2,5 +2,6 @@
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" /> <mapping directory="" vcs="Git" />
<mapping directory="$PROJECT_DIR$/repo" vcs="Git" />
</component> </component>
</project> </project>

2
Cargo.lock generated
View File

@ -55,7 +55,7 @@ dependencies = [
] ]
[[package]] [[package]]
name = "dotfiles_loader" name = "dotfiles_installer"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"dialoguer", "dialoguer",

76
rustfmt.toml Normal file
View File

@ -0,0 +1,76 @@
max_width = 120
hard_tabs = false
tab_spaces = 4
newline_style = "Unix"
indent_style = "Block"
use_small_heuristics = "Default"
fn_call_width = 60
attr_fn_like_width = 70
struct_lit_width = 18
struct_variant_width = 35
array_width = 60
chain_width = 100
single_line_if_else_max_width = 50
wrap_comments = false
format_code_in_doc_comments = false
doc_comment_code_block_width = 100
comment_width = 80
normalize_comments = false
normalize_doc_attributes = false
format_strings = false
format_macro_matchers = false
format_macro_bodies = true
hex_literal_case = "Preserve"
empty_item_single_line = true
struct_lit_single_line = true
fn_single_line = true
where_single_line = true
imports_indent = "Block"
imports_layout = "HorizontalVertical"
imports_granularity = "Crate"
group_imports = "StdExternalCrate"
reorder_imports = true
reorder_modules = true
reorder_impl_items = true
type_punctuation_density = "Wide"
space_before_colon = false
space_after_colon = true
spaces_around_ranges = false
binop_separator = "Front"
remove_nested_parens = true
combine_control_expr = true
short_array_element_width_threshold = 10
overflow_delimited_expr = true
struct_field_align_threshold = 0
enum_discrim_align_threshold = 0
match_arm_blocks = false
match_arm_leading_pipes = "Never"
force_multiline_blocks = false
fn_args_layout = "Tall"
brace_style = "PreferSameLine"
control_brace_style = "AlwaysSameLine"
trailing_semicolon = true
trailing_comma = "Never"
match_block_trailing_comma = false
blank_lines_upper_bound = 1
blank_lines_lower_bound = 0
edition = "2021"
version = "Two"
inline_attribute_width = 0
format_generated_files = true
merge_derives = true
use_try_shorthand = true
use_field_init_shorthand = true
force_explicit_abi = true
condense_wildcard_suffixes = false
color = "Auto"
required_version = "1.5.1"
unstable_features = false
disable_all_formatting = false
skip_children = false
hide_parse_errors = false
error_on_line_overflow = false
error_on_unformatted = false
ignore = []
emit_mode = "Files"
make_backup = false

View File

@ -14,20 +14,20 @@ pub struct ModToml {
} }
pub fn parse() -> Result<ModToml, String> { pub fn parse() -> Result<ModToml, String> {
let mut files: ModToml = toml::from_str(std::fs::read_to_string("mod.toml").unwrap().as_str()) let mut files: ModToml = toml::from_str(std::fs::read_to_string("repo/mod.toml").unwrap().as_str())
.map_err(|e| format!("Error while parsing files.toml:\n{}", e.to_string()))?; .map_err(|e| format!("Error while parsing files.toml:\n{}", e))?;
let home_path = std::env::var("HOME").unwrap(); let home_path = std::env::var("HOME").unwrap();
for (_, m) in files.modules.iter_mut() { for (_, m) in files.modules.iter_mut() {
m.content = m.content.iter().map(|(n, p)| ( m.content = m.content.iter().map(|(n, p)| (
n.clone(), n.clone(),
p.replace("~", home_path.as_str()) p.replace('~', home_path.as_str())
)).collect() )).collect()
} }
let base = std::path::Path::new("repo");
for (name, _path) in files.modules.iter().flat_map(|(_, m)| m.content.iter()) { for (name, _path) in files.modules.iter().flat_map(|(_, m)| m.content.iter()) {
let p = std::path::Path::new(name); if !base.join(name).exists() {
if !p.exists() {
return Err(format!("Missing module entry {}", name)); return Err(format!("Missing module entry {}", name));
} }
} }

View File

@ -8,21 +8,21 @@ use strum::{EnumIter, IntoEnumIterator};
#[derive(Debug, Clone, EnumIter)] #[derive(Debug, Clone, EnumIter)]
enum MainMenu { enum MainMenu {
INSTALL, Install,
COLLECT, Collect,
DIFF, Diff,
UPLOAD, Upload,
QUIT Quit
} }
impl ToString for MainMenu { impl ToString for MainMenu {
fn to_string(&self) -> String { fn to_string(&self) -> String {
match self { match self {
MainMenu::INSTALL => "Pull and install files", MainMenu::Install => "Pull and install files",
MainMenu::COLLECT => "Collect files", MainMenu::Collect => "Collect files",
MainMenu::DIFF => "View git diff", MainMenu::Diff => "View git diff",
MainMenu::UPLOAD => "(Commit) and push to git", MainMenu::Upload => "(Commit) and push to git",
MainMenu::QUIT => "Exit" MainMenu::Quit => "Exit"
}.to_string() }.to_string()
} }
} }
@ -37,7 +37,7 @@ fn main() {
} }
}; };
if !std::path::Path::new("mod.toml").exists() { if !std::path::Path::new("repo/mod.toml").exists() {
println!("'mod.toml' doesn't exist!"); println!("'mod.toml' doesn't exist!");
return; return;
} }
@ -54,7 +54,7 @@ fn main() {
'main_loop: loop { 'main_loop: loop {
let res = prompt::select(Some("What do you want to do?"), MainMenu::iter().collect()); let res = prompt::select(Some("What do you want to do?"), MainMenu::iter().collect());
match res { match res {
MainMenu::INSTALL => { MainMenu::Install => {
if let Err(err) = repository::pull_repo(&repo) { if let Err(err) = repository::pull_repo(&repo) {
println!("Failed to pull repo!"); println!("Failed to pull repo!");
println!("{}", err); println!("{}", err);
@ -62,10 +62,10 @@ fn main() {
} }
operations::install(&mods); operations::install(&mods);
}, },
MainMenu::COLLECT => operations::collect(&mods), MainMenu::Collect => operations::collect(&mods),
MainMenu::DIFF => { std::process::Command::new("git").arg("diff").spawn().unwrap().wait().unwrap(); }, MainMenu::Diff => { std::process::Command::new("git").arg("diff").current_dir("repo").spawn().unwrap().wait().unwrap(); },
MainMenu::UPLOAD => operations::upload(&repo), MainMenu::Upload => operations::upload(&repo),
MainMenu::QUIT => break 'main_loop MainMenu::Quit => break 'main_loop
} }
} }
} }

View File

@ -12,7 +12,7 @@ impl ToString for NamedModule {
fn install_mod(m: NamedModule) { fn install_mod(m: NamedModule) {
let spinner = indicatif::ProgressBar::new_spinner().with_message(format!("Installing module {}...", m.0)); let spinner = indicatif::ProgressBar::new_spinner().with_message(format!("Installing module {}...", m.0));
'content_iter: for entry in m.1.content { 'content_iter: for entry in m.1.content {
let src = std::path::Path::new(entry.0.as_str()); let src = std::path::Path::new("repo").join(&entry.0);
if src.is_file() { if src.is_file() {
if let Err(err) = std::fs::copy(src, entry.1) { if let Err(err) = std::fs::copy(src, entry.1) {
println!("Failed to copy {}:\n{}", entry.0, err); println!("Failed to copy {}:\n{}", entry.0, err);
@ -40,7 +40,7 @@ fn install_mod(m: NamedModule) {
let _ = std::fs::remove_dir(dir); let _ = std::fs::remove_dir(dir);
spinner.tick(); spinner.tick();
} }
if let Err(err) = crate::utils::copy_dir(src, dst, &spinner) { if let Err(err) = crate::utils::copy_dir(&src, dst, &spinner) {
println!("Failed to copy directory:\n{}", err); println!("Failed to copy directory:\n{}", err);
} }
} }
@ -50,7 +50,7 @@ fn install_mod(m: NamedModule) {
fn collect_mod(m: NamedModule) { fn collect_mod(m: NamedModule) {
let spinner = indicatif::ProgressBar::new_spinner().with_message(format!("Collecting module {}...", m.0)); let spinner = indicatif::ProgressBar::new_spinner().with_message(format!("Collecting module {}...", m.0));
'content_iter: for entry in m.1.content { 'content_iter: for entry in m.1.content {
let dst = std::path::Path::new(entry.0.as_str()); let dst = std::path::Path::new("repo").join(entry.0.as_str());
let src = std::path::Path::new(entry.1.as_str()); let src = std::path::Path::new(entry.1.as_str());
if src.is_file() { if src.is_file() {
if let Err(err) = std::fs::copy(src, dst) { if let Err(err) = std::fs::copy(src, dst) {
@ -58,8 +58,8 @@ fn collect_mod(m: NamedModule) {
} }
spinner.tick(); spinner.tick();
} else { } else {
let _ = std::fs::remove_dir_all(dst); let _ = std::fs::remove_dir_all(&dst);
if let Err(err) = std::fs::create_dir_all(dst) { if let Err(err) = std::fs::create_dir_all(&dst) {
println!("Failed to create directory {}:\n{}", entry.0, err); println!("Failed to create directory {}:\n{}", entry.0, err);
continue 'content_iter; continue 'content_iter;
} }
@ -70,7 +70,7 @@ fn collect_mod(m: NamedModule) {
}; };
for dir in to_copy.directories { for dir in to_copy.directories {
std::fs::create_dir_all(dst.join(&dir)).unwrap(); std::fs::create_dir_all(dst.join(dir)).unwrap();
spinner.tick(); spinner.tick();
} }

View File

@ -10,7 +10,7 @@ fn generate_callbacks(use_ssh_agent: bool, pb: &mut indicatif::ProgressBar) -> g
callbacks.credentials(|_, username, _| git2::Cred::ssh_key( callbacks.credentials(|_, username, _| git2::Cred::ssh_key(
username.unwrap(), username.unwrap(),
None, None,
Path::new(&format!("{}/.ssh/id_rsa", std::env::var("HOME").unwrap_or(String::new()))), Path::new(&format!("{}/.ssh/id_rsa", std::env::var("HOME").unwrap_or_default())),
None None
)); ));
} }
@ -44,7 +44,7 @@ fn _clone_repo(callbacks: git2::RemoteCallbacks) -> Result<Repository, git2::Err
builder.clone( builder.clone(
"git@ssh.gitlab.mattv.de:root/dotfiles.git", "git@ssh.gitlab.mattv.de:root/dotfiles.git",
Path::new(".") Path::new("repo")
) )
} }
@ -101,7 +101,7 @@ pub fn push_repo(repo: &Repository) -> Result<(), git2::Error> {
} }
pub fn open_repo() -> Result<Repository, String> { pub fn open_repo() -> Result<Repository, String> {
Repository::open(".") Repository::open("repo")
.or_else(|_| clone_repo()) .or_else(|_| clone_repo())
.map_err(|e| e.message().to_string()) .map_err(|e| e.message().to_string())
} }

View File

@ -1,6 +1,6 @@
use fs_extra::dir::DirContent; use fs_extra::dir::DirContent;
pub fn get_dir_content_filtered(p: &std::path::Path, ignored: &Vec<String>) -> Result<DirContent, String> { pub fn get_dir_content_filtered(p: &std::path::Path, ignored: &[String]) -> Result<DirContent, String> {
let p = p.canonicalize().map_err(|e| e.to_string())?; let p = p.canonicalize().map_err(|e| e.to_string())?;
let base_path_len = p.to_str().unwrap().len() + 1; let base_path_len = p.to_str().unwrap().len() + 1;