Skip to content

Commit

Permalink
Merge pull request #1 from santhosh-chinnasamy/feat/clap
Browse files Browse the repository at this point in the history
add support to linux
  • Loading branch information
santhosh-chinnasamy authored Jun 16, 2024
2 parents 3f29469 + 46dd047 commit 68ffbed
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 43 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ cargo install --path .
## Supports

- [x] Mac
- [ ] Linux
- [x] Linux
- [ ] Windows
67 changes: 67 additions & 0 deletions src/alias.rs
Original file line number Diff line number Diff line change
@@ -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();
}
26 changes: 26 additions & 0 deletions src/heimdall.rs
Original file line number Diff line number Diff line change
@@ -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);
});
}
71 changes: 30 additions & 41 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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<String>),
}

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);
}
}

0 comments on commit 68ffbed

Please sign in to comment.