Skip to content

Commit

Permalink
feat: added a config file
Browse files Browse the repository at this point in the history
  • Loading branch information
cecilia-sanare committed Feb 22, 2024
1 parent 071bcad commit fe470cc
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 37 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name: CI
on:
push:
branches: [ "main" ]
tags: '*'
pull_request:
branches: [ "main" ]

Expand Down
12 changes: 12 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ version = "0.1.0"
edition = "2021"

[dependencies]
home = "0.5.9"
log = "0.4.20"
open = "5.0.1"
pretty_env_logger = "0.5.0"
reqwest = "0.11.24"
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.114"
tokio = { version = "1.36.0", features = ["rt", "macros" ]}
url = "2.5.0"
35 changes: 10 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,23 @@

### Installation

_TODO_
See [Releases](https://github.com/rain-cafe/smart-open/releases/latest)

### Example Usage

```sh
# ############ #
# File Browser #
# ############ #
$ smart-open --help

# Inside of a regular directory
$ so # Opens native file explorer at the current directory
smart-open - an intelligent and pluggable version of open

# Specifying a sub directory
$ so ./src # Opens native file explorer at the current directory
Usage:

# ################ #
# Git Repositories #
# ################ #
smart-open opens a browser at the git repo if one is present, otherwise opens the file explorer
smart-open https://rains.cafe opens a browser at https://rains.cafe
smart-open rains.cafe opens a browser at https://rains.cafe
smart-open /home opens the file explorer at /home

# Inside a git repo (smart-open in this example)
$ so # Default Browser: https://github.com/rain-cafe/smart-open
Configs:

# #### #
# URLs #
# #### #

# With a simplified url
$ so rains.cafe # Default Browser: https://rains.cafe

# With a fully qualified url
$ so https://rains.cafe # Default Browser: https://rains.cafe

# With a deep url
$ so https://github.com/rain-cafe/smart-open # Default Browser: https://github.com/rain-cafe/smart-open
Update the config located at /home/ceci/.config/smart-open.json
```
38 changes: 27 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{error::Error, process::exit};
use std::{error::Error, path::Path, process::exit};
use log::{error, trace};

mod utils;
Expand All @@ -10,18 +10,34 @@ async fn main() -> Result<(), Box<dyn Error>> {

let mut args = std::env::args();

let uri = args.nth(1).unwrap_or(String::from("."));
let name = args.next().unwrap();
let uri = args.next().unwrap_or(String::from("."));

let uri = parsers::parse(&uri, &vec![
String::from("url"),
String::from("git"),
String::from("file"),
]).await.expect("Unable to parse URI");
let config = utils::config::read();

if uri == "--help" {
let name = Path::new(&name).file_name().unwrap().to_str().unwrap();

if let Err(e) = open::that(&uri) {
error!("Failed to open uri! {uri}");
trace!("Error: {e}");
exit(1);
println!("{name} - an intelligent and pluggable version of open");
println!();
println!("Usage:");
println!();
println!("{name} opens a browser at the git repo if one is present, otherwise opens the file explorer");
println!("{name} https://rains.cafe opens a browser at https://rains.cafe");
println!("{name} rains.cafe opens a browser at https://rains.cafe");
println!("{name} /home opens the file explorer at /home");
println!();
println!("Configs:");
println!();
println!("Update the config located at {0}", utils::config::get_path());
} else {
let uri = parsers::parse(&uri, &config.parsers).await.expect("Unable to parse URI");

if let Err(e) = open::that(&uri) {
error!("Failed to open uri! {uri}");
trace!("Error: {e}");
exit(1);
}
}

Ok(())
Expand Down
35 changes: 35 additions & 0 deletions src/utils/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use serde::{Deserialize, Serialize};
use std::{fs::File, path::Path};

#[derive(Serialize, Deserialize)]
pub struct Config {
pub parsers: Vec<String>,
}

pub fn get_path() -> String {
let home = home::home_dir().unwrap();
home.join(".config").join("smart-open.json").to_str().unwrap().to_string()
}

pub fn read() -> Config {
let config_path = get_path();
let path = Path::new(&config_path);

if !path.exists() {
let file = File::create(&config_path).expect("Failed to create config file");

let default_config = Config {
parsers: vec![
String::from("url"),
String::from("git"),
String::from("file"),
]
};

serde_json::to_writer_pretty(file, &default_config).expect("Failed to write to config file");
}

let file = File::open(path).expect("Failed to open config file");

serde_json::from_reader::<File, Config>(file).expect("Failed to parse config file")
}
3 changes: 2 additions & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod command;
pub mod git;
pub mod url;
pub mod url;
pub mod config;

0 comments on commit fe470cc

Please sign in to comment.