Start work on basic CLI features
+ Add basic logic for detecting conflicting configs, prompt to abort or continue + Create symlinks to configs + Basic usage is `kot path/to/dotfiles/` + Add `--backup-dir` option for setting directory for backing up configs + Default backup-dir to `backups/kapper` until finished testing + Add `--home-dir` option for setting location to install configs + Default home-dir setting to `dry-runs/kapper` until finished testing
This commit is contained in:
41
src/kot.rs
Normal file
41
src/kot.rs
Normal file
@@ -0,0 +1,41 @@
|
||||
/*##############################################################################
|
||||
## Author: Shaun Reed ##
|
||||
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
|
||||
## About: Root module for Linux configuration manager kot ##
|
||||
## ##
|
||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||
##############################################################################*/
|
||||
|
||||
pub mod cli;
|
||||
pub mod fs;
|
||||
pub mod io;
|
||||
|
||||
// =============================================================================
|
||||
// IMPLEMENTATION
|
||||
// =============================================================================
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
pub fn install_configs(args: & cli::Cli) -> std::io::Result<()> {
|
||||
// Get the configurations and their target installation paths
|
||||
// + Checks for conflicts and prompts user to abort or continue
|
||||
let config_map = fs::get_target_paths(&args)?;
|
||||
|
||||
// At this point there are either no conflicts or the user agreed to them
|
||||
for (config_path, target_path) in &config_map {
|
||||
println!("Installing config: {:?}\n+ At location: {:?}\n", config_path, target_path);
|
||||
|
||||
match std::os::unix::fs::symlink(config_path, target_path) {
|
||||
Ok(()) => (),
|
||||
Err(_e) => {
|
||||
match target_path.is_dir() {
|
||||
true => fs_extra::dir::remove(target_path),
|
||||
false => fs_extra::file::remove(target_path),
|
||||
};
|
||||
std::os::unix::fs::symlink(config_path, target_path)
|
||||
.expect(&format!("Unable to symlink config: {:?}", config_path));
|
||||
},
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
69
src/kot/cli.rs
Normal file
69
src/kot/cli.rs
Normal file
@@ -0,0 +1,69 @@
|
||||
/*##############################################################################
|
||||
## Author: Shaun Reed ##
|
||||
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
|
||||
## About: Wrapper for StructOpt crate functionality used by kot ##
|
||||
## ##
|
||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||
##############################################################################*/
|
||||
|
||||
use structopt::StructOpt;
|
||||
|
||||
// =============================================================================
|
||||
// STRUCTS
|
||||
// =============================================================================
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#[derive(Debug, StructOpt)]
|
||||
#[structopt(
|
||||
name="kot",
|
||||
about="CLI for managing Linux user configurations"
|
||||
)]
|
||||
pub struct Cli {
|
||||
#[structopt(
|
||||
help="Local or full path to user configurations to install",
|
||||
parse(from_os_str)
|
||||
)]
|
||||
pub configs_dir: std::path::PathBuf,
|
||||
|
||||
#[structopt(
|
||||
help="The location to attempt installation of user configurations",
|
||||
default_value="dry-runs/kapper", // TODO: Remove temp default value after tests
|
||||
// env = "HOME", // Default value to env variable $HOME
|
||||
long="home-dir",
|
||||
parse(from_os_str)
|
||||
)]
|
||||
pub install_dir: std::path::PathBuf,
|
||||
|
||||
#[structopt(
|
||||
help="The location to store backups for this user",
|
||||
default_value="backups/kapper",
|
||||
long="backup-dir",
|
||||
parse(from_os_str)
|
||||
)]
|
||||
pub backup_dir: std::path::PathBuf,
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// IMPLEMENTATION
|
||||
// =============================================================================
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// Augment implementation of from_args to limit scope of StructOpt
|
||||
// + Also enforces use of Cli::normalize()
|
||||
// https://docs.rs/structopt/0.3.23/src/structopt/lib.rs.html#1121-1126
|
||||
pub fn from_args() -> Cli {
|
||||
let s = Cli::from_clap(&Cli::clap().get_matches());
|
||||
s.normalize()
|
||||
}
|
||||
|
||||
impl Cli {
|
||||
// Helper function to normalize arguments passed to program
|
||||
pub fn normalize(mut self) -> Self {
|
||||
self.configs_dir = self.configs_dir.canonicalize().unwrap();
|
||||
self.install_dir = self.install_dir.canonicalize().unwrap();
|
||||
self.backup_dir = self.backup_dir.canonicalize().unwrap();
|
||||
self
|
||||
}
|
||||
}
|
||||
67
src/kot/fs.rs
Normal file
67
src/kot/fs.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
/*##############################################################################
|
||||
## Author: Shaun Reed ##
|
||||
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
|
||||
## About: Wrapper for std::fs functionality used by kot ##
|
||||
## ##
|
||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||
##############################################################################*/
|
||||
|
||||
// Allow the use of kot::fs::Path and kot::fs::PathBuf from std::path::
|
||||
pub use std::path::{Path, PathBuf};
|
||||
pub use std::collections::HashMap;
|
||||
|
||||
use std::fs;
|
||||
use fs_extra::dir;
|
||||
|
||||
// =============================================================================
|
||||
// IMPLEMENTATION
|
||||
// =============================================================================
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
fn backup_config(config_path: & PathBuf, backup_dir: & PathBuf) -> super::io::Result<()> {
|
||||
let mut backup_path = backup_dir.to_owned();
|
||||
backup_path.push(config_path.file_name().unwrap());
|
||||
match config_path.is_dir() {
|
||||
true => {
|
||||
let mut options = dir::CopyOptions::new();
|
||||
options.copy_inside = true;
|
||||
dir::move_dir(config_path, backup_path, &options)
|
||||
},
|
||||
false => {
|
||||
let options = fs_extra::file::CopyOptions::new();
|
||||
fs_extra::file::move_file(config_path, backup_path, &options)
|
||||
},
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Initialize and return a HashMap<config_dir, config_install_location>
|
||||
// Later used to check each install location for conflicts before installing
|
||||
pub fn get_target_paths(args: & super::cli::Cli) -> super::io::Result<HashMap<PathBuf, PathBuf>> {
|
||||
let mut config_map = HashMap::new();
|
||||
|
||||
let mut config_target = args.install_dir.to_owned();
|
||||
for config_entry in fs::read_dir(&args.configs_dir)? {
|
||||
match config_entry {
|
||||
Err(err) => return Err(err),
|
||||
Ok(entry) => {
|
||||
config_target.push(entry.file_name());
|
||||
|
||||
if config_target.exists() {
|
||||
match super::io::prompt(format!("Configuration already exists: {:?}\nAbort? Enter y/n or Y/N: ", config_target)) {
|
||||
true => return Err(std::io::Error::from(std::io::ErrorKind::AlreadyExists)),//panic!("User abort"),
|
||||
false => backup_config(&config_target, &args.backup_dir).ok(), // TODO: Backup colliding configs
|
||||
};
|
||||
};
|
||||
|
||||
config_map.entry(entry.path().to_owned())
|
||||
.or_insert(config_target.to_owned());
|
||||
|
||||
config_target.pop();
|
||||
},
|
||||
}
|
||||
|
||||
}
|
||||
Ok(config_map)
|
||||
}
|
||||
30
src/kot/io.rs
Normal file
30
src/kot/io.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
/*##############################################################################
|
||||
## Author: Shaun Reed ##
|
||||
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
|
||||
## About: Wrapper for std::io functionality used by kot ##
|
||||
## ##
|
||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||
##############################################################################*/
|
||||
|
||||
// Allow use of kot::io::Result
|
||||
pub use std::io::Result;
|
||||
|
||||
use std::io;
|
||||
|
||||
// =============================================================================
|
||||
// IMPLEMENTATION
|
||||
// =============================================================================
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
pub fn prompt(msg: String) -> bool {
|
||||
println!("{}", msg);
|
||||
let mut reply = String::new();
|
||||
io::stdin().read_line(&mut reply)
|
||||
.expect("Failed to read user input");
|
||||
match reply.trim() {
|
||||
"y" | "Y" => true,
|
||||
"n" | "N" => false,
|
||||
_ => prompt("Please enter y/n or Y/N\n".to_owned()),
|
||||
}
|
||||
}
|
||||
25
src/main.rs
25
src/main.rs
@@ -1,3 +1,26 @@
|
||||
/*##############################################################################
|
||||
## Author: Shaun Reed ##
|
||||
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
|
||||
## About: Main entry point for Linux configuration manager kot ##
|
||||
## ##
|
||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||
##############################################################################*/
|
||||
|
||||
mod kot;
|
||||
|
||||
// =============================================================================
|
||||
// MAIN ENTRY-POINT
|
||||
// =============================================================================
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
let args = kot::cli::from_args();
|
||||
println!("args: {:?}\n", args);
|
||||
|
||||
match kot::install_configs(&args) {
|
||||
Err(e) => println!("Error: {:?}\n+ Configs used: {:?}\n+ Install directory: {:?}\n",
|
||||
e.kind(), args.configs_dir, args.install_dir),
|
||||
Ok(()) => (),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user