Skip to content

Commit

Permalink
feat: git provider flag for add command (resolves #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
joseluisq committed Dec 9, 2020
1 parent d7c1abd commit 3481ae1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/cli/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl<'a> Actions<'a> {
}

/// Command action to install a new package and invoke a `paket_install` Fish shell event.
pub fn install(&self, pkg_name: &str) -> Result {
pub fn install(&self, pkg_name: &str, git_provider: &str) -> Result {
let pkgfmt = PkgNameFmt::from(&pkg_name)?;
let pkg_name = &pkgfmt.get_short_name();
let pkg_tag = Some(pkgfmt.pkg_tag.as_ref());
Expand All @@ -35,7 +35,7 @@ impl<'a> Actions<'a> {
);
}

self.git.clone(pkg_name, pkg_tag)?;
self.git.clone(pkg_name, pkg_tag, git_provider)?;

// Process Fish shell package structure
let pkg_dir = self.git.base_dir.join(&pkg_name);
Expand Down
2 changes: 1 addition & 1 deletion src/cli/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl App {

if let Some(commands) = &pk.opts.commands {
match commands {
Commands::Add { pkg_name } => actions.install(pkg_name),
Commands::Add { pkg_name, provider } => actions.install(pkg_name, provider),
Commands::Update { pkg_name } => actions.update(pkg_name),
Commands::Remove { pkg_name } => actions.remove(pkg_name),
}?
Expand Down
3 changes: 3 additions & 0 deletions src/cli/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ pub enum Commands {
/// Add a new package from a local or remote repository
#[structopt(name = "add")]
Add {
#[structopt(short = "p", long, default_value = "github")]
/// A Git host provider like github, bitbucket or gitlab.
provider: String,
/// Package name. E.g joseluisq/gitnow
pkg_name: String,
},
Expand Down
24 changes: 18 additions & 6 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,31 @@ impl Git {
})
}

fn get_remote_endpoint(user_repo_name: &str) -> String {
// TODO: support more Git host providers
// GitHub support for now
["https://github.com/", user_repo_name, ".git"].concat()
fn get_remote_endpoint(user_repo_name: &str, provider: &str) -> Result<String> {
let provider = match provider {
"github" => "github.com",
"bitbucket" => "bitbucket.org",
"gitlab" => "gitlab.com",
_ => bail!(
"git host provider not supported. use a full url endpoint with `add` command instead"
),
};

Ok(["https://", provider, "/", user_repo_name, ".git"].concat())
}

fn exec_name(&self) -> &'static str {
"git"
}

/// Clone a Git repository.
pub fn clone(&self, user_repo_name: &str, branch_tag: Option<&str>) -> Result<String> {
let endpoint = Git::get_remote_endpoint(user_repo_name);
pub fn clone(
&self,
user_repo_name: &str,
branch_tag: Option<&str>,
git_provider: &str,
) -> Result<String> {
let endpoint = Git::get_remote_endpoint(user_repo_name, git_provider)?;

let out_dir = self.base_dir.join(user_repo_name);
if !out_dir.exists() {
Expand Down

0 comments on commit 3481ae1

Please sign in to comment.