Skip to content

Commit

Permalink
Merge pull request #39 from siketyan/feat/resolve-default-org
Browse files Browse the repository at this point in the history
feat: Resolve <repo> pattern using default owner
  • Loading branch information
siketyan authored Dec 6, 2022
2 parents abacc76 + 264965b commit ded2324
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 16 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ If you have installed the shell extension, you can change directory into the clo
ghr clone <url_or_pattern> --cd
```

If you often use repositories of a specific owner, you can set the default owner to be resolved.

```toml
[defaults]
owner = "siketyan"
```

```shell
ghr clone <repo>
```

### Changing directory
You can change directory into a repository on the shell.
It requires installing the shell extension.
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/clone.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::mpsc::channel;
use std::time::Duration;

Expand Down Expand Up @@ -45,7 +44,7 @@ impl Cmd {
p.finish_and_clear();
});

let url = Url::from_str(&self.repo)?;
let url = Url::from_str(&self.repo, config.defaults.owner.as_deref())?;
let path = PathBuf::from(Path::resolve(&root, &url));
let profile = config
.rules
Expand Down
5 changes: 3 additions & 2 deletions src/cmd/delete.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::mpsc::channel;
use std::time::Duration;

use crate::config::Config;
use anyhow::Result;
use clap::Parser;
use console::style;
Expand All @@ -23,6 +23,7 @@ pub struct Cmd {
impl Cmd {
pub async fn run(self) -> Result<()> {
let root = Root::find()?;
let config = Config::load_from(&root)?;

if !Confirm::new()
.with_prompt(format!(
Expand All @@ -34,7 +35,7 @@ impl Cmd {
return Ok(());
}

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

let (tx, rx) = channel();
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/init.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::path::PathBuf;
use std::str::FromStr;

use anyhow::Result;
use clap::Parser;
Expand Down Expand Up @@ -32,7 +31,7 @@ impl Cmd {
let root = Root::find()?;
let config = Config::load_from(&root)?;

let url = Url::from_str(&self.repo)?;
let url = Url::from_str(&self.repo, config.defaults.owner.as_deref())?;
let path = Path::resolve(&root, &url);
let profile = config
.rules
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/open.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::path::PathBuf;
use std::str::FromStr;

use anyhow::Result;
use clap::Parser;
Expand All @@ -23,7 +22,7 @@ impl Cmd {
let root = Root::find()?;
let config = Config::load_from(&root)?;

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

config
Expand Down
7 changes: 7 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ use crate::profile::Profiles;
use crate::root::Root;
use crate::rule::Rules;

#[derive(Debug, Default, Deserialize)]
pub struct Defaults {
pub owner: Option<String>,
}

#[derive(Debug, Default, Deserialize)]
pub struct Config {
#[serde(default)]
pub defaults: Defaults,
#[serde(default)]
pub git: GitConfig,
#[serde(default)]
Expand Down
46 changes: 38 additions & 8 deletions src/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ pub struct Url {
}

impl Url {
pub fn from_str(s: &str, default_owner: Option<&str>) -> Result<Self> {
match s.contains("://") {
true => Self::from_url(&url::Url::from_str(s)?),
_ => Self::from_pattern(s, default_owner),
}
}

fn from_url(url: &url::Url) -> Result<Self> {
let mut segments = url
.path_segments()
Expand Down Expand Up @@ -120,8 +127,11 @@ impl Url {
})
}

fn from_pattern(s: &str) -> Result<Self> {
fn from_pattern(s: &str, default_owner: Option<&str>) -> Result<Self> {
lazy_static! {
static ref REPO: Regex =
Regex::new(r"^(?P<repo>[0-9A-Za-z_\.\-]+)$").unwrap();

static ref ORG_REPO: Regex =
Regex::new(r"^(?P<org>[0-9A-Za-z_\.\-]+)/(?P<repo>[0-9A-Za-z_\.\-]+)$").unwrap();

Expand Down Expand Up @@ -152,6 +162,14 @@ impl Url {
};
}

if let Some(owner) = default_owner {
pattern!(REPO, |c: Captures| Ok(Self {
owner: owner.to_string(),
repo: Self::remove_extensions(&group!(c, "repo")),
..Default::default()
}));
}

pattern!(ORG_REPO, |c: Captures| Ok(Self {
owner: group!(c, "org"),
repo: Self::remove_extensions(&group!(c, "repo")),
Expand Down Expand Up @@ -195,10 +213,7 @@ impl FromStr for Url {
type Err = Error;

fn from_str(s: &str) -> Result<Self> {
match s.contains("://") {
true => Self::from_url(&url::Url::from_str(s)?),
_ => Self::from_pattern(s),
}
Url::from_str(s, None)
}
}

Expand Down Expand Up @@ -270,6 +285,21 @@ mod tests {
)
}

#[test]
fn parse_from_pattern_repo() {
assert_eq!(
Url {
vcs: Vcs::Git,
scheme: Scheme::Https,
user: None,
host: Host::GitHub,
owner: "siketyan".to_string(),
repo: "siketyan.github.io".to_string()
},
Url::from_pattern("siketyan.github.io", Some("siketyan")).unwrap(),
)
}

#[test]
fn parse_from_pattern_org_repo() {
assert_eq!(
Expand All @@ -281,7 +311,7 @@ mod tests {
owner: "siketyan".to_string(),
repo: "siketyan.github.io".to_string()
},
Url::from_pattern("siketyan/siketyan.github.io").unwrap(),
Url::from_pattern("siketyan/siketyan.github.io", None).unwrap(),
)
}

Expand All @@ -296,7 +326,7 @@ mod tests {
owner: "siketyan".to_string(),
repo: "siketyan.github.io".to_string()
},
Url::from_pattern("gitlab.com:siketyan/siketyan.github.io").unwrap(),
Url::from_pattern("gitlab.com:siketyan/siketyan.github.io", None).unwrap(),
)
}

Expand All @@ -311,7 +341,7 @@ mod tests {
owner: "siketyan".to_string(),
repo: "siketyan.github.io".to_string()
},
Url::from_pattern("[email protected]:siketyan/siketyan.github.io.git").unwrap(),
Url::from_pattern("[email protected]:siketyan/siketyan.github.io.git", None).unwrap(),
)
}

Expand Down

0 comments on commit ded2324

Please sign in to comment.