Skip to content

Commit

Permalink
Merge pull request #1 from dob9601/https-default-support
Browse files Browse the repository at this point in the history
https default support
  • Loading branch information
dob9601 authored Feb 5, 2022
2 parents 9422352 + fc9b9cd commit 6535f37
Show file tree
Hide file tree
Showing 11 changed files with 406 additions and 84 deletions.
97 changes: 92 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ lazy_static = "1.4.0"
regex = "1.5.4"
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8"
sha-1 = "0.10.0"
strum = "0.23.0"
strum_macros = "0.23"
shellexpand = "2.1.0"
tempfile = "3"
toml = "0.5"
29 changes: 26 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use clap::Parser;

use crate::git::remote::{RepoHostName, ConnectionMethod};

#[derive(Parser, Debug)]
#[clap(name = "jointhedots", bin_name = "jtd", about)]
pub enum JoinTheDots {
Expand All @@ -14,19 +16,30 @@ pub struct InstallSubcommandArgs {
#[clap(help = "The location of the repository in the form USERNAME/REPONAME")]
pub repository: String,

#[clap(
arg_enum,
long = "method",
short = 'm',
help = "The method to use for cloning/pushing the repository",
default_value = "https"
)]
pub method: ConnectionMethod,

#[clap(
help = "The dotfiles to install. If unspecified, install all of them",
conflicts_with = "all"
)]
pub target_dotfiles: Vec<String>,

#[clap(
arg_enum,
default_value = "GitHub",
help = "Whether to source the repo from GitHub or GitLab",
long = "source",
short = 's'
short = 's',
ignore_case = true,
)]
pub source: String,
pub source: RepoHostName,

#[clap(
help = "Whether to overwrite existing configs without prompt",
Expand Down Expand Up @@ -64,11 +77,21 @@ pub struct SyncSubcommandArgs {
pub target_dotfiles: Vec<String>,

#[clap(
arg_enum,
long = "method",
short = 'm',
help = "The method to use for cloning/pushing the repository",
default_value = "ssh"
)]
pub method: ConnectionMethod,

#[clap(
arg_enum,
default_value = "GitHub",
help = "Whether to source the repo from GitHub or GitLab",
long = "source"
)]
pub source: String,
pub source: RepoHostName,
}

#[derive(clap::Args, Debug)]
Expand Down
File renamed without changes.
89 changes: 89 additions & 0 deletions src/git/remote.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use std::{error::Error, str::FromStr};

use clap::ArgEnum;
use strum_macros::{EnumIter, Display};

#[derive(ArgEnum, Clone, EnumIter, Display, Debug)]
pub enum ConnectionMethod {
SSH,
HTTPS
}

impl FromStr for ConnectionMethod {
type Err = Box<dyn Error>;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"ssh" => Ok(ConnectionMethod::SSH),
"https" => Ok(ConnectionMethod::HTTPS),
v => Err(format!("Failed to convert: '{}' is not a known variant.", v).into()),
}
}
}

#[derive(ArgEnum, Clone, EnumIter, Display, Debug, PartialEq)]
#[clap(rename_all = "PascalCase")]
pub enum RepoHostName {
GitHub,
GitLab
}

impl FromStr for RepoHostName {
type Err = Box<dyn Error>;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"github" => Ok(RepoHostName::GitHub),
"gitlab" => Ok(RepoHostName::GitLab),
v => Err(format!("Failed to convert: '{}' is not a known variant.", v).into()),
}
}
}

pub struct RepoHost {
ssh_prefix: &'static str,
https_prefix: &'static str
}

const GITLAB: RepoHost = RepoHost {
ssh_prefix: "git@gitlab.com:",
https_prefix: "https://gitlab.com/"
};

const GITHUB: RepoHost = RepoHost {
ssh_prefix: "git@github.com:",
https_prefix: "https://github.com/"
};

pub fn get_host_git_url(repository: &str, host: &RepoHostName, method: &ConnectionMethod) -> Result<String, Box<dyn Error>> {
let repo_host = match *host {
RepoHostName::GitHub => GITHUB,
RepoHostName::GitLab => GITLAB,
};

match method {
ConnectionMethod::SSH => Ok(format!("{}{}{}", repo_host.ssh_prefix, repository, ".git")),
ConnectionMethod::HTTPS => Ok(format!("{}{}{}", repo_host.https_prefix, repository, ".git")),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_get_host_git_url() {
let repo = "dob9601/dotfiles";
let host = RepoHostName::GitHub;
let method = ConnectionMethod::SSH;

let host_url = get_host_git_url(repo, &host, &method).expect("Failed to get host url");
assert_eq!(host_url, String::from("git@github.com:dob9601/dotfiles.git"))
}

#[test]
fn test_repo_host_name_from_str() {
let hostname = "github";
assert_eq!(<RepoHostName as std::str::FromStr>::from_str(hostname).expect("Could not convert from str"), RepoHostName::GitHub)
}
}
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
pub mod cli;
pub mod git;
pub mod structs;
pub mod utils;

pub mod git {
pub mod operations;
pub mod remote;
}

pub mod subcommands {
mod install;
mod interactive;
Expand Down
Loading

0 comments on commit 6535f37

Please sign in to comment.