Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Resolve <repo> pattern using default owner #39

Merged
merged 3 commits into from
Dec 6, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: Resolve <repo> pattern using defaults
siketyan committed Dec 6, 2022

Verified

This commit was signed with the committer’s verified signature.
siketyan Naoki Ikeguchi
commit 8d289d573232ccf9edb73dc1c11d68e1df69638f
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;

@@ -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
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;
@@ -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!(
@@ -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();
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;
@@ -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
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;
@@ -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
7 changes: 7 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -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)]
25 changes: 20 additions & 5 deletions src/url.rs
Original file line number Diff line number Diff line change
@@ -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()
@@ -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();

@@ -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")),
@@ -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)
}
}