Skip to content

Commit

Permalink
Merge pull request #12 from siketyan/feat/open-application
Browse files Browse the repository at this point in the history
feat: Open application in a repository
  • Loading branch information
siketyan authored Nov 22, 2022
2 parents 9df2358 + a02b262 commit 6728f83
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Commands:
clone Clones a Git repository to local
delete Deletes a repository from local
init Initialises a Git repository in local
open Opens a repository in an application
path Prints the path to root, owner, or a repository
profile Manages profiles to use in repositories
shell Writes a shell script to extend ghr features
Expand Down
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
}
}
24 changes: 19 additions & 5 deletions src/cmd/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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 Down Expand Up @@ -51,11 +55,6 @@ impl Cmd {
config.git.strategy.clone.clone_repository(url, &path)?;

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

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

tx.send(())?;
progress.await?;
Expand All @@ -65,6 +64,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(())
}
}
2 changes: 1 addition & 1 deletion src/cmd/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::url::Url;

#[derive(Debug, Parser)]
pub struct Cmd {
/// URL or pattern of the repository to clone.
/// URL or pattern of the repository to delete.
repo: String,
}

Expand Down
4 changes: 4 additions & 0 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod cd;
mod clone;
mod delete;
mod init;
mod open;
mod path;
mod profile;
mod shell;
Expand All @@ -19,6 +20,8 @@ pub enum Action {
Delete(delete::Cmd),
/// Initialises a Git repository in local.
Init(init::Cmd),
/// Opens a repository in an application.
Open(open::Cmd),
/// Prints the path to root, owner, or a repository.
Path(path::Cmd),
/// Manages profiles to use in repositories.
Expand All @@ -41,6 +44,7 @@ impl Cli {
Clone(cmd) => cmd.run().await,
Delete(cmd) => cmd.run().await,
Init(cmd) => cmd.run(),
Open(cmd) => cmd.run(),
Path(cmd) => cmd.run(),
Profile(cmd) => cmd.run(),
Shell(cmd) => cmd.run(),
Expand Down
33 changes: 33 additions & 0 deletions src/cmd/open.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::path::PathBuf;
use std::str::FromStr;

use anyhow::Result;
use clap::Parser;

use crate::config::Config;
use crate::path::Path;
use crate::root::Root;
use crate::url::Url;

#[derive(Debug, Parser)]
pub struct Cmd {
/// URL or pattern of the repository to open application in.
repo: String,

/// Name of the application entry.
application: String,
}

impl Cmd {
pub fn run(self) -> Result<()> {
let root = Root::find()?;
let config = Config::load_from(&root)?;

let url = Url::from_str(&self.repo)?;
let path = PathBuf::from(Path::resolve(&root, &url));

config.applications.open(&self.application, path)?;

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::git::Config as GitConfig;
use crate::profile::Profiles;
use crate::root::Root;
Expand All @@ -15,6 +16,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 6728f83

Please sign in to comment.