Skip to content

Commit

Permalink
feat: Open an application after cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
siketyan committed Nov 22, 2022
1 parent 1158ba0 commit 3d51e90
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 7 deletions.
59 changes: 59 additions & 0 deletions src/application.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use std::collections::HashMap;
use std::ops::Deref;
use std::path::Path;
use std::process::Command;

use anyhow::{anyhow, Result};
use serde::Deserialize;

#[derive(Debug, Deserialize)]
pub struct Application {
cmd: String,
args: Vec<String>,
}

impl Application {
pub fn open<P>(&self, path: P) -> Result<()>
where
P: AsRef<Path>,
{
let _ = Command::new(&self.cmd)
.args(
self.args
.iter()
.map(|arg| match arg.as_str() {
"%p" => path.as_ref().to_string_lossy().to_string(),
_ => arg.to_string(),
})
.collect::<Vec<_>>(),
)
.spawn()?;

Ok(())
}
}

#[derive(Debug, Default, Deserialize)]
pub struct Applications {
#[serde(flatten)]
map: HashMap<String, Application>,
}

impl Applications {
pub fn open<P>(&self, name: &str, path: P) -> Result<()>
where
P: AsRef<Path>,
{
self.get(name)
.ok_or_else(|| anyhow!("Application entry does not exists."))?
.open(path)
}
}

impl Deref for Applications {
type Target = HashMap<String, Application>;

fn deref(&self) -> &Self::Target {
&self.map
}
}
28 changes: 21 additions & 7 deletions src/cmd/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ pub struct Cmd {
/// Change directory after cloned a repository (Shell extension required).
#[clap(long)]
cd: bool,

/// Opens the directory after cloned a repository.
#[clap(long)]
open: Option<String>,
}

impl Cmd {
Expand All @@ -41,18 +45,13 @@ impl Cmd {
});

let url = Url::from_str(&self.repo)?;
let path = Path::resolve(&root, &url);
let path = PathBuf::from(Path::resolve(&root, &url));
let profile = config
.rules
.resolve(&url)
.and_then(|r| config.profiles.resolve(&r.profile));

let repo = Repository::clone(&url.to_string(), PathBuf::from(&path))?;
if let Some((name, p)) = profile {
p.apply(&mut repo.config()?)?;

info!("Attached profile [{}] successfully.", style(name).bold());
}
let repo = Repository::clone(&url.to_string(), &path)?;

tx.send(())?;
progress.await?;
Expand All @@ -62,6 +61,21 @@ impl Cmd {
repo.workdir().unwrap().to_string_lossy(),
);

if let Some((name, p)) = profile {
p.apply(&mut repo.config()?)?;

info!("Attached profile [{}] successfully.", style(name).bold());
}

if let Some(app) = self.open {
config.applications.open(&app, &path)?;

info!(
"Opened the repository in [{}] successfully.",
style(&app).bold(),
);
}

Ok(())
}
}
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fs::read_to_string;
use anyhow::Result;
use serde::Deserialize;

use crate::application::Applications;
use crate::profile::Profiles;
use crate::root::Root;
use crate::rule::Rules;
Expand All @@ -12,6 +13,8 @@ pub struct Config {
#[serde(default)]
pub profiles: Profiles,
#[serde(default)]
pub applications: Applications,
#[serde(default)]
pub rules: Rules,
}

Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod application;
mod cmd;
mod config;
mod console;
Expand Down

0 comments on commit 3d51e90

Please sign in to comment.