-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from santhosh-chinnasamy/feat/clap
add support to linux
- Loading branch information
Showing
6 changed files
with
134 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,5 +39,5 @@ cargo install --path . | |
## Supports | ||
|
||
- [x] Mac | ||
- [ ] Linux | ||
- [x] Linux | ||
- [ ] Windows |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |