From 29e8d40fc4fa13fd28626abc6278baed26194b00 Mon Sep 17 00:00:00 2001 From: Santhosh Chinnasamy Date: Sun, 16 Jun 2024 22:49:01 +0530 Subject: [PATCH 1/3] add support to linux - uses clap parser --- CHANGELOG.md | 6 ++++ Cargo.toml | 4 ++- README.md | 2 +- src/alias.rs | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 66 ++++++++++++++----------------------- 5 files changed, 126 insertions(+), 44 deletions(-) create mode 100644 src/alias.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 114cd7a..3eb2544 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## [0.2.0] - 2024-06-16 + +### Added +- support for linux +- 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..01a2dc2 --- /dev/null +++ b/src/alias.rs @@ -0,0 +1,92 @@ +use std::{ + fs, + process::{exit, Command}, +}; + +use toml::Table; + +const MACOS: &str = "open"; +const LINUX: &str = "xdg-open"; +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 program() -> String { + let os: &str = std::env::consts::OS; + let _program: &str = match os { + "macos" => MACOS, + _ => LINUX, + }; + + return _program.to_string(); +} + +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 link(alias: String) -> String { + let cliq_data = read_config(); + let links = cliq_data["links"].as_table().unwrap().clone(); + 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(); +} + +pub fn open(url: String) { + let binding = program(); + let command_name = binding.as_str(); + + println!("Opening {}", url); + + Command::new(command_name) + .arg(url) + .spawn() + .unwrap_or_else(|_| { + eprintln!("{} command not found", command_name); + exit(127); + }); +} diff --git a/src/main.rs b/src/main.rs index 9f95591..81f35e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,47 +1,29 @@ -use std::process::{exit, Command}; -use std::{env, fs, path}; -use toml::Table; - -fn main() { - let home_dir = match env::var_os("HOME") { - Some(val) => val, - None => { - eprintln!("$HOME dir not defined in the environment."); - exit(1); - } - }; - - let file_path = path::Path::new(&home_dir).join(".config/cliq/cliq.toml"); - - 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); - } - }; +use clap::{Parser, Subcommand}; +mod alias; + +#[derive(Parser)] +#[command(name = "cliq", version, about , author, long_about = None)] +struct Cliq { + #[command(subcommand)] + command: Commands, +} - let cliq_data: Table = cliq_config.parse().unwrap(); - let links = &cliq_data["links"].as_table().unwrap().clone(); +#[derive(Subcommand, Debug)] +enum Commands { + // to handle aliases in toml file + #[command(external_subcommand)] + #[allow(dead_code)] + Options(Vec), +} - let alias = env::args().nth(1).unwrap_or_else(|| { - eprintln!("no alias provided"); - exit(1); - }); +fn main() { + let cliq = Cliq::parse(); - let link = match links.get(alias.as_str()) { - Some(link) => link.as_str().unwrap(), - None => { - eprintln!("alias not found"); - exit(1); + match &cliq.command { + Commands::Options(args) => { + let input = args.join(" "); + let url = alias::link(input); + alias::open(url); } - }; - - let program = "open"; - - Command::new(program).arg(link).spawn().unwrap_or_else(|_| { - eprintln!("{} command not found", program); - exit(127); - }); - println!("Opening {}", link); + } } From 64876dfbe174e518f2b568e66373caf193a5d41d Mon Sep 17 00:00:00 2001 From: Santhosh Chinnasamy Date: Sun, 16 Jun 2024 23:06:26 +0530 Subject: [PATCH 2/3] add list command to get all aliases --- CHANGELOG.md | 1 + src/alias.rs | 7 ++++++- src/main.rs | 8 +++++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3eb2544..4d6249e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Added - support for linux +- `list` command to get all aliases - clap cli parser ## [0.1.0] - 2024-05-24 diff --git a/src/alias.rs b/src/alias.rs index 01a2dc2..81ada8a 100644 --- a/src/alias.rs +++ b/src/alias.rs @@ -62,9 +62,14 @@ fn read_config() -> Table { return cliq_data; } -pub fn link(alias: String) -> String { +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 => { diff --git a/src/main.rs b/src/main.rs index 81f35e1..797e1ce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ use clap::{Parser, Subcommand}; mod alias; #[derive(Parser)] -#[command(name = "cliq", version, about , author, long_about = None)] +#[command(name = "cliq", version, about, author, long_about = None)] struct Cliq { #[command(subcommand)] command: Commands, @@ -10,6 +10,8 @@ struct Cliq { #[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)] @@ -20,6 +22,10 @@ fn main() { let cliq = Cliq::parse(); match &cliq.command { + Commands::List => { + let parsed_toml = alias::links(); + print!("{}", parsed_toml); + } Commands::Options(args) => { let input = args.join(" "); let url = alias::link(input); From 46dd047e06e86c17a0c2c57136fc11d6df66f57d Mon Sep 17 00:00:00 2001 From: Santhosh Chinnasamy Date: Sun, 16 Jun 2024 23:19:09 +0530 Subject: [PATCH 3/3] refactor files and functions --- src/alias.rs | 32 +------------------------------- src/heimdall.rs | 26 ++++++++++++++++++++++++++ src/main.rs | 3 ++- 3 files changed, 29 insertions(+), 32 deletions(-) create mode 100644 src/heimdall.rs diff --git a/src/alias.rs b/src/alias.rs index 81ada8a..fb9df52 100644 --- a/src/alias.rs +++ b/src/alias.rs @@ -1,12 +1,7 @@ -use std::{ - fs, - process::{exit, Command}, -}; +use std::{fs, process::exit}; use toml::Table; -const MACOS: &str = "open"; -const LINUX: &str = "xdg-open"; const CONFIG_FOLDER: &'static str = ".config/cliq"; const CONFIG_FILE: &'static str = "cliq.toml"; const DEFAULT_CONFIG: &'static str = " @@ -16,16 +11,6 @@ hub = \"https://github.com\" lab = \"https://gitlab.com\" "; -fn program() -> String { - let os: &str = std::env::consts::OS; - let _program: &str = match os { - "macos" => MACOS, - _ => LINUX, - }; - - return _program.to_string(); -} - 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); @@ -80,18 +65,3 @@ pub fn link(alias: String) -> String { return link.to_string(); } - -pub fn open(url: String) { - let binding = program(); - let command_name = binding.as_str(); - - println!("Opening {}", url); - - Command::new(command_name) - .arg(url) - .spawn() - .unwrap_or_else(|_| { - eprintln!("{} command not found", command_name); - exit(127); - }); -} 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 797e1ce..c296292 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use clap::{Parser, Subcommand}; mod alias; +mod heimdall; #[derive(Parser)] #[command(name = "cliq", version, about, author, long_about = None)] @@ -29,7 +30,7 @@ fn main() { Commands::Options(args) => { let input = args.join(" "); let url = alias::link(input); - alias::open(url); + heimdall::open(url); } } }