diff --git a/CHANGELOG.md b/CHANGELOG.md index 114cd7a..4d6249e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [0.2.0] - 2024-06-16 + +### Added +- support for linux +- `list` command to get all aliases +- clap cli parser + ## [0.1.0] - 2024-05-24 ### Added diff --git a/Cargo.toml b/Cargo.toml index 6e3a67c..b706041 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "cliq" description = "open frequently accessed memorable shorten urls from cli" -version = "0.1.0" +version = "0.2.0" edition = "2021" authors = ["Santhosh Chinnasamy"] readme = "README.md" @@ -13,4 +13,6 @@ repository = "https://github.com/santhosh-chinnasamy/cliq.git" exclude = [".vscode"] [dependencies] +clap = { version = "4.5.4", features = ["derive"] } +dirs = "5.0.1" toml = "0.8.13" diff --git a/README.md b/README.md index 33d49b4..50f7791 100644 --- a/README.md +++ b/README.md @@ -39,5 +39,5 @@ cargo install --path . ## Supports - [x] Mac -- [ ] Linux +- [x] Linux - [ ] Windows diff --git a/src/alias.rs b/src/alias.rs new file mode 100644 index 0000000..fb9df52 --- /dev/null +++ b/src/alias.rs @@ -0,0 +1,67 @@ +use std::{fs, process::exit}; + +use toml::Table; + +const CONFIG_FOLDER: &'static str = ".config/cliq"; +const CONFIG_FILE: &'static str = "cliq.toml"; +const DEFAULT_CONFIG: &'static str = " +[links] +google = \"https://google.com\" +hub = \"https://github.com\" +lab = \"https://gitlab.com\" +"; + +fn config_file() -> String { + let home_dir = dirs::home_dir().unwrap().to_str().unwrap().to_string(); + let cliq_config = format!("{}/{}/{}", home_dir, CONFIG_FOLDER, CONFIG_FILE); + return cliq_config.to_string(); +} + +fn create_config() { + let home_dir = dirs::home_dir().unwrap().to_str().unwrap().to_string(); + let cliq_folder = format!("{}/{}", home_dir, CONFIG_FOLDER); + std::fs::create_dir_all(&cliq_folder).unwrap(); + + let cliq_config = format!("{}/{}", cliq_folder, CONFIG_FILE); + std::fs::File::create(&cliq_config).unwrap(); + + // add content to file + std::fs::write(cliq_config, DEFAULT_CONFIG).unwrap(); +} + +fn read_config() -> Table { + let is_config_exist = std::path::Path::new(&config_file()).exists(); + if !is_config_exist { + create_config(); + } + + let cliq_config = match fs::read_to_string(&config_file()) { + Ok(file) => file, + Err(e) => { + eprintln!("Error reading cliq.toml. \nCreate cliq.toml file under $HOME/.config/cliq folder. {}", e); + exit(1); + } + }; + + let cliq_data: Table = cliq_config.parse().unwrap(); + return cliq_data; +} + +pub fn links() -> Table { + let cliq_data = read_config(); + let links = cliq_data["links"].as_table().unwrap().clone(); + return links; +} + +pub fn link(alias: String) -> String { + let links = links(); + let link = match links.get(alias.as_str()) { + Some(link) => link.as_str().unwrap(), + None => { + eprintln!("alias not found in cliq config"); + exit(1); + } + }; + + return link.to_string(); +} diff --git a/src/heimdall.rs b/src/heimdall.rs new file mode 100644 index 0000000..edc0d3a --- /dev/null +++ b/src/heimdall.rs @@ -0,0 +1,26 @@ +use std::process::{exit, Command}; + +const MACOS: &str = "open"; +const LINUX: &str = "xdg-open"; + +fn program() -> String { + let os: &str = std::env::consts::OS; + let _program: &str = match os { + "macos" => MACOS, + _ => LINUX, + }; + + return _program.to_string(); +} + +pub fn open(url: String) { + let binding = program(); + let command = binding.as_str(); + + println!("Opening {}", url); + + Command::new(command).arg(url).spawn().unwrap_or_else(|_| { + eprintln!("{} command not found", command); + exit(127); + }); +} diff --git a/src/main.rs b/src/main.rs index 9f95591..c296292 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,47 +1,36 @@ -use std::process::{exit, Command}; -use std::{env, fs, path}; -use toml::Table; +use clap::{Parser, Subcommand}; +mod alias; +mod heimdall; + +#[derive(Parser)] +#[command(name = "cliq", version, about, author, long_about = None)] +struct Cliq { + #[command(subcommand)] + command: Commands, +} -fn main() { - let home_dir = match env::var_os("HOME") { - Some(val) => val, - None => { - eprintln!("$HOME dir not defined in the environment."); - exit(1); - } - }; +#[derive(Subcommand, Debug)] +enum Commands { + #[command(about = "get all aliases from cliq.toml")] + List, + // to handle aliases in toml file + #[command(external_subcommand)] + #[allow(dead_code)] + Options(Vec), +} - let file_path = path::Path::new(&home_dir).join(".config/cliq/cliq.toml"); +fn main() { + let cliq = Cliq::parse(); - let cliq_config = match fs::read_to_string(file_path) { - Ok(file) => file, - Err(e) => { - eprintln!("Error reading cliq.toml. \nCreate cliq.toml file under $HOME/.config/cliq folder. {}", e); - exit(1); + match &cliq.command { + Commands::List => { + let parsed_toml = alias::links(); + print!("{}", parsed_toml); } - }; - - let cliq_data: Table = cliq_config.parse().unwrap(); - let links = &cliq_data["links"].as_table().unwrap().clone(); - - let alias = env::args().nth(1).unwrap_or_else(|| { - eprintln!("no alias provided"); - exit(1); - }); - - let link = match links.get(alias.as_str()) { - Some(link) => link.as_str().unwrap(), - None => { - eprintln!("alias not found"); - exit(1); + Commands::Options(args) => { + let input = args.join(" "); + let url = alias::link(input); + heimdall::open(url); } - }; - - let program = "open"; - - Command::new(program).arg(link).spawn().unwrap_or_else(|_| { - eprintln!("{} command not found", program); - exit(127); - }); - println!("Opening {}", link); + } }