Skip to content

Commit

Permalink
feat(config): init
Browse files Browse the repository at this point in the history
  • Loading branch information
litingyes committed Apr 2, 2024
1 parent 6ac44fd commit 7979519
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 1 deletion.
135 changes: 135 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ categories = ["command-line-utilities"]
[dependencies]
clap = { version = "4.5.4", features = ["derive"] }
console = "0.15.8"
dirs = "5.0.1"
inquire = "0.7.4"
toml_edit = "0.22.9"
102 changes: 102 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
extern crate dirs;

use console::style;
use std::path::PathBuf;
use std::{env, fs};
use toml_edit::DocumentMut;

const CONFIG_FILE: &'static str = "liting.toml";

fn get_global_config() -> Result<DocumentMut, ()> {
if let Some(global_config_dir) = dirs::config_dir() {
let mut path_buf = PathBuf::new();
path_buf.push(global_config_dir);
path_buf.push(CONFIG_FILE);
let global_config_file_path = path_buf.as_path();
println!(
"The global config file path: {}.",
style(global_config_file_path.to_string_lossy()).yellow()
);

if global_config_file_path.exists() {
match fs::read_to_string(global_config_file_path) {
Ok(global_config_str) => {
let global_config = global_config_str
.parse::<DocumentMut>()
.expect("invalid global config");

Ok(global_config)
}
Err(e) => {
eprintln!("Error read global config file: {}", e);
Err(())
}
}
} else {
fs::File::create_new(global_config_file_path)
.expect("Error create global config file.");
println!("Cannot find existing global config file, so create one.");
Ok(DocumentMut::new())
}
} else {
eprintln!("Error read global config file.");
Err(())
}
}

fn get_local_config() -> Result<DocumentMut, ()> {
if let Some(local_config_dir) = env::current_dir() {
let mut path_buf = PathBuf::new();
path_buf.push(local_config_dir);
path_buf.push(CONFIG_FILE);
let local_config_file_path = path_buf.as_path();
println!(
"The local config file path: {}",
style(local_config_file_path.to_string_lossy()).yellow()
);

if local_config_file_path.exists() {
match fs::read_to_string(local_config_file_path) {
Ok(local_config_str) => {
let local_config = local_config_str
.parse::<DocumentMut>()
.expect("invalid local config.");

Ok(local_config)
}
Err(e) => {
eprintln!("Error read local config file: {}.", e);
Err(())
}
}
} else {
println!("Cannot find existing local config file.");
Err(())
}
} else {
eprintln!("Error read global config file.");
Err(())
}
}

pub fn list_config() {
let global_config = get_global_config();
match global_config {
Ok(config) => {
println!("{}", config.to_string())
}
Err(_e) => {
eprintln!("Error list config")
}
}

let local_config = get_local_config();
match local_config {
Ok(config) => {
println!("{}", config.to_string())
}
Err(_e) => {
eprintln!("Error list config")
}
}
}
13 changes: 12 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clap::{command, Parser, Subcommand};
use clap::{arg, command, Parser, Subcommand};
mod commit;
mod config;

#[derive(Parser)]
#[command(version, author, about)]
Expand All @@ -14,6 +15,11 @@ struct Cli {
#[derive(Subcommand)]
enum Commands {
Commit {},

Config {
#[arg(long, short)]
list: bool,
},
}

fn main() {
Expand All @@ -23,6 +29,11 @@ fn main() {
Some(Commands::Commit {}) => {
commit::handle_commit();
}
Some(Commands::Config { list }) => {
if *list {
config::list_config()
}
}
None => {}
}
}

0 comments on commit 7979519

Please sign in to comment.