+ 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 testingmaster
parent
cb0e627a42
commit
eadf1fd0a2
@ -0,0 +1,3 @@
|
||||
[submodule "dotfiles/dot"]
|
||||
path = dotfiles/dot
|
||||
url = https://gitlab.com/shaunrd0/dot
|
@ -1,8 +1,11 @@
|
||||
[package]
|
||||
name = "dotk"
|
||||
name = "kot"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
# See more keys and their definitions at
|
||||
# https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
structopt = "0.3.23"
|
||||
fs_extra = "1.2.0"
|
||||
|
@ -1,6 +1,26 @@
|
||||
#### dotk
|
||||
#### kot
|
||||
|
||||
Learning to program in Rust by making myself a Linux CLI tool to help manage dotfiles and configurations.
|
||||
|
||||
There are many other tools to manage dotfiles that work just fine. For now, this is intended to be just for my own learning / use and not a general dotfiles management utility. If I enjoy working on it and get good use out of it, I may rethink this later.
|
||||
There are many other tools to manage dotfiles that work just fine. For now, this is intended to be just for my own learning / use and not a general dotfiles management utility.
|
||||
|
||||
```bash
|
||||
[kapper@kubuntu ~]$./kot --help
|
||||
kot 0.1.0
|
||||
CLI utility for managing Linux user configurations
|
||||
|
||||
USAGE:
|
||||
kot [OPTIONS] <config>
|
||||
|
||||
FLAGS:
|
||||
-h, --help Prints help information
|
||||
-V, --version Prints version information
|
||||
|
||||
OPTIONS:
|
||||
--backup-dir <backup-dir> The location to store backups for this user [default: backups/kapper]
|
||||
--home-dir <install-dir> The location to attempt installation of user configurations [default: dry-
|
||||
runs/kapper]
|
||||
|
||||
ARGS:
|
||||
<config> Local or full path to user configurations to install
|
||||
```
|
||||
|
@ -0,0 +1 @@
|
||||
Subproject commit 7877117d5bd413ecf35c86efb4514742d8136843
|
@ -0,0 +1 @@
|
||||
Subproject commit 7877117d5bd413ecf35c86efb4514742d8136843
|
@ -0,0 +1 @@
|
||||
Subproject commit 7877117d5bd413ecf35c86efb4514742d8136843
|
@ -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(())
|
||||
}
|
@ -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
|
||||
}
|
||||
}
|
@ -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)
|
||||
}
|
@ -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()),
|
||||
}
|
||||
}
|
@ -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(()) => (),
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue