-
-
Notifications
You must be signed in to change notification settings - Fork 8
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 #12 from siketyan/feat/open-application
feat: Open application in a repository
- Loading branch information
Showing
8 changed files
with
121 additions
and
6 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
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 | ||
} | ||
} |
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
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,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(()) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod application; | ||
mod cmd; | ||
mod config; | ||
mod console; | ||
|